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);
}
Related
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.
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.
I have a custom AlertDialog with a bunch of checkboxes. I want to prevent the AlertDialog from closing if none of the checkboxes are selected. Is this possible?
I know I can close momentarily and re-open it, but I would rather not do this as I have some code within the setPositiveButton which I do not want to repeat.
Thanks.
I want to prevent the AlertDialog from closing
Already Posted Answer on SO
OR
Create Custom Dialog to solve the issue.
I'm assuming you're using a custom view, so disable the OK/Cancel buttons at the bottom. Next you'll need to disable the back button:
#Override
public void onBackPressed() {}
The only thing left is to make sure your dialog fills the screen. If the user clicks beside the box on the parent view the dialog will close.
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().
In my application, on some screen, I launch a popup. Depends on what button the user will click on this popup, another popup has to be launched. I use JDialog object to implement these popups. The issue is that the second popup doesn't show up (even though is use setVisible(true) and toFront()). It's created but I can't see it. I defined in its constructor the first popup as its owner. Anyone can help?
When a JDialog is opened from a parent window or dialog, and set to be modal, the Event Dispatch Thread for the parent window is halted. This prevents the parent from being focused or from passing other events, or essentially doing anything until the modal dialog is closed. The call is therefore blocking.
What you must do instead is fire your event from somewhere else, like the new dialog instead of the parent window, OR instead of using modal dialogs, use a regular JFrame and set it to be always on top using setAlwaysOnTop(true). That means the user can continue to use the parent window, and events will still fire from it.
Addendum: in response to your problem "the program concentrates in showing it and doesn't react to the event that has to hide it": when you make a dialog modal, as soon as you make it visible, it will block the parent window until it is closed, including event firing. If you need the new popup to be closed programatically, you either need to make the popup non-modal, or you need to execute the subsequent code in the context of the new popup window (such as firing an event when it becomes visible)
OK, now I managed to show the second popup. The code in the event that triggers the popup is:
printingWindow.setLocationRelativeTo(null);
printingWindow.toFront();
printingWindow.setModal(true);
printingWindow.pack();
printingWindow.setVisible(true);
But now I have a different problem:
when the printingWindow is set to be visible, the program concentrates in showing it and doesn't react to the event that has to hide it.
The code that is executed when the appropriate event is fired is:
printingWindow.setVisible(false);
printingWindow.dispose();
So how I close this popup (by firing the event)?