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

JToolBar should reject setLayout() if it will always revert to BoxLayout

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.5.1



      Name: krT82822 Date: 07/04/99


      orig synopsis: "Floating&redocking a JToolBar sets its LayoutManager to BoxLayout"

      If you set a JToolBar's layout manager to something else than
      the standard BoxLayout, then at first everything is fine.
      However, if you then move and redock the tool bar with a different
      orientation (e.g. moving the toolbar from NORTH to WEST, which
      causes it to change from horizontal to vertical), the layout
      manager changes back to BoxLayout.
      This is either a bug or an undocumented feature.
      It is reproducible under both JDK 1.2 (demo included) and
      under JDK1.1.6 (change the import statements in the included
      demo code).

      I manipulated the ToolBarDemo.java program from the Java Tutorial
      ( http://java.sun.com/docs/books/tutorial/uiswing/components/
        example-swing/ToolBarDemo.java )
      to demonstrate this.
      The program source follows, it contains comments explaining
      its use.
      The program is for JDK1.2 .

      **************CUT HERE*************************
      import javax.swing.JToolBar;
      import javax.swing.JButton;
      import javax.swing.ImageIcon;

      import javax.swing.JFrame;
      import javax.swing.JTextArea;
      import javax.swing.JScrollPane;
      import javax.swing.JPanel;

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

      /*
       * Toolbar Bug Demonstration
       * If you set a JToolBar's layout manager to something else than
       * the standard box layout, then at first everything is fine.
       * However, if you then move and redock the tool bar with a different
       * orientation the layout manager changes back to BoxLayout.
       * This is either a bug or an undocumented feature.
       *
       * To use this program:
       * Just click on any button in the window to display the toolbar's
       * current layout manager.
       * Move and redock the toolbar in WEST or EAST position,
       * then click a button again to see the change.
       *
       * Sebastian Leske ###@###.###
       * 5 Feb 1999
       */
      public class ToolBarDemo extends JFrame {
          protected JTextArea textArea;
          protected String newline = "\n";
          public JToolBar toolBar = new JToolBar();

          public ToolBarDemo() {
              //Do frame stuff.
              super("ToolBarDemo");
              addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });

              //Create the toolbar.
              addButtons(toolBar);

              //Create the text area used for output.
              textArea = new JTextArea(5, 30);
              JScrollPane scrollPane = new JScrollPane(textArea);

      //inserted to demonstrate BUG
      //set ToolBar layout manager to GridLayout
      toolBar.setLayout(new GridLayout());

              //Lay out the content pane.
              JPanel contentPane = new JPanel();
              contentPane.setLayout(new BorderLayout());
              contentPane.setPreferredSize(new Dimension(400, 100));
              contentPane.add(toolBar, BorderLayout.NORTH);
              contentPane.add(scrollPane, BorderLayout.CENTER);
              setContentPane(contentPane);
          }

          protected void addButtons(JToolBar toolBar) {
              JButton button = null;

              //first button
              button = new JButton("Button");
              button.setToolTipText("This is the left button");
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      displayResult("Action for first button");
                  }
              });
              toolBar.add(button);

              //second button
              button = new JButton("Button");
              button.setToolTipText("This is the middle button");
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      displayResult("Action for second button");
                  }
              });
              toolBar.add(button);

              //third button
              button = new JButton("Button");
              button.setToolTipText("This is the right button");
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      displayResult("Action for third button");
                  }
              });
              toolBar.add(button);
          }

          protected void displayResult(String actionDescription) {
              textArea.append(actionDescription + newline);
      //changed to demonstrate BUG
      textArea.append("Current toolbar layout manager: "+toolBar.getLayout()+newline);
          }

          public static void main(String[] args) {
              ToolBarDemo frame = new ToolBarDemo();
              frame.pack();
              frame.setVisible(true);
          }
      }
      (Review ID: 53787)
      ======================================================================

            apikalev Andrey Pikalev
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: