开发者

Enabling/disabling an AWT Button

开发者 https://www.devze.com 2022-12-11 22:18 出处:网络
I wrote the following piece of code, which is supposed to return a panel with one checkbox and one button. The idea is that the button should be enabled only 开发者_Python百科if the checkbox is checke

I wrote the following piece of code, which is supposed to return a panel with one checkbox and one button. The idea is that the button should be enabled only 开发者_Python百科if the checkbox is checked. It works, meaning that if the checkbox is not checked, and I try to push the button, nothing happens. However, the visual appearance of the button is wrong, it appears as disabled when it should appear as enabled, and vice-versa. Any idea what's wrong with my code ? Thanks a lot in advance

public Panel createCalibrationPanel(final ImagePlus imp) {
    final Panel panel = new Panel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.add(Box.createHorizontalStrut(20));
    final Checkbox checkbox = new Checkbox(
            "Use image spatial calibration for q scale", true);
    final Button button = new Button("Set scale");
    useCalibration = checkbox.getState();
    button.setEnabled(checkbox.getState());
    panel.add(checkbox);
    panel.add(button);
    checkbox.addItemListener(new ItemListener() {
        public void itemStateChanged(final ItemEvent e) {
            boolean state = checkbox.getState();
            setUseCalibration(state);
            button.setEnabled(state);
        }
    });
    button.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            imp.unlock();
            IJ.run(imp, "Set Scale...", "");
            imp.lock();
        }
    });
    return panel;
}


The logic controlling the button is correct. I modified your sample code and it works as you described. (This is the version I used: http://pastebin.com/f6cd6cfac, tested on Sun Java 6).

But, there are some other methods that you call, but which you haven't shown us: setUseCalibration, imp.unlock, and IJ.run. Are you sure they are returning correctly? You should remove the external calls from code, and add them back in one at time until you find which one(s) is causing the problem.


I was pretty sure you're doing everything correctly, and my test bore this out. I removed the image reference and the action code, and the button behaves as intended.

I'm running JDK 6 from Sun on Ubuntu Linux. But that shouldn't make any difference.

I wonder if imp.lock() is doing something heinous. If this is the kind of lock associated with synchronization and concurrent processing, it looks intuitively wrong, because you're keeping the poor imp locked up for most of the program's lifetime. In any case, you could have a look at what happens when you comment out the action code.


I can't reproduce the problem either using Sun Java 6 on 64 bit OpenSuse 11.0. However, this doesn't mean that the code is correct. It just happens to work for me on my machine, OS and JRE today.

From your description it sounded like button.setEnabled(state) wasn't updating the appearance of the button, so I wondered if adding a button.repaint() directly after the call would make it work? This is more of an experiment than a solution, as even if it works it doesn't explain why your original code runs for me and not for you.

Either way I suspect one of:

  • Something in the way that the code is called.
  • Something in the library code.
  • A bug in the JRE (if it's a Sun JRE you can search the bugs database).
  • A threading problem (make sure that the AWT code runs on the EventDispatchThread and synchronize on mutable data members that are shared between threads).
0

精彩评论

暂无评论...
验证码 换一张
取 消