Hi,

I'm trying to implement an option dialogue in a tentative game application.

In that dialogue the user can choose the opening of the game. If s/he select "Manual" it is showed another windows dialogue (JOptionPane) where s/he has to input the wanted opening. The inputs are to be validated.

My code doesn't work as I espected :(

When I click on the cancel botton of the second dialogue (JOptionPane) without any inputs it doesn't close, I have to click twice.

A similar behaviour it has after the validation.

I think that I'm misusing the propretryChange. I've read a lot of tutorials, but I haven't succeeded to understand what to do .

Can someone help me?

The concerned code follows.

Shall I post the whole application? It's just the initial frame with a bar menu, where only the option item is active.

Java Code:
package MainFrame.BaoMenuBar.BaoBar_Menus.Menu_Items.Tools_MenuItems.GameSetup_Dialogue;

import MainFrame.BaoFrame.MainFrame;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;

/**
 *
 * @author Nino
 */
public class GameSetup extends JDialog {
 
    private static String player;
    private final CustomDialog myDialog;
    
    public GameSetup( final MainFrame ff) {
        
        super(ff, "Set Up", true);
        
        myDialog = new CustomDialog(ff, "geisel", this);
        myDialog.pack();        
        setSize(300,350);
        setLocation(100,100);
        panel.setLayout(null);
        Container cp=getContentPane();
        
        panel.setSize(300,300);
        
        myCom.setSize(80,20);
        myCom.setLocation(50,50);
        
        type_game.setSize(80,24);
        type_game.setLocation(40,20);

        kutakatia.setSize(150,24);
        kutakatia.setLocation(40,90);
                
        attiva.setSize(80,24);
        attiva.setLocation(50,120);
        inattiva.setSize(80,24);
        inattiva.setLocation(150,120);
        
        Buttongroup.add(attiva);
        Buttongroup.add(inattiva);
        
        Buttongroup.setSelected(attiva.getModel(),true);
        
        attiva.setEnabled(true);
        inattiva.setEnabled(true);

        longmove.setSize(100,24);
        longmove.setLocation(40,150);
        n_giri.setSize(80,24);
        n_giri.setLocation(160,150);
        n_giri.setText("12");

        durata.setSize(100,24);
        durata.setLocation(40,180);
        int min=5;
        button1=new JButton("OK");
        button2=new JButton("Cancel");
        
        button1.setSize(80,24);
        button2.setSize(80,24);
        
        button1.setLocation(50,250);
        button2.setLocation(130,250);
        
        button1.addActionListener(new ActionListener() {
        
            public void actionPerformed(ActionEvent e) {
                dispose();
                setVisible(false);
                String selected=(String) myCom.getSelectedItem();
                livello=selected;
                //mynewFrame.start_Again();
            }
        });
        
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        panel.setLayout(null);
        panel.add(myCom);
        panel.add(button1);
        panel.add(button2);
        panel.add(type_game);
        panel.add(kutakatia);
        panel.add(attiva);
        panel.add(longmove);
        panel.add(n_giri);
        panel.add(inattiva);
        panel.add(durata);
        
        cp.add(panel);
        
        myCom.addItemListener(new GameSetup.MyItemLestin());
        myCom.addItem("Official");
        myCom.addItem("Novice");
        myCom.addItem("Manual");
        
    }

   public static boolean getKutakatia(){
            return attiva.isEnabled();
   }

   public String getLevel(){
       return myCom.getSelectedItem().toString();
   }
   public String getNGiri(){
       return n_giri.getText();
   }
   public static String getLivello(){
       return livello;
   }
   public static String getPlayer(){
       return player;
   }
 
   void setLabel(String newText) {
        // label.setText(newText);
    }
 

   class MyItemLestin implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            String selected=((JComboBox)e.getSource()).getSelectedItem().toString();
            
