0

I have 2 button and they are the same model. I want to mouse hover a button, another button is highlight. It 's OK. But, when I click a button, the listener of 2 buttons is active. How to active listener of the button that is clicked ?

Jbutton A, B; 
A.addActionListener(lnA);
B.addActionListener(lnB);
ButtonModel bm = previewButton.getModel();
arrow.setModel(bm);
Phạm Quốc Bảo
  • 864
  • 2
  • 10
  • 19
  • 2
    Don't use the same model – Hovercraft Full Of Eels Jun 11 '15 at 02:50
  • No, I want to still use model..@@ – Phạm Quốc Bảo Jun 11 '15 at 02:55
  • Then check the source in the ActionEvent parameter and base response depending on the source. – Hovercraft Full Of Eels Jun 11 '15 at 02:56
  • 1
    The problem is, as Hovercraft has pointed out, when the model is triggered, then both buttons fire since they rely on the same state as each other. The listeners are actually registered with the model, not the buttons – MadProgrammer Jun 11 '15 at 03:02
  • 1
    `I want to mouse hover a button, another button is highlight.` - then use a MouseListener, not the same model. – camickr Jun 11 '15 at 03:12
  • @camickr : How to create background that look like when user hover a normal button. – Phạm Quốc Bảo Jun 11 '15 at 08:18
  • 1
    Possible duplicate [here](http://stackoverflow.com/q/30722745/230513) or [here](http://stackoverflow.com/q/10861852/230513). What's the [real problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378)? – trashgod Jun 11 '15 at 08:57
  • @PhạmQuốcBảo, you invoke the `setRollover(...)` method of the `ButtonModel` for the button. I still don't understand your real question. Why do you want to change the rollover of two buttons when the mouse is over one button? – camickr Jun 11 '15 at 15:25
  • @camickr : when use serRollover(..) and setbackgroud(..). But I can not get color as well as shadow, hightlight... look like style of a normal button on swing – Phạm Quốc Bảo Jun 12 '15 at 08:18

0 Answers0