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

When JDesktop already added to parent, can't see JSlider in JInternalFrame

XMLWordPrintable

    • beta
    • generic
    • generic



      Name: gsC80088 Date: 03/12/99


      I attach some example code which is an extension of the
      JInternalFrame demo in the Sun Java Tutorial (Jan 22).

      Basically it creates a JFrame, JDesktop, and calls a function
      called createFrame() 3 times to add 3 JInternalFrames
      with a JSlider and a JLabel added. THEN it adds the JDesktop
      to its parent (a viewport, in scrollpane, in top JFrame content
      pane).
      All is well, both the JSlider and JLabel are visible.

      Then via menu option the user is able to create further
      JInternalFrames calling the same function as above - createFrame
      - so now the JInternalFrame is being added to a JDesktop
      which has ALREADY been added to it parent.
      In all these cases the JLabel is visible, but the JSlider is
      NOT.

      This program is a cut down solution trying to pinpoint a JSlider
      not appearing in a much larger commercial application we have
      under development which creates internal frames in response
      to menu option selections, this exhibited a similar problem -
      a window with everything on it except the JSlider. This window
      also exhibited a problem with table fillInternalFrameDemo.java MyInternalFrameFactory.javaing/sizing which I
      will put in a seperate bug report if I can pin it down.

      The attached code is based on the Java tutorial, you may
      notice I have converted what was a subclass of JInternalPane
      to a factory class, this was becuase I suspected for some time
      that is was subclassing JInternalPane that caused the problem,
      but in fact it did not prove to be the case.

      Code attached is 2 source files.
      1. InternalFrameDemo.java
      2. MyInternalFrameFactory.java


      InternalFrameDemo.java
      ---------------
      // Internal Frames and JSliders bug report - code based on Sun Tutorial
      // internal frames demo

      //XXX: Metalworks is the only desktop-using app I looked at. This follows
      //XXX: its lead. How will these desktop apps usually be arranged? Really
      //XXX: a menu at the top of the main JFrame?

      //XXX: You might think of using a JWindow to house the JDesktopPane, but
      //XXX: JWindows behave badly. They *always* stay in front. And you can't
      //XXX: miniaturize them.

      import javax.swing.JInternalFrame;
      import javax.swing.JDesktopPane;
      import javax.swing.JMenu;
      import javax.swing.JMenuItem;
      import javax.swing.JMenuBar;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JRootPane;
      import javax.swing.JSlider;
      import javax.swing.JScrollPane;
      import javax.swing.JViewport;
      import javax.swing.JLabel;
      import java.awt.GridBagLayout;
      import java.awt.GridBagConstraints;

      import java.awt.event.*;
      import java.awt.*;

      public class InternalFrameDemo extends JFrame {

          JDesktopPane desktop;

          public InternalFrameDemo() {
              super("InternalFrameDemo");

              //Make the big window be indented 50 pixels from each edge
              //of the screen.
              int inset = 50;
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              setBounds(inset, inset,
                        screenSize.width - inset*2,
                        screenSize.height-inset*2);

              //Quit this app when the big window closes.
              addWindowListener(new WindowAdapter() {
      //XXX: Notes for Kathy.
      //XXX: common problem: windowClosing or other event method ignored.
      //XXX: did you add it as a listener? Did you define the method right,
      //XXX: E.g., is it public void, spelled right, and have the right kind
      //XXX: of argument (issue for adapter subclasses).
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });

              //Set up the GUI.
              desktop = new JDesktopPane(); //a specialized layered pane
              createFrame(); //Create first window
              createFrame(); //Create second window
              createFrame(); //Create firs


      JPanel contentPane = (JPanel)this.getContentPane(); // contentPane of JFrame
      JScrollPane scrollPane = new JScrollPane();
      contentPane.add(scrollPane, "Center", -1);
      JViewport viewPort = scrollPane.getViewport();
      viewPort.add(desktop, null, -1);

              setJMenuBar(createMenuBar());
          }

          protected JMenuBar createMenuBar() {
              JMenuBar menuBar = new JMenuBar();

              JMenu menu = new JMenu("Document");
              JMenuItem menuItem = new JMenuItem("New");
              menuItem.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      createFrame();
                  }
              });
              menu.add(menuItem);
              menuBar.add(menu);

              return menuBar;
          }

          protected void createFrame() {
              JInternalFrame iframe = MyInternalFrameFactory.createIFrame();

              JRootPane root = iframe.getRootPane();
              JPanel contentPane = (JPanel)root.getContentPane(); //content Pane of Int Frame
              //contentPane.setLayout( new java.awt.BorderLayout() );
      contentPane.setLayout( new java.awt.GridBagLayout() );


              JSlider slider = new JSlider();
              //contentPane.add(slider, "Center", -1);

              JLabel label = new JLabel("Hello world");
              //contentPane.add(label, "Center", -1);
      {
                  GridBagConstraints grid = new GridBagConstraints();
                  grid.gridwidth = GridBagConstraints.REMAINDER;
                  grid.weightx = 1.0;
                  grid.weighty = 1.0;
                  contentPane.add(slider, grid, -1);
                  grid.anchor = GridBagConstraints.NORTH;
                  grid.fill = GridBagConstraints.HORIZONTAL;
                  grid.weighty = 0.0;
                  contentPane.add(label, grid, -1);
      }

              desktop.add(iframe);
              try {
                  iframe.setSelected(true);
              } catch (java.beans.PropertyVetoException e2) {}
          }

          public static void main(String[] args) {
              InternalFrameDemo frame = new InternalFrameDemo();
              frame.setVisible(true);
          }
      }

      MyInternalFrameFactory.java
      -------
      import javax.swing.JInternalFrame;

      import java.awt.event.*;
      import java.awt.*;

      //XXX: Note: setVisible(true) has a different implementation than
      //XXX: show(). This seems highly bogus -- the setVisible(true)
      //XXX: method was supposed to replace show() -- not add another
      //XXX: method.
      public class MyInternalFrameFactory {

          static int openFrameCount = 0;
          static final int xOffset = 30, yOffset = 30;

          static public JInternalFrame createIFrame() {

      JInternalFrame iframe =
                  new JInternalFrame("Factory Window #" + (++openFrameCount),
                    true, //resizable
                    true, //closable
                    true, //maximizable
                    true);//iconifiable

              //...Create the GUI and put it in the window...
              //...Then set the window size or call pack...
              iframe.setSize(300,300);

              //Set the window's location.
              iframe.setLocation(xOffset*openFrameCount, yOffset*openFrameCount);

      return (iframe);
          }
      (Review ID: 55304)
      ======================================================================

      Name: gsC80088 Date: 03/12/99


      To see this bug:

      1. Compile and run the program below.
      2. From the menubar, select Test|Slider Frame. A JInternalFrame
         containing a JSlider will appear.
      3. Select Test|Other Frame. Another JInternalFrame appears. No
         problem.
      4. Quit the program and run it again.
      5. This time, select Test|Other Frame first.
      6. Select Test|Slider Frame. The JInternalFrame appears as
         before, but the JSlider it contains does not.

      The problen has to do with calling JInternalFrame.toFront(). If
      you comment out that line in the program below, it will work.

      java full version "JDK-1.2-V"

      // Begin BugTest.java
      import javax.swing.*;
      import java.awt.event.*;

      public class BugTest {

          public static void main(String[] arg) {
              JFrame frame=new JFrame("Test");
              final JDesktopPane desktop=new JDesktopPane();
              frame.setContentPane(desktop);
              JMenuBar menubar=new JMenuBar();
              JMenu menu=new JMenu("Test");
              menu.add(new AbstractAction("Other Frame") {
                  public void actionPerformed(ActionEvent e) {
                      JInternalFrame iFrame=new JInternalFrame("Other Frame");
                      iFrame.getContentPane().add(new JLabel("Blah blah blah"));
                      iFrame.pack();
                      desktop.add(iFrame);
                  }
              });
              menu.add(new AbstractAction("Slider Frame") {
                  public void actionPerformed(ActionEvent e) {
                      JInternalFrame iFrame=new JInternalFrame("Slider Frame");
                      JPanel panel=new JPanel();
                      panel.add(new JLabel("Slider:"));
                      panel.add(new JSlider());
                      iFrame.setContentPane(panel);
                      iFrame.pack();
                      desktop.add(iFrame);

                      // Here is the problem
                      iFrame.toFront();
                  }
              });
              menubar.add(menu);
              frame.setJMenuBar(menubar);
              frame.pack();
              frame.setSize(500, 500);
              frame.setVisible(true);
          }
      }
      // End BugTest.java
      ======================================================================

      Name: skT88420 Date: 06/22/99


      When a JInternalFrame containing a JSlider controls is first
      created the JSlider is not visible until you resize the window.
      In debug mode it shows that the JSlider is first created, then
      the JInternalFrame is drawn over the JSlider making the
      Slider not visible. All other controls seem to be drawn after
      the JInternalFrame is drawn except for JSlider. Again this
      is only when it is first created. After I resize the window
      the JSlider is drawn twice, once before the JInternalWindow
      and once after, which makes it visible.
      (Review ID: 84671)
      ======================================================================

            hgajewsksunw Hania Gajewska (Inactive)
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: