Kian Cross
Kian Cross

Reputation: 1818

How can I check if this check box is enabled from another method in Java?

What I am trying to do is check if a check box is enabled from another method that is also running on another thread. I am fairly new to Java so I apologise in advanced if my code isn't how Java is usually written (or if it is written badly).

So I've made a method that created a iframe then adds a check box to it. I've removed the part which creates the jframe just to keep my code minimal - you can see it below:

private void initialize() {
    chckbxEnabled.setHorizontalAlignment(SwingConstants.LEFT);
    chckbxEnabled.setForeground(Color.WHITE);
    chckbxEnabled.setBounds(98, 123, 81, 23);
    frame.getContentPane().add(chckbxEnabled);
}

I've then created a new method in a new thread and called it from another method which I haven't shown here.

static Thread thread = new Thread(new Runnable() {
public void getPing() throws IOException, InterruptedException {
    while (true) {
        System.out.println(chckbxEnabled.isEnabled());
        if(chckbxEnabled.isEnabled()) {] // Part I am having trouble with
            String apiKey = "exmapleApiKey";
            URL url = new URL("http://example.com/ping.php?mode=get&apikey="+apiKey);
            URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    yc.getInputStream()));
            String inputLine;
            inputLine = in.readLine();
            }
        Thread.sleep(1000);
    }
}
   public void run() {     
       try {
            getPing();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       }
    });
}

As you can see I am trying to access chckbxEnabled.isEnabled(). This worked because just after I made my main class I added private static JCheckBox chckbxEnabled = new JCheckBox("Enabled");. So I can access it but it always returns true when I print it even though the check box is sometimes checked.

So my question is what is a better way of doing this as I expect that way I have experimented with is 'messy' and not the way it should be done?

Upvotes: 1

Views: 314

Answers (1)

ted
ted

Reputation: 4975

You are mixing up methods:

isEnabled is for checking if the user can interact with the user (think greyed out elements)

isSelected is what you are looking for (check for the checkmark inside the box). You might want to take a look at ItemListener (example)

Also: keep in mind that you can draw to the api only form the gui thread.


Update: So: most gui frameworks allow only one thread to draw the gui, since synchronisation is very complex. Therefore most of swing is not thread safe. A good answer outlining this (actually related to swing), can be found here

Quote:

A note on thread safety: It may seem strange that such an important part of the Java platform is not thread safe. It turns out that any attempt to create a thread-safe GUI library faces some fundamental problems. For more on this issue, see the following entry in Graham Hamilton's blog: MultiThreaded toolkits: A failed dream?

Given that we have to draw the gui from a single thread, what does this mean? Update operations, i.e. changing a labels text should not be done from a thread you spawned, but should be done from the main thread that runs your gui code (in swing it is called event dispatch thread(in the swing trail). A tutorial on how to have worker threads that update the gui can be found i.e. here. the basic idea is to have a backgound thread do the calculations and return the result. There is SwingWorker to help with this. The gui thred will then update the gui.

A introduction to concurreny and swing is i.e. here

Upvotes: 6

Related Questions