Results 1 to 8 of 8
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Issue with JCheckBox and JOptionPane

    Hey I am having an issue with using a JOptionPane with a JCheckBox. I want to click a JCheckbox that fires input dialog using JOptionPane. What happens is when The JOptionpane Opens, it will ask for input but the checkbox will not stay checked but it will perform the action. I can keep clicking and entering data but will never stay Checked. I then tried adding JCheckBox.setSelected(true); then what happens now is the JOptionPane will pop up twice but this time the Checkbox will actually become Selected and only accept the input from first JOptionPane not the second.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Code.

    If it is too long create a SSCCE.

  3. #3
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Sorry about that I will include

    Java Code:
    private void otherJCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {                                                     
            checkBoxesStates();
        }     
    
    private void checkBoxesStates()
        {
            boolean isOtherChecked = otherJCheckBox.isSelected();
            double otherAmount = 0.00;
    
            if(isOtherChecked)
            {
                String otherTemp = JOptionPane.showInputDialog("Enter Your Choice");
                otherJCheckBox.setSelected(true);
                oTAmount = Double.parseDouble(otherTemp);
            }
    
            calculate(isOtherChecked, oTAmount);
            calculationsJTextArea.setText(showCalc.toString());
        }
    Last edited by patriotsfan; 06-06-2011 at 09:44 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    1. Post in an appropriate section of the forums. I've moved the thread this time.

    2. Use code tags. This time, I've added them for you.

    3. You were asked for an SSCCE. The code you posted, isn't. When in doubt, ask -- or search the net.

    4. Another recommended page: How to ask questions the smart way. It'll help you to phrase your question in a way that it can actually be answered.

    db

  5. #5
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    I did some googling and I found this same problem listed as a bug on java sun website, the problem has no solution posted only explanation with sample code.
    Bug ID: 6924233 JOptionPane inside JCheckBox itemListener causes setSelected(false)

    Here is sample of problem
    Java Code:
    JCheckbox checkBox = new JCheckBox("hitme");
    
    checkbox.addItemListener(new ItemListener()
       {
            public void itemStateChanged(ItemEvent ie)
            {
        	      int retval = JOptionPane.showConfirmDialog(null, "message", "title", JOptionPane.OK_CANCEL_OPTION);
             }
       });
    Last edited by patriotsfan; 06-06-2011 at 10:39 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    As has been stated by others, without a small compilable runnable program, without our ability to test your code, there's no way of knowing if this bug applies to you. Odds still strongly favor a bug in your code rather than a Swing bug as the cause for your problems. If you're seriously in need of our help, then create and post an SSCCE. You've been given the link several times on what this requires, so the rest is up to you.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Edit: This is an SSCCE that shows your problem and the solution:

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class WeirdCheckBox {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            final JCheckBox checkbox = new JCheckBox("hitme");
    
            checkbox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent ie) {
                    // int retval = JOptionPane.showConfirmDialog(null, "message",
                    // "title", JOptionPane.OK_CANCEL_OPTION);
                }
            });
    
            checkbox.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    int retval = JOptionPane.showConfirmDialog(null, "message",
                            "title", JOptionPane.OK_CANCEL_OPTION);
                }
            });
    
            JPanel panel = new JPanel();
            panel.add(checkbox);
    
            JFrame frame = new JFrame("App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    The solution is to use an ActionListener not an ItemListener. If you had posted something like this sooner when initially asked, we'd have gotten a solution to you sooner and more of the effort would have been yours and not mine -- which is as it should be since we're helping you for free, and I do consider my time valuable. Again, next time, please post an SSCCE when asked.

  8. #8
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Thanks for the help, the ActionListener solved my problem.

Similar Threads

  1. Jcheckbox problem
    By xyknight in forum New To Java
    Replies: 10
    Last Post: 04-09-2011, 05:40 PM
  2. JCheckbox in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 09-07-2010, 06:29 AM
  3. How can I add JCheckBox to each row?
    By batya in forum AWT / Swing
    Replies: 1
    Last Post: 11-04-2009, 10:25 PM
  4. Issue with Jcheckbox on JTableheader
    By ram_76uk in forum AWT / Swing
    Replies: 2
    Last Post: 07-30-2008, 09:24 AM
  5. jcheckbox issues need help. thanks.
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •