I have a bug with modal dialogs I'm running into with Java 8u151/8u152 that wasn't an issue in 8u112, and I'm not sure if I'm doing something inherently wrong, or if this is a JRE bug. This only affects Mac OS as far as I can tell (I'm running OS 10.12.6), so although my question isn't Mac-specific, you won't be able to observe this bug if you're not a Mac user.
I have a modal dialog created with the main JFrame as its parent. I then create a new modal dialog that I create from dialog #1. Is it wrong to set dialog #2's parent to the main JFrame as well? Or must it be dialog #2?
Asked another way: If both dialogs share the same parent (the main JFrame), but dialog #2 was created last, is it reasonable to expect that dialog #2 should be on top, or is the behaviour undefined?
With 8u112, dialog #2 was focused and on top. With 8u151, dialog #2 is focused but behind. If I click on either dialog, dialog #2 grabs focus and seems to behave appropriately for the most part. However, sometimes dialog #2 will remain on top of other applications (I believe this happens if I click on the main window, then back on the modal dialog... this part is definitely a bug).
Note: for Java 8u121 through (I think) 8u144, there were additional problems with modal dialogs that were supposed to have been fixed in 8u152 (I logged a bug report and checked an early access build several months ago, and things really were fixed back then).
Some sample code to illustrate what I'm doing:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
MyPanel panel;
public Main() {
setTitle("This is a frame");
setSize(300, 200);
panel = new MyPanel(this);
add(panel);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Main frame = new Main();
frame.pack();
frame.setVisible(true);
});
}
private static class MyPanel extends JPanel {
int dialogCounter = 1;
final JFrame theParent;
public MyPanel(JFrame parent) {
super();
theParent = parent;
setPreferredSize(new Dimension(300, 200));
JButton button = new JButton("Press the button");
button.addActionListener(e -> showDialog(theParent));
add(button);
}
private void showDialog(Frame parent) {
JDialog dialog = new JDialog(parent, "This is dialog " + dialogCounter, true);
setupDialog(dialog);
}
private void setupDialog(JDialog dialog) {
JPanel dialogPanel = new JPanel();
dialogPanel.setPreferredSize(new Dimension(300, 200));
dialogPanel.add(new JLabel("Current dialog count: " + dialogCounter++));
JButton button = new JButton("Open a new modal dialog");
button.addActionListener(e -> showDialog(theParent));
dialogPanel.add(button);
dialog.add(dialogPanel);
dialog.pack();
dialog.setVisible(true);
}
}
}
This is a bug introduced in 8u151. It has already been fixed and should be backported to the next OpenJDK 8 update release.
Below is a workaround you could use:
dialog.addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
dialog.removeWindowListener(this);
dialog.toFront();
}
});
Related
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 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'm following through a book called "The JFC Swing Tutorial (Second Edition)" and I'm pretty much at the start I have followed this code and it should be displaying the button and the label in the content pane, but All im getting is a blank screen. any ideas?
Thanks.
import java.awt.GridLayout;
import javax.swing.*;
public class m extends JFrame
{
void UserFrame()
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hellow You");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new GridLayout(0,1));
//makes label
JLabel label = new JLabel("Sup ");
//adds to the frames content pane a label
frame.getContentPane().add(label);
JButton button = new JButton("Hai");
frame.getContentPane().add(button);
jp.add(button);
jp.add(label);
jp.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
//pack set the window to what it needs AKA to display all components
frame.pack();
//frame.setSize(250, 250);
//shows window
frame.setVisible(true);
}
public static void main(String[] args)
{
final m window = new m();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
window.UserFrame();
}
});
}
}
Simply add
frame.add(jp);
just before
frame.pack();
What's happening here? You correctly add all your widgets to a JPane, but you basically threw that JPane away and didn't use it anywhere.
This will be sufficient just to get it to work properly.
If you want to do it correctly, you should also remove frame.getContentPane().add(label); and frame.getContentPane().add(button); (Thank you #dic19 for noting that!). These will not work the way you used it.
My app has a JWindow that needs to be minimized when the custom minimizer button clicked.
Please reply if anyone knows how to minimize a JWindow. I have searched a lot but couldn't find any suitable method to minimize.
I know how to minimize a JFrame. So please don't bother answering regarding JFrame.
Thanks.
I know you don't want to hear this, but the terrible truth is that there is no big difference between undecorated jframes (with setstate methods) and jwindows... :)
JFrame f = new JFrame("Frame");
f.setUndecorated(true);
Due to the fact that a JWindow is not decorated with any control icons, no setState method is provided. One workaround is to allow your custom minimizer button to set the window visible as required:
public class JWindowTest extends JFrame {
JWindow window = new JWindow();
JButton maxMinButton = new JButton("Minimize Window");
public JWindowTest() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maxMinButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (window.isVisible()) {
maxMinButton.setText("Restore Window");
} else {
maxMinButton.setText("Minimize Window");
}
window.setVisible(!window.isVisible());
}
});
add(maxMinButton);
window.setBounds(30, 30, 300, 220);
window.setLocationRelativeTo(this);
window.add(new JLabel("Test JWindow", JLabel.CENTER));
window.setVisible(true);
}
public static void main(String[] args) {
new JWindowTest().setVisible(true);
}
}
I am writing an application in Java (I will admit I am a novice Java programmer, but experienced in others) and have a need to be able to set X11 window properties explicitly. Specifically, I need to be able to set the _NET_WM_STATE property to let the window manager know a child window is modal.
The definition of this property can be seen here:
http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#id2578152
This application has to be written in Java and will be running solely on a Linux/X11 platform so portability does not even have to be considered. When running on a standard Ubuntu PC the modality of the window is properly handled.
The window manager running on the platform where this application is expected to run does things above and beyond normal modality and expects the _NET_WM_STATE window property to be set.
From my research it seems that Java does not want to set these properties. I am guessing that this is so that it can handle the modality within the Java framework so that it will be have the same across platforms.
I have used xwininfo and xprop to see what kinds of things Java sets on child windows when they are modal vs non-modal. It seems there is basically nothing in the X11 window properties that is different between the two types of windows.
Here is some sample code of how the child window is created:
public class ChildDialog extends JDialog {
static final long serialVersionUID = 2;
public ChildDialog(JFrame frame, String title, boolean modal) {
super(frame, title, modal);
JPanel dgrid = new JPanel();
dgrid.setLayout(new BoxLayout(dgrid, BoxLayout.Y_AXIS));
dgrid.setBorder(BorderFactory.createLineBorder(Color.red, 3));
JLabel label = new JLabel(title);
JTextArea desc = new JTextArea("This is a child window.");
desc.setBackground(label.getBackground());
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
dgrid.add(label);
dgrid.add(desc);
dgrid.add(close);
this.setSize(300, 200);
this.add(dgrid);
this.setVisible(true);
}
}
This class is then instantiated when a button on the main window is clicked. Like this:
JButton spawnChild = new JButton("Child Window");
spawnChild.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChildDialog(getSelf(), "Child Window", false);
}
});
bgrid.add(spawnChild);
JButton spawnModalChild = new JButton("Child Window (Modal)");
spawnModalChild.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChildDialog(getSelf(), "Child Window (Modal)", true);
}
});
bgrid.add(spawnModalChild);
Any ideas?