Mushphang Posted December 26, 2016 Share Posted December 26, 2016 Sadly have spent about 2 hours trying to get a check box in my gui to disable other options. Basically what I need it to do is, If checkbox is enabled, enable the spinner. Here's the code I have: chckbxAutoGE.addItemListener(new ItemListener() { @[member=Override] public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ spinnerHides.setEnabled(true); } if(e.getStateChange() == ItemEvent.DESELECTED){ spinnerHides.setEnabled(false); } } }); any help is highly appreciated. Link to comment Share on other sites More sharing options...
Butters Posted December 26, 2016 Share Posted December 26, 2016 Maybe you're going the right way, but what I did is I used ActionListener instead of a ItemListener and everything worked fine private void muleCheckActionPerformed(java.awt.event.ActionEvent evt) { if (muleCheck.isSelected()) { fisherCheck1.setSelected(false); fisherPanel.setEnabled(false); mulePanel.setEnabled(true); for (java.awt.Component comp : mulePanel.getComponents()) { comp.setEnabled(true); } for (java.awt.Component comp : fisherPanel.getComponents()) { comp.setEnabled(false); } } } This disables/enables one of the two JPanels and all of it's components depending on which checkbox is selected. Code is for one checkBox only Link to comment Share on other sites More sharing options...
Mushphang Posted December 26, 2016 Author Share Posted December 26, 2016 (edited) Maybe you're going the right way, but what I did is I used ActionListener instead of a ItemListener and everything worked fine private void muleCheckActionPerformed(java.awt.event.ActionEvent evt) { if (muleCheck.isSelected()) { fisherCheck1.setSelected(false); fisherPanel.setEnabled(false); mulePanel.setEnabled(true); for (java.awt.Component comp : mulePanel.getComponents()) { comp.setEnabled(true); } for (java.awt.Component comp : fisherPanel.getComponents()) { comp.setEnabled(false); } } } This disables/enables one of the two JPanels and all of it's components depending on which checkbox is selected. Code is for one checkBox only Thanks for your quick response. I'm still new to this whole GUI thing so I'm easily confused here. This is my whole GUI class. http://pastebin.com/ji1gyg6N and what it looks like: http://puu.sh/t1mZK/f11021eade.png It's probably setup odd (first gui) but has worked well for me up until this point. Edited December 26, 2016 by Mushphang Link to comment Share on other sites More sharing options...
Butters Posted December 26, 2016 Share Posted December 26, 2016 Thanks for your quick response. I'm still new to this whole GUI thing so I'm easily confused here. This is my whole GUI class. http://pastebin.com/ji1gyg6N and what it looks like: http://puu.sh/t1mZK/f11021eade.png It's probably setup odd (first gui) but has worked well for me up until this point. /*chckbxAutoGE.addActionListener(e -> { if (chckbxAutoGE.isSelected()) { spinnerHides.setEnabled(true); } else { spinnerHides.setEnabled(false); } });*/ Why is this part commented out? Should work Link to comment Share on other sites More sharing options...
Mushphang Posted December 26, 2016 Author Share Posted December 26, 2016 /*chckbxAutoGE.addActionListener(e -> { if (chckbxAutoGE.isSelected()) { spinnerHides.setEnabled(true); } else { spinnerHides.setEnabled(false); } });*/ Why is this part commented out? Should work Doesn't work for some reason Tried multiple things in the 2 + hours I spent on this. Link to comment Share on other sites More sharing options...
Hel Posted December 26, 2016 Share Posted December 26, 2016 (edited) Didn't read your code mb lmaoEdit: after actually reading your code, could you not just do something like this? chckbxAutoGE.addActionListener(e -> spinnerHides.setEnabled(!spinnerHides.isEnabled())); Edited December 26, 2016 by Hydra Link to comment Share on other sites More sharing options...
FrostBug Posted December 26, 2016 Share Posted December 26, 2016 Works fine when I run your code. Make sure you don't have an old cached version of your Jar interfering somewhere Link to comment Share on other sites More sharing options...
Mushphang Posted December 26, 2016 Author Share Posted December 26, 2016 Didn't read your code mb lmao Edit: after actually reading your code, could you not just do something like this? chckbxAutoGE.addActionListener(e -> spinnerHides.setEnabled(!spinnerHides.isEnabled())); Haha all good, and still no luck. Could it be possible it has something to do with the main start button action listener? Does it matter what order the listeners are in? Works fine when I run your code. Make sure you don't have an old cached version of your Jar interfering somewhere O lord, checking now. Works fine when I run your code. Make sure you don't have an old cached version of your Jar interfering somewhere I can't seem to find anything.. What do you specifically mean? Link to comment Share on other sites More sharing options...
Satire Posted December 26, 2016 Share Posted December 26, 2016 (edited) chckbxAutoGE.addItemListener(new ItemListener() { @[member='Override'] public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ spinnerHides.setEnabled = true; }else{ spinnerHides.setEnabled = false; } } }); This should work. Literally from my project, just renamed to your scenario. This should work, else you will need to see what you've done wrong. There is something in your project that is disallowing this to work. If the first condition isn't met, then it go false. Edited December 26, 2016 by Satire Link to comment Share on other sites More sharing options...
Mushphang Posted December 26, 2016 Author Share Posted December 26, 2016 chckbxAutoGE.addItemListener(new ItemListener() { @[member='Override'] public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ spinnerHides.setEnabled = true; }else{ spinnerHides.setEnabled = false; } } }); This should work. Literally from my project, just renamed to your scenario. This should work, else you will need to see what you've done wrong. There is something in your project that is disallowing this to work. If the first condition isn't met, then it go false. Tried this and no luck again, made a fresh class file on another script. This is the code and still doesn't disable the other checkbox import javax.swing.JCheckBox; import javax.swing.JFrame; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class SettingsGUI { public static void Window(main m) { JFrame jFrame = new JFrame("Curtidor"); jFrame.setSize(261, 419); jFrame.setResizable(false); jFrame.getContentPane().setLayout(null); JCheckBox chckbxNewCheckBox = new JCheckBox("New check box"); chckbxNewCheckBox.setBounds(40, 75, 97, 23); jFrame.getContentPane().add(chckbxNewCheckBox); JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New check box"); chckbxNewCheckBox_1.setBounds(40, 146, 97, 23); jFrame.getContentPane().add(chckbxNewCheckBox_1); jFrame.setVisible(true); chckbxNewCheckBox.addItemListener(new ItemListener() { @[member=Override] public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { chckbxNewCheckBox_1.setEnabled(true); } else { chckbxNewCheckBox_1.setEnabled(false); } } }); } } Link to comment Share on other sites More sharing options...
Mushphang Posted December 26, 2016 Author Share Posted December 26, 2016 Well everyone.. I've spent hours tonight on this. Problem is solved. I literally was right click testing the gui in eclipse on the design preview. It didn't come across my mind (still being new to java) that apparently the action listeners don't do anything on the preview.. After officially testing on osbot it works just find :^). Me: Link to comment Share on other sites More sharing options...
Hel Posted December 26, 2016 Share Posted December 26, 2016 This makes me both chronically depressed & very happy that you found the answer Link to comment Share on other sites More sharing options...
Satire Posted December 26, 2016 Share Posted December 26, 2016 Well everyone.. I've spent hours tonight on this. Problem is solved. I literally was right click testing the gui in eclipse on the design preview. It didn't come across my mind (still being new to java) that apparently the action listeners don't do anything on the preview.. After officially testing on osbot it works just find :^). Me: I guess that's a lesson learned for the future Glad you got it working. Link to comment Share on other sites More sharing options...
Team Cape Posted December 27, 2016 Share Posted December 27, 2016 (edited) Sadly have spent about 2 hours trying to get a check box in my gui to disable other options. Basically what I need it to do is, If checkbox is enabled, enable the spinner. Here's the code I have: chckbxAutoGE.addItemListener(new ItemListener() { @[member='Override'] public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ spinnerHides.setEnabled(true); } if(e.getStateChange() == ItemEvent.DESELECTED){ spinnerHides.setEnabled(false); } } }); any help is highly appreciated. Edit: my bad, didn't realize this was solved Edited December 27, 2016 by Imateamcape Link to comment Share on other sites More sharing options...