I have implemented a JFace Wizard with 2 WizardPages.
By default, the first WizardPage has these 4 Buttons:
Back (disabled)
Next (focused)
Cancel
Finish (disabled)
Now I want to set the default focus on the Cancel Button. How do I do that?
Removing the focus and setting it to some Control of the page's content would also be ok.
I tried setting the focus to a Button in the content layout of the WizardPage, but this only sets me a second focus on the immediateButton. The focus on the Next button is still there, and the Next button reacts to pressing enter, which is what I want to avoid.
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
immediateButton.setFocus();
}
}
How can I access the Dialog buttons and change their focus?
The Next button does not actually have focus, rather it is the Shell Default button.
The logic in WizardDialog makes either Next or Finish the default button and there does not seem to be a way to change this.
You may be able to override this by calling getShell().setDefaultButton(button) in your wizard page.
Update: Testing this you can do it in setVisible but you need to use Display.asyncExec to make the code run at the right time:
final Shell shell = getShell();
shell.getDisplay().asyncExec(() -> shell.setDefaultButton(immediateButton));
above is for Java 8, for Java 7 or earlier:
shell.getDisplay().asyncExec(new Runnable()
{
#Override
public void run()
{
shell.setDefaultButton(immediateButton);
}
});
Related
Context: JDK 8 & JavaFX
I have a TextField control that is used in a dialog. It is the first edit control, so it gets the focus when the dialog opens. The dialog has a button configured as the cancel button (Button.setCancelButton(true))
With a plain TextField, if I hit ESC immediately after the dialog opens, the dialog is closed (as expected).
However, once I add a TextFormatter with input filter to the TextField, the ESC keypress appears to be being consumed by the input control and ESC no longer closes the dialog.
The TextFormatter only has an input filter (to restrict the input control to just digits), but the input filter does not get invoked on the ESC keypress - because the content of the field has not changed.
It's a fairly minor issue, but it's annoying not having consistent behaviour, and not being able to just hit ESC to dismiss the dialog. Any ideas on how to ensure that the ESC keypress is propagated/not consumed, so that it is handled by the dialog?
Edit:
My question appears to be a duplicate of this one: Escape from a Number TextField in a JavaFX dialog. Which of course I failed to find despite trawling through Google before posting... TLDR; the TextFormatter class fails to forward the ESC keypress event on.
I think the easiest approach is to avoid trying to “fix” the TextField and TextFormatter, and just add a key listener:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
dialog.setResult(ButtonType.CANCEL);
}
});
If the Dialog is not an Alert (or more precisely, is not a Dialog<ButtonType>), you can locate the button and activate it yourself:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
Button cancelButton = (Button)
dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancelButton.fire();
}
});
In my application I have a main window and a utility popup dialog that is shown when the user clicks on a menu item. My issue is that if another program (say firefox) is opened over the java application, this obviously hides the java application. This is OK - but when the user then clicks on my java application again, only the main window is shown - the utility popup dialog is still hidden under firefox. I would like to design it such that when the user interacts with the main window in any way the utility popup dialog is also brought to the front.
I've tried adding a MouseInputListener to the main frame to bring the utility dialog to the front but this also transfers focus to it, which I don't want.
private MouseInputAdapter onWindowClick = new MouseInputAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (scheduleDialog != null)
scheduleDialog.toFront(); // the utility dialog
}
};
the utility popup dialog is still hidden
When the dialog is created you need to specify the main window as the owner of the dialog.
Then when you click on the icon for the window, the dialog will display as well.
Read the JDialog API for the proper constructor to use.
I have 4 simple buttons that interact with a listview. If you click a certain button you can go one down, one up, to the start and to the end of the listview. So each button has a different onAction method:
(Buttons: previous, next, end, start)
public void toNext(){
list.getSelectionModel().selectNext();
}
public void toPrevious(){
list.getSelectionModel().selectPrevious();
...
But now I want to disable the buttons if they can't go to the start, end, up or down. I tried to do the following by adding this code to the method toPrevious (example):
previous.setDisable(list.getSelectionModel().getSelectedIndex() == 0)
This code does disable the button but it won't enable it when you can go to the previous string in the listview. Does anyone has a simple solution for this?
You can use a binding:
previous.disableProperty().bind(
list.getSelectionModel().selectedIndexProperty().isEqualTo(0));
next.disableProperty().bind(
list.getSelectionModel().selectedIndexProperty().isEqualTo(
Bindings.size(list.getItems()).subtract(1)));
When I'm setting the button to be disabled using this:
jButton.setEnabled(false);
then there is this visual effect visible on the second element ->
How can I disable the button, but keep the the look of the first element?
how to get rid of visual effect when disabling buttons
Companies spend millions of dollars to develop a UI can is common and can be used by all users.
How is the user suppose to know that the button is disabled if there is no visual indication?
Anyway, (rant finished) you can manually set the disabled icon:
button.setDisabledIcon( button.getIcon());
If you also happen to have text on the button the text will still be disabled so instead of a disabled icon you can use a custom ButtonModel:
button.setModel( new DefaultButtonModel()
{
#Override
public boolean isArmed()
{
return false;
}
#Override
public boolean isPressed()
{
return false;
}
});
As a newbie to Java, and many years of iOS and .NET experience, I found this to be massively confusing. What I want I thought would be very simple - I want a dialog (called from a main window) with OK and Cancel buttons. When you click OK, it does something, then dismisses the dialog. When you click Cancel, it just dismisses the dialog.
However, doing this using the SWT shell Dialog class is not obvious. How do you get a button to dismiss the dialog, and return execution back to the main Window?
Use Shell.close() rather than dispose() - so shlCheckOut.close().
Shell.close sends the SWT.Close event and then calls dispose.
With some trial-and-error and a lot of fruitless searching, I found in your button code, you have to call the .dispose() method of the dialog's shell variable. For example, my dialog is CheckOutDialog, thus I named the shell variable shlCheckOut. In the createContents() method, I put the button code as such:
...
Button btnCancel = new Button(shlCheckOut, SWT.NONE);
btnCancel.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shlCheckOut.dispose();
}
}
}