            if("Official".equals(selected)) {
                attiva.setEnabled(true);
                
            } else if("Novice".equals(selected)) {
                attiva.setEnabled(false);
            } else{
                if (myDialog.isVisible()) {
                    myDialog.setVisible(false);
                }else{
                    myDialog.setResizable(false);
                    myDialog.setVisible(true);
                }
            }   
        }
        
                
    }
    
    private final JButton button1;
    private final JButton button2;
    private final JPanel panel=new JPanel();
    private final JLabel type_game=new JLabel(" Opening: ");
    private final JLabel kutakatia=new JLabel(" Ignore KUTAKATIA? ");
    private final JLabel longmove=new JLabel(" Long move limit: ");
    private final JLabel durata=new JLabel(" Game time limit: ");
    private final JTextField n_giri=new JTextField(2);
    private static JRadioButton attiva=new JRadioButton("No");
    private static JRadioButton inattiva=new JRadioButton("Yes");
    
    private static String livello="Official";    
    private ButtonGroup Buttongroup = new ButtonGroup();
    private final MyComboBox myCom=new MyComboBox();
        
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
    }
    
    
}
class MyComboBox extends JComboBox {
    MyComboBox() {
    }
}

class CustomDialog extends JDialog
                   implements ActionListener,
                              PropertyChangeListener {
    private String typedText = null;
    private JTextField textField;
    private GameSetup dd;
    private JOptionPane optionPane;

    private String btnString1 = "Confirm";
    private String btnString2 = "Cancel";
    private String NTurn = "North plays";
    private String STurn = "South plays";
    private JCheckBox cbNNyumba= new JCheckBox();
    private JCheckBox cbSNyumba= new JCheckBox();
    private final JLabel Nyumba0=new JLabel("Nyumba");
    private final JLabel Nyumba1=new JLabel("Nyumba");
    private static JRadioButton rdbNTurn=new JRadioButton("");
    private static JRadioButton rdbSTurn=new JRadioButton("");
    
    JTextField[][] tCells = new JTextField[5][9];
    private int n;


    public String getValidatedText() {
        return typedText;
    }

    /** Creates the reusable dialog. */
    public CustomDialog(Frame aFrame, String aWord, GameSetup parent) {
        super(aFrame, true);
        dd = parent;

        setTitle("Free opening");
        textField = new JTextField(10);
        String msgString1 = "<html><body><div width='300px' align='center'>Input the opening setup.</div></body></html>";
        Object[] options = {btnString1, btnString2};

        optionPane = new JOptionPane(msgString1,
                                    JOptionPane.PLAIN_MESSAGE,
                                    JOptionPane.YES_NO_OPTION,
                                    null,
                                    options,
                                    options[0]);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setSize(300,300);
 
        JRadioButton rdbNTurn = new JRadioButton(NTurn);
        rdbNTurn.setActionCommand(NTurn);
        rdbNTurn.setSelected(false);
 
        JRadioButton rdbSTurn = new JRadioButton(STurn);
        rdbSTurn.setActionCommand(STurn);
        rdbSTurn.setSelected(true);

        ButtonGroup group = new ButtonGroup();
        group.add(rdbNTurn);
        group.add(rdbSTurn);

        cbNNyumba.setSelected(true);
        cbNNyumba.addItemListener(null);        
        addItem(panel,cbNNyumba,1,0,1,1,GridBagConstraints.EAST);
        addItem(panel,Nyumba0,GridBagConstraints.RELATIVE,0,3,1,GridBagConstraints.WEST);        
        addItem(panel,rdbNTurn,GridBagConstraints.RELATIVE,0,4,1,GridBagConstraints.EAST);                                
        tCells[0][1] = new JTextField(2);
        tCells[0][1].setText("0");
        addItem(panel,tCells[0][1],0,3,1,1,GridBagConstraints.CENTER);        
        for (int columnCount = 1; columnCount < 9; columnCount++) {
            for (int rowCount = 1; rowCount < 5; rowCount++) {
                tCells[rowCount][columnCount] = new JTextField(2);
                tCells[rowCount][columnCount].setText("0");
                addItem(panel,tCells[rowCount][columnCount],columnCount,rowCount+2,1,1,GridBagConstraints.CENTER);                        
            }
        }

        tCells[1][0] = new JTextField(2);
        tCells[1][0].setText("0");
        addItem(panel,tCells[1][0],9,6,1,1,GridBagConstraints.CENTER);        
        cbSNyumba.setSelected(true);
        cbSNyumba.addItemListener(null);
        addItem(panel,cbSNyumba,1,8,1,1,GridBagConstraints.EAST);        
        addItem(panel,Nyumba1,GridBagConstraints.RELATIVE,8,3,1,GridBagConstraints.WEST);        
        addItem(panel,rdbSTurn,GridBagConstraints.RELATIVE,8,4,1,GridBagConstraints.EAST);                                
        
        optionPane.add(panel,1); //1=in mezzo
        
        setContentPane(optionPane);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    optionPane.setValue(new Integer(
                                        JOptionPane.CLOSED_OPTION));
            }
        });

        optionPane.addPropertyChangeListener(this);
    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
    GridBagConstraints gc = new GridBagConstraints();
    gc.gridx = x;
    gc.gridy = y;
    gc.gridwidth = width;
    gc.gridheight = height;
    gc.ipady=1;
    gc.insets = new Insets(0, 0, 0, 0);
    gc.anchor = align;
    gc.fill = GridBagConstraints.NONE;
    p.add(c, gc);
  }
    

    public void actionPerformed(ActionEvent e) {
        optionPane.setValue(btnString1);
    }

    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();

        if (isVisible()
         && (e.getSource() == optionPane)
         && (optionPane.VALUE_PROPERTY.equals(prop) ||
             optionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
            Object value = optionPane.getValue();

            if (value == JOptionPane.UNINITIALIZED_VALUE) {
                return;
            }

            JOptionPane checkPane = new JOptionPane();
            optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            
            if (btnString1.equals(value)) {
                System.out.println(value+" 2\n");
                int m=isTextValid();
                String warning = "";                
                if (m==1) {
                    warning = "According to seeds number the player "+
                            "with the move is wrong";
                    checkPane.showMessageDialog(null,
                            warning,
                            "Invalid Input", JOptionPane.ERROR_MESSAGE);
                }else if (m>100 && m<200) {
                    
                    warning = "The are "+(m-100)+" extra seeds.";
                    checkPane.showMessageDialog(null,
                            warning,
                            "Invalid Input", JOptionPane.ERROR_MESSAGE);
                }else if (m>200) {
                    warning = "The are "+(m-200)+" missing seeds.";
                    Object[] options = {"Confirm",
                        "Cancel"};
                    n = checkPane.showOptionDialog(null,
                            warning,
                            "Non standard input",
                            checkPane.YES_NO_OPTION,
                            checkPane.QUESTION_MESSAGE,
                            null,        //do not use a custom Icon
                            options,     //the titles of buttons
                            options[0]); //default button title
                            if(n==checkPane.NO_OPTION)
                            {
                                this.clearAndHide();
                            }

                            
                }else{
                             clearAndHide();
                }

            } else {
                typedText = null;
                
                this.clearAndHide();
            }
        }
}
    private int isTextValid() {
        int msg = 0;
        try {
            int number = 0;
            int nGhala = 0;
            int sGhala = 0;
            for (int columnCount = 1; columnCount < 9; columnCount++) {
                for (int rowCount = 1; rowCount < 5; rowCount++) {
                    number += Integer.parseInt(tCells[rowCount][columnCount].getText());
                }
            }
            nGhala = Integer.parseInt(tCells[0][1].getText());
            sGhala = Integer.parseInt(tCells[1][0].getText());
            if (number+nGhala+sGhala >64) {
                msg=number+nGhala+sGhala-64+100; // troppi semi NO
            }else if(rdbNTurn.isSelected() && nGhala == sGhala && nGhala>0 && sGhala>0){
                msg=1; // il turno deve NO
            }else if(number+nGhala+sGhala <64){
                msg=64-number-nGhala-sGhala+200; // mancano dei semi VA BENE
            }
        } catch (NumberFormatException e) {
            // one of the few times it's OK to ignore an exception
        }
        return msg;
   }
    public void clearAndHide() {
        setVisible(false);
    }
 }