[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 1998-05-18
2
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 880 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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