Solved

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

Posted on 1998-05-18
2
309 Views
Last Modified: 2008-02-26
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
Comment
Question by:binkle
2 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 220 total points
ID: 1221173
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
 

Author Comment

by:binkle
ID: 1221174
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now