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

JToolBar in System Look and feel disappears on Microsoft Windows XP

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      FULL PRODUCT VERSION :
      Java version "1.6.0_05"
      Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
      Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      On Windows XP and System Look and Feel, it is possible to drag a floatable JToolBar outside of the desktop area. By this, the JToolBar disappears completely and cannot be retreived again, even not with the Cascading/Titling Windows options of the desktop.
      The problem is related to the fact that when grabbing the toolbar with the mouse, it centers the floating toolbar at the mouse location, but it later positions the toolbar at its top left corner. Therefore, if the toolbar changes its orientation at the drop point, it can be completely outside the desktop.

      In default look and feel, this effect does not happen so easily, since the default look and feel does not center the floating toolbar at the mouse location but rather keeps the top left corner of the toolbar close to the mouse location.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached sample on Windows XP.
      Start dragging the toolbar.
      When the toolbar is close to the left or right border of the window, it changes to vertical mode.
      Drag it carefully to the left away from the left border, or to the right away from the right border, so that it stays in vertical mode.
      (Avoid that you come to close to the top border, to avoid that it changes to horizontal mode).
      Now drag the toolbar so that the mouse is at the top border of the desktop.
      If it is now in vertical mode and you drop it, it switches to horizontal mode and is placed outside the desktop, i.e. at negative y location on the screen. It disappears.

      After it disappeared, it seems not to exist anymore, because it cannot be retrieved.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The toolbar should always stay inside the desktop area.

      ACTUAL -
      The toolbar disappears, obviosuly because it is located completely outside the desktop area.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import java.net.*;
      import javax.swing.*;

      public class Test
      {
        public static final void main(String[] arg)
        {
          try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Throwable e1) {
            e1.printStackTrace();
          }

          final JFrame frame = new JFrame("Test Sample");

          JToolBar controlBar = new JToolBar();
          controlBar.add(new JButton ("A"));
          controlBar.add(new JButton ("B"));
          controlBar.add(new JButton ("C"));
          controlBar.add(new JButton ("D"));
          controlBar.add(new JButton ("E"));
          controlBar.add(new JButton ("testing testig testing testing testing"));
          controlBar.add(new JButton ("testing testig2 testing testin"));

          frame.getContentPane().add(controlBar, BorderLayout.NORTH);
          frame.getContentPane().add(new JPanel(), BorderLayout.CENTER);

          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              frame.setSize(600, 600);
              frame.setVisible(true);
            }
          });
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One workaround is to avoid using the system look and feel.

      The attached program illustrates another workaround:


      public class Test2
      {
        private static boolean fixToolBar = true;


        public static final void main(String[] arg)
        {
          try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Throwable e1) {
            e1.printStackTrace();
          }

          // only needed when fixToolBar = true;
          final ComponentListener listener = new ComponentListener () {
            public void componentResized(ComponentEvent e) {
              Component c = e.getComponent();
              if (c != null) {
                Point p = c.getLocation();
                if (p.x < 0 || p.y < 0) {
                  if (p.x < 0) p.x = 0;
                  if (p.y < 0) p.y = 0;
                  c.setLocation(p);
                }
              }
            }
            public void componentMoved(ComponentEvent e) {
              Component c = e.getComponent();
              if (c != null) {
                Point p = c.getLocation();
                if (p.x < 0 || p.y < 0) {
                  if (p.x < 0) p.x = 0;
                  if (p.y < 0) p.y = 0;
                  c.setLocation(p);
                }
              }
            }
            public void componentShown(ComponentEvent e) {}
            public void componentHidden(ComponentEvent e) {}
          };

          final JFrame frame = new JFrame("Test2 Sample");

          JToolBar controlBar = new JToolBar() {
            public void setBounds(int x, int y, int width, int height) {
              Component parent = getParent();
              while (fixToolBar && parent != null) {
                if (parent instanceof JDialog) {
                  Point p = parent.getLocation();
                  ComponentListener[] listeners = parent.getComponentListeners();
                  boolean found = false;
                  if (listeners != null) {
                    for (int i = 0; i < listeners.length; i++)
                      if (listeners[i] == listener)
                        found = true;
                  }
                  if (!found) {
                    parent.addComponentListener(listener);
                  }
                }
                parent = parent.getParent();
              }
              super.setBounds(x, y, width, height);
            }
          };
          controlBar.add(new JButton ("A"));
          controlBar.add(new JButton ("B"));
          controlBar.add(new JButton ("C"));
          controlBar.add(new JButton ("D"));
          controlBar.add(new JButton ("E"));
          controlBar.add(new JButton ("testing testig testing testing testing"));
          controlBar.add(new JButton ("testing testig2 testing testin"));

          frame.getContentPane().add(controlBar, BorderLayout.NORTH);
          frame.getContentPane().add(new JPanel(), BorderLayout.CENTER);

          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              frame.setSize(600, 600);
              frame.setVisible(true);
            }
          });
        }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mlapshin Mikhail Lapshin (Inactive)
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: