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.
Related
So, first you selected it and it does the things I told it to do, and then you deselected it, wanting to start an event, but only when it first has been selected.
Add an ItemListener to the radio button.
Read the section from the Swing tutorial on How to Write an ItemListener for more information.
and then you deselected it, wanting to start an event, but only when it first has been selected.
Well, the only way you will get the unselected event is if it has been selected first.
I have two radio buttons, both added to a ButtonGroup. I have added an ActionListener for both. Suppose that at present the first radio button is selected, then again if I click on the same button then again actionPerformed() will be called. It doesn't look good. I want to prevent the call to actionPerformed() if that radio button is already selected.
One possible way could be to store current selected radio button state in a variable, but I want to know the java internal method for this.
Is there any method to do this?
if(radioButton.isSelected()) this will tell you if its selected. if it's selected you don't perform action. If it's not selected perform the action.
I have a ProgressMonitorDialog object which contains a cancel button. I want to disable the initial focus of the cancel button. The reason I want to remove the initial focus from the button is that a user may accidentally hit a key on the keyboard while a batch operation is in progress and cancel the whole operation. If a user wants to cancel an operation I would prefer that they press the tab key and manually set the focus, or click on the cancel button with a mouse.
From what I can tell, there is no easy way to do this. I can create another button on the ProgressMonitorDialog that does nothing and have that take focus, but that's an ugly workaround. Especially since the button has to be visible or the focus will shift to the cancel button. I have also tried overriding the method that creates the cancel button and bypassing the shell.setDefaultButton() method but no luck.
Any clues/suggestions?
If you are extending Dialog class, then you can do the following, override createButtonsForButtonBar(Composite) method, and pass in false as default button argument on the button which you don't want the initial focus on.
#Override
protected void createButtonsForButtonBar(Composite parent) {
// create OK and Cancel buttons by default
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
Here is a puzzler, every time I create a group of radio buttons in SWT/JFace, the first button is always true, but if create a standard button and call it to reset the radio button it will reset to being false.
I would like it to be set to false as default.
Has any encountered a problem like this and how to solve it?
Thanks in advance
When creating a group of radio buttons, exactly one from them HAS to be selected at a time. By selecting a different button, the first one becomes unselected. Therefor, if you set selected true to second radio button, the first will be unselected.
If you create a group of radio buttons, the first one is always selected by default. Use
secondRadioButton.setSelection(true);
to change the state...
My back button is overridden for ... reasons. I've implemented a new feature, the options menu, and I need unique function calls if the back button is pressed while this options menu is up versus when it is not up. How can I discern if the menu is up when the back button is pressed? Thanks guys!
Use a boolean. Set it to true in onPrepareOptionsMenu(). Set it to false in onOptionsMenuClosed().