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

JToolBar - spacing needed when adding JComboBox, JSpinner and JLabel

XMLWordPrintable

      Need spacing between the JComboBox's, JSpinner and JLabel after adding them to a toolbar, like there is between the JButton's and JTextField's. See picture attached.

      Test code below:
      --------------------------------------------------------------------------
      import java.awt.*;
      import java.util.*;
      import java.util.List;
      import javax.swing.*;

      public class ToolBarTest extends JFrame {

              public ToolBarTest() {

                      setLayout(new BorderLayout());

                      // Add buttons
                      JToolBar toolBar = new JToolBar("Test");
                      toolBar.add(new JButton("Green"));
                      toolBar.add(new JButton("Blue"));

                      // Add combobox's
                      String[] styles = {"Plain","Bold","Italic","Bold/Italic"};
                      toolBar.add(new JComboBox(styles));
                      toolBar.add(new JComboBox(styles));

                      // Add spinner
                      List values = new ArrayList();
                              for(int i=1; i<=9; i++) {
                                      values.add("0" + i);
                      }

                      SpinnerListModel spinnerModel = new SpinnerListModel(values);
                      JSpinner sizeSpinner = new JSpinner(spinnerModel);
                      sizeSpinner.setValue("04");
                      toolBar.add(sizeSpinner);

                      // Add button
                      toolBar.add(new JButton("Black"));

                      // Add textfield
                      toolBar.add(new JTextField(8));

                      // Add button
                      toolBar.add(new JButton("Purple"));

                      // Add label
                      toolBar.add(new JLabel("Need Spacing"));

                      // Some buttons
                      toolBar.add(new JButton("Red"));
                      toolBar.add(new JButton("Orange"));

                      add(toolBar, BorderLayout.NORTH);
                      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      pack();
                      setVisible(true);
              }

              public static void main(String[] args) {
                      new ToolBarTest();
              }
      }

            Unassigned Unassigned
            sjchan Steven Chan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: