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

Boxlayout not honoring preferred size for JComboBox

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.0
    • client-libs



      Name: dbT83986 Date: 01/11/99


      If you have a JComboBox inside a panel that is using the BoxLayout
      manager, and that panel has extra space due to some other component
      within the frame, then the combobox's width will be wrong because
      the BoxLayout manager stretches it to fill the extra space. Since
      the BoxLayout manager is suppose to use preferred sizes this seems
      to be a definite bug.

      Even when adding "glue" to the BoxLayout manager to try and force
      the combobox all the way to the left, the width of the combobox
      is still wrong because it is much wider than its widest element.

      The following code will display the bug. You can use "noglue"
      on the command line to avoid having horizontal glue added. If you
      do you'll see the combobox stretched to fill the extra space.
      You can also use "notext" on the command line to avoid having
      the JTextField component added. If you do then you'll see that
      the correct width for the combobox is used.

      //~~~~~~~~~~~~~~~~~~~~~~ cut here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      class bug2 {
          public static void main (String args[]) {
              // Command line options:
              // noglue -> no horizontal glue will be added in toolbar
              // notext -> no JTextField component will be added in lower panel
              boolean addGlue = true;
              boolean addText = true;
              for (int i=0; i < args.length; i++) {
                  if (args[i].equals("noglue")) addGlue = false;
                  else if (args[i].equals("notext")) addText = false;
              }

              JFrame frame = new JFrame();
              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
              });

              JPanel pane = new JPanel();
              pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));

              JLabel l = new JLabel("Choices : ");
              String s[] = {"Choice1", "Choice Number2"};
              JComboBox cb = new JComboBox(s);

              pane.add(l);
              pane.add(cb);
              if (addGlue)
                  pane.add(Box.createHorizontalGlue());

              JTextField txt = new JTextField(40);

              // *** Create main panel to hold two inner containers ***
              JPanel main = new JPanel();
              main.setLayout(new BorderLayout());
              main.add(pane, BorderLayout.NORTH);
              if (addText)
                  main.add(txt, BorderLayout.CENTER);

              frame.getContentPane().add(main);
              frame.pack();
              frame.setVisible(true);
          }
      }
      (Review ID: 52452)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: