I have a modal settings dialog which is a JDialog. In this settings window I placed some components including a button to yet another modal settings dialog which is also a JDialog. I made them JDialogs because that is the only way I know of to make a modal dialog.
The problem is this: when I create the main settings dialog I have to construct the JDialog either without a parent Frame or with a parent Frame. Since my main window is a JFrame, I can just pass that to the main settings dialog constructor. But when I want to create the second modal settings dialog which should have the main settings dialog as a parent, I can't find a way to get the (J)Frame of the JDialog. I do want to pass that main settings dialog as a parent so that the second settings dialog centers on it when it is shown. Let's assume the second settings dialog has no constructor for passing a location, just the constructors of the JDialog.
Is there a way to get the (J)Frame of a JDialog?
Is there a design flaw in my setup and should I have used something else for these settings dialogs? (And if so, how should I make these alternative settings dialogs modal?)
Thank you for your help,
Erik
UPDATE:
Thank you all for your answers. They led me to understand that apparently it's not absolutely necessary to have an owner for a JDialog. I thought this was needed for the dialog to be able to disable the owner until the dialog is closed, but apparently the modality is independent of the owner. I also noticed that even with an owner the dialog still doesn't center on the owner, so now my code is like:
public class CustomDialog extends JDialog {
public CustomDialog(String title) {
setModal(true);
setResizable(false);
setTitle(title);
buildGUI();
}
public Result showDialog(Window parent) {
setLocationRelativeTo(parent);
setVisible(true);
return getResult();
}
}
This also allows for modal dialogs in modal dialogs.
Thanks for all your help!
Not sure what exactly you have as a problem, but here is an example on how you can have multiple modal dialogs:
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestDialog {
protected static void initUI() {
JPanel pane = newPane("Label in frame");
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
public static JPanel newPane(String labelText) {
JPanel pane = new JPanel(new BorderLayout());
pane.add(newLabel(labelText));
pane.add(newButton("Open dialog"), BorderLayout.SOUTH);
return pane;
}
private static JButton newButton(String label) {
final JButton button = new JButton(label);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Window parentWindow = SwingUtilities.windowForComponent(button);
JDialog dialog = new JDialog(parentWindow);
dialog.setLocationRelativeTo(button);
dialog.setModal(true);
dialog.add(newPane("Label in dialog"));
dialog.pack();
dialog.setVisible(true);
}
});
return button;
}
private static JLabel newLabel(String label) {
JLabel l = new JLabel(label);
l.setFont(l.getFont().deriveFont(24.0f));
return l;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initUI();
}
});
}
}
1.please read The New Modality API in Java SE 6
2.Is there a way to get the (J)Frame of a JDialog?
Window ancestor = SwingUtilities.getWindowAncestor(this);
or
Window ancestor = (Window) this.getTopLevelAncestor();
It is now 2021 and so about 8.5 years after the question was posted, but back in 2012 and still in these days by far and away the easiest solution is to simply create the JDialog with a Dialog as owner. Java has 5 constructors with a Dialog owner to offer, next to 5 similar ones with a Frame owner and 5 with a Window owner.
The issue that the OP was probably faced with was the default template for a JDialog in Netbeans or some other IDE. Netbeans (12.2) offers this as a template for a new JDialog Form...:
public class NewJDialog extends javax.swing.JDialog {
/**
* Creates new form NewJDialog
*/
public NewJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
You can simply change the signature of the constructor to use a Dialog as owner instead of the default option of a Frame:
public class NewJDialog extends javax.swing.JDialog {
/**
* Creates new form NewJDialog
*/
public NewJDialog(java.awt.Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
}
And away you go.
Related
As for testing-reasons I tried to open a JDialog window with the panel and its contents of the main application frame. As I already had anything in there I wanted to see if I could simply set the JDialogs contentPane to the one I passed over. So in simplyfied form this came together:
testsforSO.java :
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class testsforSO extends JFrame {
private static final long serialVersionUID = -3890178393751567629L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testsforSO frame = new testsforSO();
frame.setSize(300, 300);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public testsforSO() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("I am the Frame");
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setLayout(null);
JButton btnRestart = new JButton("Restart");
btnRestart.setBounds(10, 10, 50, 50);
btnRestart.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
testsforSO.main(null);
dispose();
}
});
panel.add(btnRestart);
getContentPane().add(panel);
repaint();
// -----------DELETE These comments-------------
// JDialog myDialg = new JDialog(this);
// myDialg.setContentPane(panel);
// myDialg.setVisible(true);
// myDialg.setSize(300,300);
// myDialg.setLocation(new Point(250, 250));
// myDialg.setTitle("I am Dialog from within the script");
myDialog.main(panel);
}
}
and myDialog.java :
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JDialog;
public class myDialog extends JDialog {
private static final long serialVersionUID = 7079322237622743228L;
public static void main(Container myContainer) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
myDialog frame = new myDialog(myContainer);
frame.setVisible(true);
frame.setContentPane(myContainer);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public myDialog(Container myContainer) {
setContentPane(myContainer);
getContentPane().setLayout(null);
setBounds(200,200,200,200);
}
}
When starting the main frame I assumed it would contain the restarting button as well as the dialog does. But interestingly it was only the dialog with the button.
However when hitting it, the main frame properly restarted, a second dialog was set up and it contained the button again. This time however the main frame had the button as well, just without any function. Clicking on it does absolutely nothing.
Then I tried further and checked if that behaviour would change if I added the dialog directly into the main applications code (see the commented lines) and, starting the application once again only the dialog in its own class showed the button. Even hitting this one now restarted properly but the button won't show up on any other window except the lonely declared dialog.
What am I missing here and how could I refactor my code to work properly if even at all in this manner?
Understand that Swing components can only be present in one container, and while you may see the visual residue of a component in a container, the actual component is only present in the last container added to.
Myself, if I wanted dialog and jframe to have the same content pane components, I'd create a factory method to create the contentPane, and then use it to create two unique but identical contentPanes.
Also, I'd be remiss if I didn't mention something about your use of null layouts. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For instance, when I ran your code, this is the dialog that I saw:
You hard coded the button's size to be too small to show its text on my platform. If you had used layout managers, and called pack() on the top-level window, the button would show appropriately.
So this is probably a silly question but I just cant figure it out.
Basically I have my MainWindow, when I press a button a new window should appear named PartSelectionWindow that contains a JTable (the JTable is filled out with a custom table model). Then I select a row and press ok to go back to the MainWindow and work with the info selected. Here's the problem:
If I use a JFrame for the PartSelectionWindow, after the window is closed the code execution does not continue in the MainWindow button event
If i use a JDialog, the Jtable is not filled out with the custom model and the button for it does not respond, it can only be closed by clicking on the X of the JDialog. It's as if it was disabled. When debugging I noticed it DOES try to fill out the table AFTER closing the JDialog. I read somewhere that this may be because the code is paused after the setVisible is set to true, so I tried putting the code to fill out the JTable BEFORE the setVisible is set to true but it still does not work.
Obligatory code:
//MainWindow button event when tryng to use new JFrame
private void btnSetupListActionPerformed(java.awt.event.ActionEvent evt) {
//call to new JFrame
new partsWindow();
//after closing partsWindow the next line of code does NOT execute
txtPartNum.setText(PartsWindow.jpart.getPartNumber());
}
//MainWindow button event when tryng to use new JDialog
private void btnSetupListActionPerformed(java.awt.event.ActionEvent evt) {
//call to new JDialog
new partsDialog(this, true);
//after closing partsWindow the next line of code does NOT execute
txtPartNum.setText(PartsWindow.jpart.getPartNumber());
}
Constructor for JDialog window
public partsDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setTitle("Setup Material Client");
this.setVisible(true);
this.setLocationRelativeTo(null);
jTable1.getTableHeader().addMouseListener(new partsDialog.ColumnFitAdapter());
jTable1.getTableHeader().setReorderingAllowed(false);
GetBomForSetupMaterial = jtrace.GetBomForSetupMaterial(Main.station);
jTable1.setModel(new PartModel(GetBomForSetupMaterial.getPartPositionList()));
}
Any help would be appreciated.
To ilustrate my comment you can use code like this (simplified)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JDialogTest extends JDialog {
private static final long serialVersionUID = 1L;
private String text;
private JTextField textField;
public JDialogTest(JFrame owner, String text){
super(owner,true);
this.text = text;
init();
}
private void init() {
this.getContentPane().setLayout(new BorderLayout());
JButton btnContinue = new JButton("Close me and continue");
btnContinue.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JDialogTest.this.dispose();
}
});
textField = new JTextField();
this.getContentPane().add(new JLabel(text),BorderLayout.NORTH);
this.getContentPane().add(textField,BorderLayout.CENTER);
this.getContentPane().add(btnContinue,BorderLayout.SOUTH);
this.pack();
}
public JTextField getTextField() {
return textField;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
JDialogTest test = new JDialogTest(frame, "Hello");
System.out.println("I open");
test.setLocationRelativeTo(null);
test.setVisible(true);
System.out.println("Good you closed it value is " + test.getTextField().getText());
frame.setVisible(false);
}
}
I just passed some text to end up in JLabel you should pass your table or info on how to create it
For the second window, do frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) or something like that. I'm on my phone so i can't get the exact code. This line of code will make it so the frame will be terminated, not the program
In your main window make it a JFrame. When it is constructed create your dialog in there but set its visible to false. The dialog should be modal = false. When you set this as false the main window will wait until the JDialog is no longer visible to continue its code execution:
Public class MainWindow extends JFrame {
protected JDialog = yourDialog;
public MainWindow {
yourDialog = new JDialog(this, false);
//* rest of your construction *//
}
private void yourButtonPressedActionPreformed(java.awt.eventActionEvent evt) {
yourDialog.setVisible(true);
//* The code halted here until you have set the visibility of that dialog to false.
Make the dialog do that to itself when the button is pressed *//
foobar = yourDialog.getYourData();
}
Setting the JDialog modal to false will cease the code operation in your main window until it is done but since you have only set its visibility to false you can still grab data from it as you need.
I have a frame, on this frame I have a Menu with About MenuItem. When we select it the program opens a new JPanel with texts and with OK button and the enabled status of parent panel is set to false.
And now comes a problem. When we click on OK, then I want to close this About panel, and I want to turn to parent panel, and I want to enable it!
Please tell me, how?
Consider using a WindowListener that reacts to the closing event of the about-dialog. You can add this in your frame or in the constructor of your dialog, just set the variables accordingly.
myDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
parentFrame.setEnabled(true);
}
});
If you really only have a switching JPanel, use a ButtonListener.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
frame.setEnabled(true);
}
});
As mentioned in the comments, using a modal JDialog would be a more elegant way of solving the problem of disabling a parent frame while a dialog is active. Here is a tutorial.
Why don't you use simply a JOptionPane (particularly the showMessageDialog method)? You can specify there an Object (for example a JPanel) which will be presented in a modal dialog. Take a look at this sample code I've written for you (I've used a JButton, but it will be the same for JMenuItem):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class AboutDialogDemo extends JFrame {
private final JButton btnAbout = new JButton("About...");
public AboutDialogDemo() {
final JFrame thisFrame = this;
btnAbout.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(thisFrame, new AboutPanel());
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(btnAbout, BorderLayout.PAGE_END);
pack();
}
public static void main(String[] args) {
AboutDialogDemo frame = new AboutDialogDemo();
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class AboutPanel extends JPanel {
private final JLabel lblAbout = new JLabel("Sample about text");
public AboutPanel() {
setLayout(new BorderLayout());
add(lblAbout, BorderLayout.PAGE_START);
}
}
I hope you'll find it useful
I have two Jframes where frame1 has some text fields and when a button on frame1 is clicked, I open another JFrame which contains a search box and a JTable containing search results.
When I click on a result row on JTable, I want that particular values to be reflected in the frame1 text fields.
I tried passing the JFrame1's object as a parameter but I have no clear idea on how to achieve this.
Any help would be highly appreciated.
Thanks
First of all, your program design seems a bit off, as if you are using a JFrame for one of your windows where you should in fact be using a JDialog since it sounds as if one window should be dependent upon the other.
But regardless, you pass references of GUI objects the same as you would standard non-GUI Java code. If one window opens the other (the second often being the dialog), then the first window usually already holds a reference to the second window and can call methods off of it. The key often is when to have the first window call the second's methods to get its state. If the second is a modal dialog, then the when is easy -- immediately after the dialog returns which will be in the code immediately after you set the second dialog visible. If it is not a modal dialog, then you probably want to use a listener of some sort to know when to extract the information.
Having said this, the details will all depend on your program structure, and you'll need to tell us more about this if you want more specific help.
For a simple example that has one window open another, allows the user to enter text into the dialog windows JTextField, and then places the text in the first window's JTextField, please have a look at this:
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowCommunication {
private static void createAndShowUI() {
JFrame frame = new JFrame("WindowCommunication");
frame.getContentPane().add(new MyFramePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// let's be sure to start Swing on the Swing event thread
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyFramePanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton openDialogeBtn = new JButton("Open Dialog");
// here my main gui has a reference to the JDialog and to the
// MyDialogPanel which is displayed in the JDialog
private MyDialogPanel dialogPanel = new MyDialogPanel();
private JDialog dialog;
public MyFramePanel() {
openDialogeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openTableAction();
}
});
field.setEditable(false);
field.setFocusable(false);
add(field);
add(openDialogeBtn);
}
private void openTableAction() {
// lazy creation of the JDialog
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
dialog = new JDialog(win, "My Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
}
dialog.setVisible(true); // here the modal dialog takes over
// this line starts *after* the modal dialog has been disposed
// **** here's the key where I get the String from JTextField in the GUI held
// by the JDialog and put it into this GUI's JTextField.
field.setText(dialogPanel.getFieldText());
}
}
class MyDialogPanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton okButton = new JButton("OK");
public MyDialogPanel() {
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okButtonAction();
}
});
add(field);
add(okButton);
}
// to allow outside classes to get the text held by the JTextField
public String getFieldText() {
return field.getText();
}
// This button's action is simply to dispose of the JDialog.
private void okButtonAction() {
// win is here the JDialog that holds this JPanel, but it could be a JFrame or
// any other top-level container that is holding this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose();
}
}
}
You'd do a very similar technique to get information out of a JTable.
And again, if this information doesn't help you, then please tell us more about your program including showing us some of your code. The best code to show is a small compilable example, an SSCCE similar to what I've posted above.
I originally developed the following code on Win XP. When you click the program icon in the XP taskbar, the parent frame remains iconified and the JDialog returns to focus, which is the behavior I want. However, when clicking the program's taskbar icon on Win 7, the parent JFrame changes its state back to Normal and shows behind the app-modal JDialog. I've tried overriding the JFrame's setExtendedState() method to intercept the frame's state change with no luck.
Is there a workaround for this, or is there a flaw my logic I need to address?
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class TestLogin extends JFrame {
public TestLogin() {
this.setSize(300, 300);
iconify(this);
setLocationRelativeTo(null);
this.setTitle("I'm a Frame!");
this.setVisible(true);
LoginScreen login = new LoginScreen(this);
}
public static void main(String [] args) {
TestLogin frame = new TestLogin();
}
public static void iconify(Frame frame) {
int state = frame.getExtendedState();
// Set the iconified bit
state |= Frame.ICONIFIED;
// Iconify the frame
frame.setExtendedState(state);
}
public static void deiconify(Frame frame) {
int state = frame.getExtendedState();
// Clear the iconified bit
state &= ~Frame.ICONIFIED;
// Deiconify the frame
frame.setExtendedState(state);
}
public class LoginScreen extends JDialog {
private JFrame root;
public LoginScreen(JFrame root) {
super(root);
this.root = root;
setLocationRelativeTo(null);
this.setTitle("I'm a Dialog!");
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
this.setSize(200, 200);
setVisible(true);
}
}
}
It looks like a bug in the java paradigm of "write once, run anywhere". If that is to include windows 7, then you might contact oracle and fill a bug report.
Regards,
Stéphane