Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4399174

JLabel not showing Text in JDialog created from a JDialog

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs



      Name: boT120536 Date: 12/18/2000


      java version "1.3.0"
      java(TM)2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      Resubmit ID: 112336

      reproduce the problem by compliling the code attached and running the applet
      from the html file.

      summary of problem: non-modal JDialog loses it's internal component (in this
      case JLabel) when it is called from a class outside of the Applet.

      Code samples to illustrate problem:

      runApplet.html -- fill in your code base param
      <HTML>
      <HEAD>
      <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
      <HTML>
      <TITLE>
      HTML Test Page
      </TITLE>
      </HEAD>

      <BODY>
      TestApplet will appear below in a Java enabled browser.<BR>
      <APPLET code="test/TestApplet"
       name = "TestApplet"
       codebase='file:XXXXXXX' width=800 height=300>
      </APPLET>
      </BODY>
      </HTML>

      TestApplet.java
      package test;

      import javax.swing.*;
      import javax.swing.border.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;
      import java.util.*;
      import java.net.*;
      import java.applet.*;

      /**
       * Test Applet for debugging JDialog issues
       * This is the main applet it builds child
       * dialogs and controls when to hide and show them
       * <P>
       * @author JJL
       * @date August 2000
       */
      public class TestApplet extends JApplet implements ActionListener {

        //Launcher GUI buttons and search box
        protected JButton _button;
        protected JDialog _childDialog = null;

        protected JPanel _pane = null;
        protected JFrame _frame = null; //parent frame for child dialogs

        //we need this reference to display processing message
        protected ProcessDialog _pd = new ProcessDialog();

        //base URL used by DataFlow and for building images
        protected URL _codeBase = null;
        protected AppletContext _appletContext = null;

        /**
         * Called on initialization.
         */
        public void init() {

          setAppletContext();
          setBaseURL();

          //call the process dialog from the init method -- it works beautifully!
          _pd = new ProcessDialog();
          _pd.showDialog("Called from TestApplet.init method");

          try {
            Thread.sleep(6000);
          } catch (InterruptedException ie) {
            System.out.println("TestApplet caught: " + ie.toString());
          }
          _pd.disposeDialog();

          //display the Launcher panel
          getContentPane().add(makeAppletPanel());
          setVisible(true);

          //make the children
          makeChildDialog();

          //let's call it from the query method
          query("junk");

        }

        /**
         * This method builds the dialog for clicking on the chart
         * button. It is set to hide until the user clicks "chart"
         */
        private void makeChildDialog() {
          ChildDialog child = new ChildDialog(_appletContext);
          _childDialog = child.buildGUI();
        }


        /**
         * This method builds the Launcher JPanel which consists of
         * the chart, screen, and portfolio buttons and the Quick
         * Search box.
         * @return Container - formatted JPanel
         */
        public Container makeAppletPanel() {

          //build panel with Chart/Screen/Portfolio buttons
          _button = new JButton("Open Child Dialog");
          _button.setFocusPainted(false);
          _button.setBackground(Color.black);
          _button.setForeground(Color.white);
          _button.setActionCommand("child");
          _button.addActionListener(this);

          _pane = new JPanel();
          _pane.setBackground(Color.blue);
          _pane.add(_button);

          return _pane;
        }

        /**
         * This method handles listener actions and displays dialogs
         * based on mouse clicks
         * @param e - action event performed
         */
        public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();
          _childDialog.pack();
          _childDialog.show();
        }


        /**
         * This method goes off and queries a database. It puts up a non-modal
         * dialog with a simple text method. The dialog is disposed when the
         * query is finished.
         * <P>
         * This is what is giving me oodles of trouble...the message just won't
         * appear when I call it from anywhere besides the init method...
         */
        public void query(String query) {

          try {
             _pd.showDialog("Called from TestApplet.query method");
           } catch (Exception e) {
             System.out.println("TestApplet.query caught: " + e.toString());
           }

          //simulate database query here...for now I'm just gonna sleep on
          //the main application thread so you can see if the process dialog
          //text appears... queries are taking about 6 seconds
          try {
            Thread.sleep(6000);
          } catch (InterruptedException ie) {
            System.out.println("TestApplet.query caught: " + ie.toString());
          }

          _pd.disposeDialog();

        }

        /* PRIVATE METHODS */

        private void setBaseURL() {
          if (_codeBase == null) {
            _codeBase = getCodeBase();
          }
          System.out.println("codebase: " + _codeBase);
        }

        private void setAppletContext() {
          _appletContext = this.getAppletContext();
          System.out.println(_appletContext.toString());
        }

      }


      ChildDialog.java
      /*
       *
       * Child class for TestApplet
       */
      package test;

      import javax.swing.*;
      import javax.swing.border.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;
      import java.util.*;
      import java.applet.AppletContext;

      /**
       * This class uses builds a simple GUI and calls the query method
       * in the TestApplet class
       * <P>
       * @author JJL
       * @date August 2000
       */
      public class ChildDialog implements ActionListener {
        protected JDialog _childDialog = null;
        protected JButton _button = null;
        protected JFrame _frame = null; //parent frame is null

        protected TestApplet _applet = null;


        /**
         * Constructor
         * @param frame - parent frame passed in by calling class
         * @param title - Dialog title
         * @param modal - true if modal; false otherwise
         */
        public ChildDialog(AppletContext applet) {
          _applet = (TestApplet) applet.getApplet("TestApplet");
        }

        /**
         * This method builds the Chart Dialog GUI
         * @return JDialog - formatted JDialog
         */
        public JDialog buildGUI() {
          _button = new JButton("Launch Applet Query");
          _button.setFocusPainted(false);
          _button.setBackground(Color.black);
          _button.setForeground(Color.white);
          _button.setActionCommand("child");
          _button.addActionListener(this);

          _childDialog = new JDialog(_frame, "Child Dialog");
          _childDialog.getContentPane().add(_button);
          return _childDialog;
        }

        /**
         * This methods handles button clicks and search input
         * It handles ActionListener action events
         * @param e - the action event being performed
         */
        public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();

          //for the heck of it lets try displaying our own dialog
          ProcessDialog pd = new ProcessDialog();
          pd.showDialog("created directly from ChildDialog class");

          try {
            Thread.sleep(6000);
          } catch (InterruptedException ie) {
            System.out.println("ChildDialog.actionPerformed caught: " +
      ie.toString());
          }

          pd.disposeDialog();

          //ok now lets try it from Applet context
          _applet.query("junk query");
        }

      }

      ProcessDialog.java

      package test;

      import javax.swing.*;
      import java.awt.*;
      import java.util.*;

      /**
       * This class displays a simple JDialog to tell the user that the
       * server is busy processing a request
       * <P>
       * @author Jeanette Lundgren
       */
      public class ProcessDialog {
        private static final boolean IS_MODAL = false;
        private static final String TITLE = "Processing...";
        private static final JFrame PARENT = new JFrame();
        private JDialog _dialog = new JDialog(PARENT, TITLE, IS_MODAL);

        //force dialog to have it's own thread so that modal=true without
        //stopping main execution thread and displaying it's components
        private java.util.Timer _timer = null;
        private static final long TIMER_INTERVAL = 1;

        /**
         * Constructor
         */
        public ProcessDialog() {
        }

        public void showDialog(String messageText) {
          _dialog = new JDialog(PARENT, TITLE, IS_MODAL);
          _dialog.setLocationRelativeTo(PARENT);
          _dialog.setBackground(Color.yellow);

          JLabel message = new JLabel(messageText);
          message.setForeground(Color.black);

          _dialog.getContentPane().add(message);
          _dialog.pack();
          _dialog.show();

          /* I Tried setting MODAL = true and running dialog on own
             thread...but it didn't work. For some reason non-modal
             dialog behave properly when called from child classes.
             Unfortunately I CANNOT block the application thread !!
          _timer = new java.util.Timer();

          _timer.schedule( new TimerTask() {
                public void run() {
                _dialog = jop.showProcessDialog();
                _dialog.validate();
                _dialog.pack();
                _dialog.show();
               }
          }, TIMER_INTERVAL);
          */
        }

        public void disposeDialog() {
          //_timer.cancel();
          _dialog.validate();
          _dialog.dispose();
        }

      }
      (Review ID: 112584)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: