2

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();
      }
    });
  }
}

enter image description here

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:

  1. Open application and click "Show Dialog" button.
  2. Now frame and dialog are visible. Then click the "Show Desktop" icon from task bar
  3. 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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Tamil
  • 1,193
  • 9
  • 24
  • That code does not compile unless `JFrame f = new JFrame("JFrame");` is instead `final JFrame f = new JFrame("JFrame");` .. – Andrew Thompson Jun 27 '16 at 14:46
  • BTW - the code works as expected here on Windows.. I note you explicitly mention Ubuntu, and it seems like it might be a problem specific to Ubuntu. If that is the case, it will take some hackery to fix.. – Andrew Thompson Jun 27 '16 at 14:49
  • Am using Java compiler V1.5 in my eclipse and i can run the application! – Tamil Jun 27 '16 at 14:51
  • That's more a problem with Eclipse. It should **not** be allowing programmers to run code that does not compile cleanly. Try it with a `final` in there. – Andrew Thompson Jun 27 '16 at 14:56
  • 1
    @AndrewThompson: he may be compiling it at a higher compiler setting since a local variable only needs to be "*effectively*" final in the latest versions of Java in order to work in inner classes and lambdas. – Hovercraft Full Of Eels Jun 27 '16 at 15:02
  • 1
    @HovercraftFullOfEels OK.. that did not occur to me. – Andrew Thompson Jun 27 '16 at 15:06
  • JDialog jdialog = new JDialog((Window) null, "JDialog"); This code solves my problem but i cannot locate the dialog center. Any ideas? – Tamil Jun 28 '16 at 07:15
  • 1
    Do you want to center the Dialog? If so, let me answer this by stea...quoting from Hovercraft Full Of Eels: http://stackoverflow.com/a/9543339/1368690 – hamena314 Jul 05 '16 at 09:25
  • Thanks @hamena314. If i have swap the pack() and setLocationRelativeTo() method, it works fine. – Tamil Jul 05 '16 at 11:29
  • Again i need to find a solution for JOptionPane.showOptionDialog for the same issue. – Tamil Jul 05 '16 at 11:38
  • What happens if you switch `setLocationRelativeTo()` and `pack()` again? – hamena314 Jul 06 '16 at 07:22
  • @hamena314: Dialog opens at the top of the screen if `pack() next to setLocationRelativeTo()` – Tamil Jul 06 '16 at 09:44

0 Answers0