I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
Asked
Active
Viewed 310 times
1
-
What is the underlying purpose for this? That information might give us better ideas on how to implement things. The reason I ask is that a JButton click and a MouseListener are two completely separate and somewhat unrelated things. Tying them together has some traps, and the way this could be done will depend on the needs. – Hovercraft Full Of Eels May 15 '15 at 21:11
-
I have a grid of buttons and the purpose is to drag your mouse and change the color of the buttons you hover over. I got is to work with MouseMoved listener but I cant get the mouse dragged to work because (i'm assuming) the first button never gets unclicked. – toxicswagger1 May 15 '15 at 21:14
-
You may want to use the glasspane for this, if you want to add a MouseListener "above" your JButtons. Then when set visible, the glasspane would steal the mouse inputs, and when no longer visible, the components on the GUI would receive the inputs. – Hovercraft Full Of Eels May 15 '15 at 21:45
1 Answers
1
What you observe is the typical behavior of the ButtonModel
used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI
delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI
and a custom ButtonModel
that uses isRollover()
to add buttons to your program's notion of a selection model.
As an alternative, consider JList
, which contains a ListSelectionModel
that allows MULTIPLE_INTERVAL_SELECTION
. A compete example is shown here.