Jump to content

GUI checkbox wont disable other GUI options


Mushphang

Recommended Posts

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

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

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 by Mushphang
Link to comment
Share on other sites

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

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

  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 by Satire
Link to comment
Share on other sites

  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

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:

 

 

01a.jpg

Link to comment
Share on other sites

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:

 

 

01a.jpg

I guess that's a lesson learned for the future :) 

Glad you got it working.

Link to comment
Share on other sites

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 by Imateamcape
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...