How to close a Java SWT window with a button? - java

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();
}
}
}

Related

Java Swing Dialog Window focus

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.

Close platform after closing a pop-up | JavaFX

So I have 2 stages, one is the main stage and the other is a pop-up screen. When the pop-up screen shows up you can close it by pressing the 'x' at the upper-left (or upper-right depending on your OS). Is there a way to close the main stage whenever you close the pop-up screen?
Stage and Popup inherit an onHidden property from Window. This is a handler that is invoked immediately after the window is hidden (by any mechanism). You can call Platform.exit() in the handler in order to exit the application:
popup.setOnHidden(event -> Platform.exit());
Note that Platform.exit() is generally preferred to System.exit(0): calling System.exit(...) will not allow the Application's stop() method to be called, so you may bypass any resource clean-up your application is performing.
There have a Event named setOnCloseRequest. If you are opening an Alert pop-up window.
Alert popup = new Alert(AlertType.INFORMATION);
Then your solution is:
alert.setOnCloseRequest(new EventHandler<DialogEvent>()
{
#Override
public void handle(DialogEvent t)
{
System.exit(0);
}
});
Else if you want to close another window with it's owner, then just use it's stage and replace DialogEvent with WindowEvent.

How do I change the focus on buttons of JFace WizardPage?

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);
}
});

Dialog box that disappear automatically

I am developing a application. I want when I exit it, a message box appears having message, for example, "Thanks for using Soft Attendance". Then disappear automatically say after few seconds.
I have write code for this as following:
public void windowClosing(WindowEvent e){
int whichOption;
whichOption=JOptionPane.showConfirmDialog(f1,"Are you Serious?","Soft Attendence",JOptionPane.YES_NO_OPTION);
if(whichOption==JOptionPane.YES_OPTION){
f1.dispose();
JOptionPane.showMessageDialog(null,"Thanks for using Soft Attendence");
}
}
When i click on exit button a confirmation dialog box appear and after clicking yes button
application is exited and a another message box appears.
Now I have two questions in my mind :
First question is I have dispose application already and then message box is shown. Is it fine to show message box after its parent is killed?
When second message box appears I don't want to click on "OK" button. I want it should automatically disappear. So my second question is How to shown message box that disappear automatically after some time?
Is it fine to show message box after its parent is killed?
I think this would not be a ideal case. But you can open dialog if the parentComponent has no Frame JOptionPane.showConfirmDialog(null,"...");
How to shown message box that disappear automatically after some time?
Auto disposable you can get it by a trick, you can create a SwingWorker which normally performs GUI-interaction tasks in a background thread. Set a timer and which will execute after time period and close your dialog explicitly.
class AutoDisposer extends SwingWorker<Boolean, Object> {
#Override
public String doInBackground() {
try{
JOptionPane.getRootFrame().dispose();
return Boolean.True;
}catch(Exception ex){...}
return Boolean.False;
}
}
...
new Timer(seconds * 1000, autoDisposer).start();

JOptionPane won't show its dialog on top of other windows

I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
Old post, but I was struggling with this.
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
Firstly the old JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
Windows is blocking this operation since XP.
The scenario before was like:
Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
The come to front call in java is only working for java windows.
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
Correction the post above..
I have resolve my problem as below:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
You might think about using a JFrame instead. It may give you a little more flexibility.
If you are using a JFrame and you want it to popup on top of the other windows use:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.

Categories

Resources