Allow doClick() on buttons, but not from mouse

Hi all,

I have an odd situation:

I have a drawing tool with a bunch of JButtons. When users click a button, the button is selected, any currently selected button gets deselected (using the button's doClick() method). Also, certain events in my program use the button's doClick() method.

However, what I DON'T want is for users to UNCLICK a button by themselves. So if a user clicks an already selected button, I want nothing at all to happen.

I'm not sure how I could do this, however, as I don't even know where I'd catch the clicking event (since JButton handles that itself). I don't want to set the button to be disabled, because other events in the program ought to be able to access the JButton.

Any ideas would be greatly appreciated,

Thanks!

[799 byte] By [Asbestosa] at [2007-11-26 19:50:47]
# 1
Why don't you use JToggleButton and actionPerformed with a state check, either setting the toggle button again or setting and deselecting the currently selected one?And why don't yo uask in the Swing forum?
CeciNEstPasUnProgrammeura at 2007-7-9 22:40:21 > top of Java-index,Java Essentials,Java Programming...
# 2
You can remove the actionListener for that button inside actionPerformed method when the user clicks it so that any clicks would go unrecognized and later add the actionListener once again when you want the button to recognize the clicks.
qUesT_foR_knOwLeDgea at 2007-7-9 22:40:21 > top of Java-index,Java Essentials,Java Programming...
# 3

> Why don't you use JToggleButton and actionPerformed

> with a state check, either setting the toggle button

> again or setting and deselecting the currently

> selected one?

1 - I can't switch to JToggleButtons. I'm a fairly minor cog in a large project, and there's a lot of code already built around the JButtons as they are now.

2 - I'd prefer not to have the button unselect and then automatially reselect, as there are a number of events that get triggered on unselect which should not happen in this situation. I'd prefer nothing at all to happen when the user tries to unselect.

3 - If I had to, I guess I could add a whole bunch of setEnabled(true/false) for everything that uses doSelect(). This seems a little inelegant, though.

> And why don't yo uask in the Swing forum?

Because I figured this was more of an algorithm question than just a swing functioanlity question. If you think I should move it, I can.

Asbestosa at 2007-7-9 22:40:21 > top of Java-index,Java Essentials,Java Programming...
# 4

I suppose you could call it an algorithm.If clicked button != selected button

deselect selected button

select clicked buttonAlthough as far as I can see you don't even need the if-statement there. Are you asking because what you have now doesn't work, or have you not tried anything yet?

DrClapa at 2007-7-9 22:40:21 > top of Java-index,Java Essentials,Java Programming...
# 5

> I suppose you could call it an algorithm. If

> clicked button != selected button

My problem is that I can't see any way to differentiate between the clicked button and the selected button. When the user clicks a button, it's the JButton's event handler that deals with the selection (or something like that), and so the button gets selected before I can do anything.

For instance, if I add a little error in one of my button listeners, so I can get a stack trace from the clicked event, I get this stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero

at org.concord.graph.examples.GraphWindowToolBar$1.actionPerformed(GraphWindowToolBar.java:246)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:269)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)

at java.awt.Component.processMouseEvent(Component.java:5554)

... and so on up to the EventDispachThread. I don't understand at what point I could put in code such as "If clicked button != selected then select button, otherwise don't" because it seems as if the button is already selected before I get a chance to do that.

Asbestosa at 2007-7-9 22:40:21 > top of Java-index,Java Essentials,Java Programming...
# 6
When you select a button, store a reference to that button in an instance variable of the class. Then you have your own interpretation of "the selected button" apart from Swing's.
DrClapa at 2007-7-9 22:40:22 > top of Java-index,Java Essentials,Java Programming...
# 7

Right, and then I could select it again if I wanted to. But this wouldn't help me prevent the selection action happening in the first place, would it?

Automatically reselecting after the user deselects isn't viable, because I can't let all the events that normally fire when the button unselects fire.

Asbestosa at 2007-7-9 22:40:22 > top of Java-index,Java Essentials,Java Programming...
# 8

So leave the if statement in place. If the button that was clicked on is the same button as the one your variable claims is selected, do nothing. Otherwise deselect the one your variable claims is selected and change the variable to refer to the button that was clicked on. I can't see why this has to be so complicated.

DrClapa at 2007-7-9 22:40:22 > top of Java-index,Java Essentials,Java Programming...