[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

How can I get my Modal JDialog to work?????

I've written an app using swing. The main view is based on a JFrame and uses modal JDialog's to collect detailed info from the user along the way. However my modal JDialogs are not working!

What tends to happen (not always though - if you're gentle with the dialog!(serious)) is that the dialog loses all it's components and is incapable of receiving input when moved or one of the comonents clicked, pressed or whatever. This does not happen if a dialog is non-modal, but I need modal.

Below is source code for a simple test that illustrates the problem. If you compile and run the class below with no command line arguments ("java JDialogTest") a frame and associated non-modal dialog are created - you can interact with either till the cows come home.

If you run the class with an argument (eg "java JDialogTest modal") the dialog will be modal and will respond to a limited amount of interaction before freezing.

Does anybody know what I need to do in order to keep my modal JDialog alive?

I've considered using the various JOptionPane methods to create the modal dialog, however the dialogs in my app are quite complicated and I don't see how I can make them with one of these methods.

import com.sun.java.swing.*;
import java.awt.*;
import com.sun.java.swing.JButton;
import com.sun.java.swing.JLabel;

public class JDialogTest extends com.sun.java.swing.JDialog
{
      protected int count=0;
      public JDialogTest(Frame parent, boolean modal)
      {
            super(parent, modal);
            //{{INIT_CONTROLS
            setTitle("JDialogTest");
            getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
            setVisible(false);
            setSize(430,270);
            jButton1 = new com.sun.java.swing.JButton();
            jButton1.setText("Hello");
            jButton1.setBounds(177,5,63,25);
            jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
            jButton1.setForeground(new Color(0));
            jButton1.setBackground(new Color(-3355444));
            getContentPane().add(jButton1);
            jLabel1 = new com.sun.java.swing.JLabel();
            jLabel1.setText("0");
            jLabel1.setHorizontalAlignment(com.sun.java.swing.SwingConstants.CENTER);
            jLabel1.setBounds(245,10,7,15);
            jLabel1.setFont(new Font("Dialog", Font.BOLD, 12));
            jLabel1.setForeground(new Color(-10066279));
            jLabel1.setBackground(new Color(-3355444));
            getContentPane().add(jLabel1);
            //}}

            //{{REGISTER_LISTENERS
            SymWindow aSymWindow = new SymWindow();
            this.addWindowListener(aSymWindow);
            SymMouse aSymMouse = new SymMouse();
            jButton1.addMouseListener(aSymMouse);
            //}}
      }

      public void addNotify()
      {
            // Record the size of the window prior to calling parents addNotify.
            Dimension d = getSize();

            super.addNotify();

            if (fComponentsAdjusted)
                  return;

            // Adjust components according to the insets
            setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
            Component components[] = getContentPane().getComponents();
            for (int i = 0; i < components.length; i++)
            {
                  Point p = components[i].getLocation();
                  p.translate(insets().left, insets().top);
                  components[i].setLocation(p);
            }
            fComponentsAdjusted = true;
      }

      // Used for addNotify check.
      boolean fComponentsAdjusted = false;


      public synchronized void show()
      {
            Rectangle bounds = getParent().bounds();
            Rectangle abounds = bounds();

            move(bounds.x + (bounds.width - abounds.width)/ 2,
                  bounds.y + (bounds.height - abounds.height)/2);
            super.show();
      }

      public void setVisible(boolean b)
      {
            if(b)
            {
                  Rectangle bounds = getParent().getBounds();
                  Rectangle abounds = getBounds();
                  setLocation(bounds.x + (bounds.width - abounds.width) /2 ,
                        bounds.y + (bounds.height - abounds.height) / 2);
            }
      super.setVisible(b);
      }

      class SymWindow extends java.awt.event.WindowAdapter
      {
            public void windowClosing(java.awt.event.WindowEvent event)
            {
                  Object object = event.getSource();
                  if (object == JDialogTest.this)
                        JDialogTest_WindowClosing(event);
            }
      }

      void JDialogTest_WindowClosing(java.awt.event.WindowEvent event)
      {
            dispose();
      }
      //{{DECLARE_CONTROLS
      com.sun.java.swing.JButton jButton1;
      com.sun.java.swing.JLabel jLabel1;
      //}}


      class SymMouse extends java.awt.event.MouseAdapter
      {
            public void mouseReleased(java.awt.event.MouseEvent event)
            {
                  Object object = event.getSource();
                  if (object == jButton1)
                        jButton1_mouseReleased(event);
            }
      }

      void jButton1_mouseReleased(java.awt.event.MouseEvent event)
      {
            ++count;
            jLabel1.setText(""+count);
      }
      public static void main(String [] args){
            JFrame f=new JFrame();
            f.setBounds(200, 200, 200, 200);
            f.show();
            JDialogTest t;
            if(args.length==0){
                  t=new JDialogTest(f, false);
            } else t=new JDialogTest(f, true);
            t.pack();
            t.show();
      }
}
0
binkle
Asked:
binkle
1 Solution
 
evijayCommented:
Hi Binkle,

Please use setVisible instead of show() in the above program since it is deprecated (the problem is setVisible does some priliminary processing and invokes show(). See the source code of JDialog for more info).

Dont override show(). You need not do this since you are not using it any more !! you will be using setVisible instead.

Here is working program ....



    import com.sun.java.swing.*;
    import java.awt.*;
    import com.sun.java.swing.JButton;
    import com.sun.java.swing.JLabel;

    public class JDialogTest extends com.sun.java.swing.JDialog
    {
    protected int count=0;
    public JDialogTest(Frame parent, boolean modal)
    {
    super(parent, modal);
    //{{INIT_CONTROLS
    setTitle("JDialogTest");
    getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
    setVisible(false);
    setSize(430,270);
    jButton1 = new com.sun.java.swing.JButton();
    jButton1.setText("Hello");
    jButton1.setBounds(177,5,63,25);
    jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
    jButton1.setForeground(new Color(0));
    jButton1.setBackground(new Color(-3355444));
    getContentPane().add(jButton1);
    jLabel1 = new com.sun.java.swing.JLabel();
    jLabel1.setText("0");
    jLabel1.setHorizontalAlignment(com.sun.java.swing.SwingConstants.CENTER);
    jLabel1.setBounds(245,10,7,15);
    jLabel1.setFont(new Font("Dialog", Font.BOLD, 12));
    jLabel1.setForeground(new Color(-10066279));
    jLabel1.setBackground(new Color(-3355444));
    getContentPane().add(jLabel1);
    //}}

    //{{REGISTER_LISTENERS
    SymWindow aSymWindow = new SymWindow();
    this.addWindowListener(aSymWindow);
    SymMouse aSymMouse = new SymMouse();
    jButton1.addMouseListener(aSymMouse);
    //}}
    }

    public void addNotify()
    {
    // Record the size of the window prior to calling parents addNotify.
    Dimension d = getSize();

    super.addNotify();

    if (fComponentsAdjusted)
    return;

    // Adjust components according to the insets
    setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
    Component components[] = getContentPane().getComponents();
    for (int i = 0; i < components.length; i++)
    {
    Point p = components[i].getLocation();
    p.translate(insets().left, insets().top);
    components[i].setLocation(p);
    }
    fComponentsAdjusted = true;
    }

    // Used for addNotify check.
    boolean fComponentsAdjusted = false;


    public void setVisible(boolean b)
    {
    if(b)
    {
    Rectangle bounds = getParent().getBounds();
    Rectangle abounds = getBounds();
    setLocation(bounds.x + (bounds.width - abounds.width) /2 ,
    bounds.y + (bounds.height - abounds.height) / 2);
    }
    super.setVisible(b);
    }

    class SymWindow extends java.awt.event.WindowAdapter
    {
    public void windowClosing(java.awt.event.WindowEvent event)
    {
    Object object = event.getSource();
    if (object == JDialogTest.this)
    JDialogTest_WindowClosing(event);
    }
    }

    void JDialogTest_WindowClosing(java.awt.event.WindowEvent event)
    {
    dispose();
    }
    //{{DECLARE_CONTROLS
    com.sun.java.swing.JButton jButton1;
    com.sun.java.swing.JLabel jLabel1;
    //}}


    class SymMouse extends java.awt.event.MouseAdapter
    {
    public void mouseReleased(java.awt.event.MouseEvent event)
    {
    Object object = event.getSource();
    if (object == jButton1)
    jButton1_mouseReleased(event);
    }
    }

    void jButton1_mouseReleased(java.awt.event.MouseEvent event)
    {
    ++count;
    jLabel1.setText(""+count);
    }
    public static void main(String [] args){
    JFrame f=new JFrame();
    f.setBounds(200, 200, 200, 200);
    f.setVisible(true);
    JDialogTest t;
    if(args.length==0){
    t=new JDialogTest(f, false);
    } else t=new JDialogTest(f, true);
    t.pack();
    t.setVisible(true);
    }
    }


0
 
binkleAuthor Commented:
Thanks evijay.
I am eternally grateful for your solution and am kicking myself for the hours spent trying to work this out myself. I thought that show() was new to java1.1, is it really deprecated in swing? If so I still haven't been able to find any docs telling me so - maybe I'll look a bit harder.

Thanks heaps.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now