0

I am working on an application in which I have developed a window with three check boxes. When first and second check boxes are selected, new windows will be opened as desired. The third check box is for closing the application. When Exit check box is selected, it is showing the conformation Dialog as desired but Exit check box is not ticked.

I could not trace out the issue here. Please help me to resolve this issue! enter image description here

package jcheckbox;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class InitiaaWindow extends JPanel {
    static JFrame frame = new JFrame("Credit Contract Validation");

    private static final long serialVersionUID = 1L;
    JCheckBox jValidateECOUT;
    JCheckBox jValidateSuperDeals;
    JCheckBox jEXIT;

    JLabel jlbPicture,jlbPicture1;
    CheckBoxListener myListener = null;

    public InitiaaWindow() {

        myListener = new CheckBoxListener();

        jValidateECOUT = new JCheckBox("ValidateECOUT");
        jValidateECOUT.setMnemonic(KeyEvent.VK_C);      
        jValidateECOUT.setSelected(false);
        jValidateECOUT.addItemListener(myListener);

        jValidateSuperDeals = new JCheckBox("ValidateSuperDeals");
        jValidateSuperDeals.setMnemonic(KeyEvent.VK_G);      
        jValidateSuperDeals.setSelected(false);
        jValidateSuperDeals.addItemListener(myListener);

        jEXIT = new JCheckBox("EXIT");
        jEXIT.setMnemonic(KeyEvent.VK_G);    
        jEXIT.setSelected(false);
        jEXIT.addItemListener(myListener);

        jlbPicture = new JLabel(new ImageIcon("src/jcheckbox/image.jpg"));
        jlbPicture1 = new JLabel(new ImageIcon("src/jcheckbox/image1.jpg"));

        JPanel jplCheckBox = new JPanel();
        jplCheckBox.setLayout(new GridLayout(0, 1));        
        jplCheckBox.add(jValidateECOUT);
        jplCheckBox.add(jValidateSuperDeals);
        jplCheckBox.add(jEXIT);

        setLayout(new BorderLayout());
        add(jplCheckBox, BorderLayout.WEST);
        add(jlbPicture1, BorderLayout.CENTER);
        add(jlbPicture, BorderLayout.EAST);
        setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
    }
    class CheckBoxListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {

            if (jValidateECOUT.isSelected())
            {
                try {
                    UIPack.UI.myMethod(null);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            else if (jValidateSuperDeals.isSelected())
            {
                try {
                    ValidateSuperDealsUIPack.UI.ValidateSuperDealsUI(null);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            else if (jEXIT.isSelected())
            {
                int dialogButton = JOptionPane.YES_NO_OPTION;
                int dialogResult = JOptionPane.showConfirmDialog(null, "Would you like to close the application", "Conformation message",dialogButton);
                if(dialogResult==0)
                  System.exit(1);
                else
                    JOptionPane.getRootFrame().dispose();
            }
            }
        }

    public static void main(String s[]) {
         frame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                System.exit(0);
             }
         });     
         frame.setContentPane(new InitiaaWindow());
         frame.pack();
         frame.setVisible(true);
    }    
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Java Developer
  • 165
  • 4
  • 8
  • 18

3 Answers3

1

Since showConfirmDialog creates a modal dialog until the dialog is closed the execution of item selection event may not propagate to other listeners which might be responsible for updating the display of the checkbox.

If you click 'No' in the dialog, does the checkbox update properly? If yes then you can create the dialog in a separate runnable task using SwingUtilities.invokeLater this will ensure the current processing of selection event completes before the modal dialog is opned.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
0

You can add

jEXIT.setSelected(true);

before

int dialogButton = JOptionPane.YES_NO_OPTION;
  • if jEXIT.setSelected(true); before int dialogButton = JOptionPane.YES_NO_OPTION; jEXIT is alaways true and which ever the check box is checked, respective window along with exit button Dialog box is opening. – Java Developer Oct 10 '13 at 10:48
  • Sorry but I didn't understand what is the problem with the solution. Can you be more explanatory? – Mehmet Sedat Güngör Oct 10 '13 at 11:00
  • Exit check box is coded to show the conformation Dialog if it is checked. As desired it is showing the Dialog but Exit check box is not getting ticked/selected. Where as firts and second check boxes are working fine. – Java Developer Oct 10 '13 at 11:08
0

1.Sorry, but your code style is really terrible (IMHO, I don't want you to be dissappointed of that).

2.Use ActionListener instead of ItemListener for JCheckBox. And check the source of action before process it:

ActionListener listener = new ActionListener({

    @Override
    public void actionPerfomed(ActionEvent ae) {
        if (ae.getSource().equals(comboBox1) {
            //process 1
        } else if (ae.getSource().equals(comboBox2) {
            //process 2
        } else if (ae.getSource().equals(comboBox3) {
            //process 3
        }
    }
});
comboBox1.add(listener);
comboBox2.add(listener);
comboBox3.add(listener);

3.If you want to show modal dialogs on action events, it is good to use SwingUtilities.invokeLater() for that, because your action' source should repaint after action perfomed.

SeniorJD
  • 6,946
  • 4
  • 36
  • 53