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.
Related
I am trying to set the default close operation in NetBeans 8.0.2 (in Ubuntu 14.04 on an older Asus gaming laptop.) My program is very large but uses no JFrame or java.swing components.
I merely need to save some values when the "x" in the lower right corner is clicked (this is one usual way to stop execution of a java program in NetBeans.)
I found suggestions that involved swing & JFrame, but it wasn't clear just where to insert the code:
DefaultApplicationView view = new DefaultApplicationView(this);
javax.swing.JFrame frame = view.getFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.out.println("CLOSING");
}
}
show(view);
I also found a set of instructions that I think I would prefer to use, but the post is old enough that my NetBeans doesn't have the tabs/menu-items referred to:
Set Window to Design Mode by clicking the 'Design' Tab
In the Navigator: Right click the 'JFrame' -> 'Properties'
In the Properties Tab: Set 'defaultCloseOperation' (top of the list) to 'DO_NOTHING'
Select 'Events' Tab
Scroll down to 'windowClosing'
Click on the "..." button on the right of the event to bring up the custom editor
Click 'Add...' and name the handler (i.e. custom function that you want to have execute on click of the 'X', or window close event).
Click 'Ok'
Netbeans now automatically creates the function and takes to you the function body in the source view
Now simply add what you want to do here: eg. dispose(), or system.exit or pintln(), or whatever your heart desires, as long as its JAVA and makes sense to the app.
Then there are a few other possibly relevant posts, but they all explicitly involve JFrame and/or swing. (Am I ignorant of some fact such as "All NetBeans java applications use JFrame", or some such?)
A pared down example of code for what I'm trying to do would be:
public class MyApp{
public static void main(String[] args){
loadMyVariables();
// do some work that changes variables' values
// during this work user clicks the 'x' box to halt execution
// I need then automatically to save the variables' new values
}
// needs to be called by the OS or GUI when execution is halted by user
public static void saveMyVariables{
// here the usual printStream stuff saves some values to a file
System.exit(0);
}
public static void loadMyVariables{
// here the usual Scanner stuff reads some values from a file
}
}
(I need help setting the tags for this, so I'm doing as instructed and asking the community.)
THANKS
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.
What object type (eg. JFace, Shell, ApplicationWindow) should I use to create my own window?
In my plugin, this window will have three 'panels', lots of components and what' s most important I want it to be on top, till it's closed - like any other window in Eclipse, so that user can' t do anything else in IDE till he close the window.
The window will be launched after clicking the icon in Eclipse's menu.
If you mean something like the Eclipse 'Find/Replace' dialog which does not block the main window use a JFace Dialog (org.eclipse.jface.dialogs.Dialog).
Set the dialog to be modeless and turn off 'block on open'.
This is what the Find/Replace dialog uses:
public FindReplaceDialog(Shell parentShell) {
super(parentShell);
// .... other code ...
setShellStyle(getShellStyle() ^ SWT.APPLICATION_MODAL | SWT.MODELESS);
setBlockOnOpen(false);
}
Add a command following tutorial at : http://www.vogella.com/tutorials/EclipseCommands/article.html
Create a handler for the command that opens a swt widget Dialog that is modal. ( use SWT.APPLICATION_MODAL ) populate the dialog with composites that you need.
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();
}
}
}
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.