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

mixed components in JToolBar don't align well

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.2
    • 1.0.1, 1.1.5, 1.2.0
    • client-libs
    • 1.2.2
    • generic, sparc
    • solaris_2.5, solaris_2.5.1, solaris_9
    • Verified

      Mixed components (JButton, JCombobox, JTextfield ...) in JToolBar don't align well, which makes the toolbar look bad/unprofessional. The alignment looks better in Metal Look&Feel than Motif.

      The following test case which contains a toolbar with JButton, JComboBox and JTextFields will demonstrate the problem.

      import com.sun.java.swing.*;
      import com.sun.java.swing.tree.*;


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


      class TestToolBar extends JFrame {
        JToolBar toolbar;
       
        public TestToolBar() {
       
          getContentPane().setLayout(new BorderLayout());
          toolbar = createToolBar();
          getContentPane().add("North", toolbar);
          getContentPane().add("Center", new JLabel("JButton and JComboBox in ToolBar don't align well"));
        }

       

         /**
           * Create Menu Toolbar for the editor
           */
        protected JToolBar createToolBar(){

          toolbar = new JToolBar();
          JButton toolButton;

          toolButton = addTool(toolbar, "new");
          // toolButton.addActionListener(new NewAction());
          toolButton = addTool(toolbar, "importFile");
          // toolButton.addActionListener(new ImportAction());
          toolbar.addSeparator();
          toolButton = addTool(toolbar, "cut");
          //toolButton.addActionListener(cutAction);
          toolButton = addTool(toolbar, "copy");
          // toolButton.addActionListener(copyAction);
          toolButton = addTool(toolbar, "paste");
          //toolButton.addActionListener(pasteAction);

          toolbar.addSeparator();
          addToggleTool(toolbar, "bold");
          //toolButton.addActionListener(new StyledEditorKit.BoldAction());
          addToggleTool(toolbar, "italic");
          addToggleTool(toolbar, "underline");
          
       
          toolbar.addSeparator();
          toolButton = addTool(toolbar, "left");

          toolButton = addTool(toolbar, "center");
       
          toolButton = addTool(toolbar, "right");


          toolbar.addSeparator();
          toolbar.add(createFontChoices());
          toolbar.add(createSizeChoices());
      toolbar.add(new JTextField("This is a TextField"));
          return toolbar;
        }
          /**
           * Create Font Choices
           */
        JComboBox createFontChoices() {
          JComboBox fontChoices = new JComboBox();
          String[] fonts = getToolkit().getFontList();
          for (int i = 0; i < fonts.length; i++) {
            fontChoices.addItem(fonts[i]);
          }
          fontChoices.setEditable(true);
          fontChoices.setSelectedItem("Courier");
          fontChoices.setSize (new Dimension(100,17));

          return fontChoices;
        }

        JComboBox createSizeChoices() {
          JComboBox sizeChoices = new JComboBox();
          sizeChoices.setEditable(true);
          sizeChoices.setSize (new Dimension(20,17));
          String[] fontsizes = {"8","12", "14","16","18","20","24", "48"};
          for (int i = 0; i < fontsizes.length; i++) {
            sizeChoices.addItem(fontsizes[i]);
          }
          sizeChoices.setSelectedItem("12");
       
          return sizeChoices;
        }


        public JButton addTool(JToolBar toolbar, String name) {
          JButton b = (JButton) toolbar.add(new JButton(new ImageIcon("images/" + name + ".gif")));
        b.setToolTipText(name);
        b.setMargin(new Insets(0,0,0,0));
        return b;
        }

        public JToggleButton addToggleTool(JToolBar toolbar, String name) {
          JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(new ImageIcon("images/" + name + ".gif")));
        b.setToolTipText(name);
        b.setMargin(new Insets(0,0,0,0));
        return b;
        }
        
        public static void main(String s[]) {
          TestToolBar testTB = new TestToolBar();
       
          testTB.pack();
          testTB.show();

        }
       }

            gsaab Georges Saab
            tsusunw Tina Su (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: