I want to create two or more JButton that share state, that is, when the mouse button is pressed on JButton , both of them appear as pressed (or "armed"), or if they are flags, both are checked / not checked at the same time, etc. .
For the user, it should look as if both buttons were the same button appearing in more than one place in the hierarchy (in fact, Swing does not allow this.)
I can get half way by creating one ButtonModel and assigning the same model to both buttons. This synchronizes their status with enabled / checked / selected, etc.
However, one noticeable effect that is not shared between the buttons is focus - clicking on one button gives the button focus (indicated by the box inside the button) and removes it from the other button. I would like to display both buttons as if they were focused whenever the button really has focus.
Is there a clean way to do this?
Ideally, I would like it to not depend on the selected appearance.
Edit: I discovered another issue with ButtonModel . When one of the buttons loses focus, it sets the properties of the armed and pressed models to false . This happens after processing mousePressed , so if you press the second button when the first button has focus, it will not enter the pressed state until you press it a second time.
finnw source share