I've got a JDialog that's created and set to visible whenever the button is clicked.
My problem is that the button keeps the focus and doesn't give it to the JDialog.
Is this a normal behaviour or there's something wrong going on ?
JDialogs aren't modal (are "modeless") by default:
Creates a modeless dialog without a title and without a specified Frame owner.
Try constructing it as:
new JDialog(owner, title, ModalityType.APPLICATION_MODAL);
(Or the equivalent super() call if you're subclassing JDialog. Or whichever modality type you want.)
Try using dialog.requestFocus() if dialog is the newly created JDialog.
See requestFocus() or requestFocusInWindow() for more information.
Related
When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it
i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required.
and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!
This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.
JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.",
"EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);
It sounds like you want to make it a JOptionPane.PLAIN_MESSAGE. That is what you need to put instead of whatever int2 is. I was going to link you to the tutorial but Space Pope already did that. You don't need to create a custom dialog to change the default icon, just change the message type to a plain message. The tutorial covers all this stuff.
You'll need to keep a reference to the dialog if you want to close it yourself. See the Oracle tutorial on custom Dialogs. The constructor you're using also puts in an icon by default; if you make your own dialog, you can control that part too.
JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:
JOptionPane optionPane = (JOptionPane)
SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
optionPane.setValue(JOptionPane.CLOSED_OPTION);
If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:
Window window = (Window) button.getTopLevelAncestor();
window.dispose();
We have an application which, as its first UI action, displays a modal JDialog without a parent frame.
public LoginDialog(Frame owner, Config config, Object... params) {
super((Frame)null, true);
It unfortunately has the annoying characteristic that when it appears, although it comes to the front, it does not grab the focus.
So the user, after launching the application by double-clicking on the start menu, has to use the mouse to select the "login" dialog and type in information.
Is there a way to make this JDialog grab the focus when it appears in the front?
I've tried calls to "requestFocus" before, after and via invokeLater "during" the call to setVisible(true) - but none of these seems to have any effect.
How do we make a modal dialog grab the focus?
UPDATE: The issue was the code used to try to present a background "wait window". This window was displayed "behind" the login dialog as a hack so that when the dialog disappeared the user would see the "Please wait" message. As it was the first window created by the application, it got the focus. I am not sure if there would have been a way to make the dialog gain the focus again inside the event dispatch thread with this hack - but I fixed it by un-hacking it and doing things properly.
First, it a little strange that modal dialog is parent-less. The point in modal dialog is that it is displayed on its parent and does not allow to access parent.
So, the first recommendation is to make it non-modal. I believe it will work.
BTW I have just tried to create such dialog and have not problems with focus. Try probably to simplify your code:
JDialog d = new JDialog();
d.setSize(200, 200);
d.setVisible(true);
This works for me and I believe will work for you. Now start changing this simple code towords your real application code. At some point it will stop working and you will see where the problem is.
If nothing helps try to use the trick I described in this article. Look for title "Portable window activation". I hope it will help.
See Dialog Focus for a potential fix using a RequestFocusListener. I have used it successfully for setting focus in JOptionPane dialogs.
1) you have to create JDialog contents and showing container wrapped inside invokeLater()
or best and safiest way is
2) you have to set for ModalityTypes or Modal for parent
3) only one from containers could be Modal in applications lifecycle
Does exist in the JDialog class a way to prevent that a child window (JDialog) would be displayed more than once when the button from the main window (JFrame) used to open it, is pressed several times? Many thanks in advance!
Yes, and you don't need to make the box modal to do it (although making it modal would be the easiest way).
Simply do something like the following
In your member delcarations:
private final MyDialog dialog = new MyDialog();
In your code:
private void showDialog() {
dialog.setVisible(true);
dialog.requestFocus(); // May be needed to bring window to front
}
This will ensure that you only instantiate the box once. Simply call showDialog() whenever the button is pressed.
Another way that I've done in the past with Swing is that when the button is pressed the first thing I do is disable the button. Then I use the observable pattern to look at the child window and re-enable the button when the child window is closed. That way if it takes a while to display the child window for some reason the user can't click on it multiple times and mess things up.
You could make the JDialog modal, then the parent window would not react until it is closed.
Or you could initialize the JDialog before, and just make it visible when your button is pressed. Making it visible twice will not display it twice.