0

I'd like the program to cancel the whole process when the NO_OPTION is chosen, only showing the output "The price has not been changed". But when I press the No-button the showInputDialog option is started. What am I doing wrong? Thanks in advance.

import javax.swing.JOptionPane;
public class Gebrauchtwagen {
    public static void main (String... args){
        Auto Auto1 = new Auto(8999.99, "VW Golf", 2007, 56845, "blau");

        System.out.println(Auto1.getPreis());
        System.out.println(Auto1.getModell());
        System.out.println(Auto1.getBaujahr());
        System.out.println(Auto1.getKilometerstand());
        System.out.println(Auto1.getFarbe());
 
        int dialogButton = JOptionPane.YES_NO_OPTION;
        JOptionPane.showConfirmDialog(null,  "Wollen Sie den Preis des Fahrzeuges ändern?", "Frage", dialogButton);
 
        if(dialogButton == JOptionPane.YES_OPTION) {
            String eingabe = JOptionPane.showInputDialog("Der geänderte Preis:");
     
            double preisNeu = Double.parseDouble(eingabe);
     
             Auto1.setPreis(preisNeu);
             System.out.print("Der neue Preis beträgt:"+Auto1.getPreis());
        }
        if(dialogButton == JOptionPane.NO_OPTION) {
            System.out.print("The price has not been changed.");
        }
    }
}
aren pi
  • 29
  • 5
  • 1
    Please read the [docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,java.lang.Object,java.lang.String,int)). `showConfirmDialog` returns the result that you need to check, not `dialogButton` – QBrute Jun 25 '20 at 09:45
  • Thanks QBrute, a look into the docs made some things clear to me. – aren pi Jun 25 '20 at 09:59

1 Answers1

2

Use the return value showConfirmDialog to get the result

int result = JOptionPane.showConfirmDialog(...)
Reimeus
  • 158,255
  • 15
  • 216
  • 276