Solved

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

Posted on 1998-05-18
2
308 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
HashMap Vs TreeMap 12 47
unix in java example 9 37
JUnit 4 @Before and @BeforeClass differences 3 19
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

771 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

11 Experts available now in Live!

Get 1:1 Help Now