Solved

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

Posted on 1998-05-18
2
311 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project error 5 55
Apps blocked by Java 9 79
How do I remove an object from a 3 25
Is Applet the way to go for my drag and drop system? 8 14
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

773 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