I have a Swing based Java application that needs to show a JDialog
:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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 JDialogHide {
public void buildGUI() {
final JFrame f = new JFrame("JFrame");
// f.setResizable(false);
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("SHow Dialog");
p.add(btn,new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JDialog jdialog = new JDialog(f, "JDialog");
jdialog.setPreferredSize(new Dimension(300,200));
jdialog.add(new JLabel("Test"));
jdialog.setLocationRelativeTo(f);
jdialog.pack();
jdialog.setVisible(true);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new JDialogHide().buildGUI();
}
});
}
}
My problem is that the dialog automatically hides when the user clicks "Show Desktop" and again if they click the frame in task bar, the dialog doesn't show up.
i.e.,
Steps:
- Open application and click "Show Dialog" button.
- Now frame and dialog are visible. Then click the "Show Desktop" icon from task bar
- Now if we select the application via task bar, the dialog doesn't show up.
I am using Ubuntu 14.04 LTS.
This solves my problem for jdialog but I cannot locate the dialog center:
JDialog jdialog = new JDialog((Window) null, "JDialog");
I can swap pack()
and setLocationRelativeTo()
, now the dialog is located at the center of the frame.
But the same problem occurs on JOptionPane.
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JDialogHide {
public void buildGUI() {
final JFrame f = new JFrame("JFrame");
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("SHow Dialog");
p.add(btn,new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JDialog jdialog = new JDialog((Window) null, "JDialog");
jdialog.setPreferredSize(new Dimension(300,200));
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("Show showOptionDialog");
p.add(btn,new GridBagConstraints());
jdialog.getContentPane().add(p);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showOptionDialog(jdialog, "Test ?", "TEST ", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
}
});
jdialog.pack();
jdialog.setLocationRelativeTo(null);
jdialog.setVisible(true);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new JDialogHide().buildGUI();
}
});
}
}
How can I fix it for JOptionPane?