Difference between isArmed,isPressed and isSelected - java

What is the difference between the following methods :
jButton.getModel().isArmed()
jButton.getModel().isSelected()
jButton.getModel().isPressed()
I do not understand what the documentation says for isArmed and the rest two have an obvious documentation. But I do not how do they behave differently.

isArmed means:
When the user presses the mouse button down on the JButton , but hasn't yet released it, the JButton is armed. However the armed state does'nt mean that action is going to be triggered for sure , because The user may release the button while the cursor is over the JButton, or the user may move the cursor elsewhere and release.Hence isArmed returns true if the JButton is armed, else it return false.

The documentation for ButtonModel explains the difference:
Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button).
As for isSelected:
isSelected() - Indicates if the button has been selected. Only needed for certain types of buttons - such as radio buttons and check boxes.

Related

JustTouched works on windows?

I'm using justTouched for manipulate easly touches and released in android, cause isTouched method acumulates many touches; but it works as the same in windows? Meaning, one event even when keep pressed? Or do I need another method/invoker/listener?
On desktop builds, Libgdx treats mouse button presses as touches. justTouched acts exactly the same, except that it polls mouse buttons instead of screen taps. And just like how on mobile you can't tell which finger just touched the screen, you can't tell which mouse button was just pressed. If you need to know which mouse button or finger touched down, you need to use an InputProcessor, which gives you far more information than using the Gdx.input convenience methods.
If you don't care which mouse button was just pressed, all you need is:
if (Gdx.input.justTouched()){
//...
}
Based on your comments under your question, you seem to be trying to distinguish which button just touched with || Gdx.input.isButtonPressed(Input.Buttons.LEFT)) which will return true on every frame as long as the left button is held down. And if instead you did && Gdx.input.isButtonPressed(Input.Buttons.LEFT)), then you wouldn't be sure that it's the left button that was just pressed. (Maybe you're holding down the left button and just pressed the right button.) There is no easy way to distinguish which button was pressed unless you are using an InputProcessor.

How to "Unclick" a JButton without releasing the left mouse button?

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?
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.

Un-Select JToggleButton

I have a JToggleButton, not in a group, and if it's pressed, I want to be able to Un-Select
it if I press another JButton.
I've tried using:
toggleButton.setSelected(false);
toggleButton.doClick();
but neither un-Select the toggle button, it stays "highlighted".
What can I do so that the toggle button goes back to the normal
un-Selected state, like if I pressed it again?
Is it a matter of calling the above while in the UI Thread?
jToggleButton.doClick(): Programmatically perform a "click". This does the same thing as if the user had pressed and released the button.
jToggleButton1.setSelected(false);
jToggleButton1.doClick();
If you execute this code subsequently, it is actually doing nothing. Because, as soon as the first line set the jToggleButton1 as unselected second line set it as selected. If you want just jToggleButton to be unselected use jToggleButton1.setSelected(false) by removing doClick(). But if you want to toggle among selected and deselected using your other JButton click, use jToggleButton1.doClick() only.

Setting label to visible in an action listener

I have a JFrame with a button on it. When the user presses the button, a time consuming series of actions are performed. I have a JLabel on the form that says "Please wait" that I want to become visible while the actions are being performed and then go invisible when they are completed. So, I put label.setVisible(true) at the beginning of the action listener and label.setVisible(false) at the end, but nothing happens.
It seems like the displaying of the label was being queued until the actions were finished, so it just goes visible and then immediately invisible. How can I make sure the label becomes visible before continuing with the rest of the code in the action listener?
Use a SwingWorker. When the button is clicked call execute. Update the JLabel when done.

Test if a javax.swing.JButton is pressed down

I would like to check whether a certain javax.swing.JButton (regular push-button) is pressed down (before it was released). Is there any option at all to check whether a button is down?
The most trivial solution is to add a MouseListener that will respond to the mouse click and release events. But, this does not cover the case where the button was activated by the Enter key, or any other way. I don't want to disable activating the mouse by keyboards or other ways - I just want to know when is it pressed down without restricting it's behaviour.
I tried listening to all the different events, and the only two that do respond to button press are the ActionPreformed (ActionEvent) and the StateChanged (ChangedEvent) events. ActionPreformed is executed once per click, meaning only after the button was pressed and released, so it's not good. StateChanged is indeed invoked several times when I click a button, and several times when I release it. But, the event object only includes information about the source widget (the button) and no information about the state change itself. This prevents from distiguishing which of the events we want to catch.
Thanks in advance!
ButtonModel can do that, more here or here or maybe off-topic JMenuItem & ChangeListener by #kleopatra

Categories