I realize from the comments I've received in response to my post that
I did not state the problem completely accurately, and also that the
sample code I presented did not do justice to the problem.  So below
is another sample program, one which illustrates the problem
perfectly.


(If you can compile and run it, you can skip the next few paragraphs
of description, and continue reading at the paragraph that begins with
"The problem...")

It produces a GUI roughly like this
        ___ ___
       |   | B | E
       | A |---|
       |   | C |
       |___|___|
       |_______|D

A contains a JTextArea, B contains a JButton within a JPanel, C
contains a JList within a JScrollPane, and D is a JLabel.  A, B, and C
are laid out in a JPanel E using GridBagLayout.  E and D are laid out
in a JFrame using a BoderLayout (E is CENTER and D is SOUTH).

Initially, the JList in C is empty, and D shows the message "The list
is empty".  Clicking the button "Toggle" in B adds a visible item to
the list in C, and clears the message in D.  Clicking the same button
again, removes the item from the JList in C, and causes the message
"The list is empty" to be displayed again.  Clicking the button
therefore toggles between two states.

The problem is that clicking on the "Toggle" button when the list is
empty (thereby adding one element to the list) causes C to become
*narrower* (and C wider).  The opposite happens if one hits "Toggle"
while the list is not empty.

Why is this?  (Note that C contains a JScrollPane.)  Is this a bug?
It sure looks like that to me.

Thank you *very* much for your help.

KJ


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BuggySwing extends JPanel {

  private JLabel messageBar;
  private GridBagConstraints gbc;
  private JButton button;
  private JPanel panel;
  private DefaultListModel listModel;
  private JList list;
  private JScrollPane scrollPane;
  private boolean emptyList = true;

  public BuggySwing() {
    super();
    setLayout(new GridBagLayout());

    gbc = getBaseConstraints();
    gbc.gridx = 0; gbc.gridy = 0;
    gbc.gridheight = GridBagConstraints.REMAINDER;
    add(new JTextArea("Some text"), gbc);

    button = new JButton("Toggle");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      listModel.clear();
        if(emptyList) {
            emptyList = false;
                listModel.addElement("one list item");
                    messageBar.setText(null);
                      }
                        else {
                            emptyList = true;
                                messageBar.setText("The list is empty");
                                  }
                                  }
      });

    panel = new JPanel();
    panel.add(button);

    gbc = getBaseConstraints();
    gbc.gridx = 1; gbc.gridy = 0;
    gbc.weighty = 0.;
    add(panel, gbc);

    listModel = new DefaultListModel();
    list = new JList(listModel);
    emptyList = true;
    scrollPane = new JScrollPane(list);

    gbc = getBaseConstraints();
    gbc.gridx = 1; gbc.gridy = 1;
    gbc.weighty = 1.;

    add(scrollPane, gbc);
  }

  private GridBagConstraints getBaseConstraints() {
    GridBagConstraints baseGbc = new GridBagConstraints();
    baseGbc.weightx = baseGbc.weighty = 1.;
    baseGbc.fill = GridBagConstraints.BOTH;
    return baseGbc;
  }

  private static JLabel makeMessageBar() {
    JLabel label = new JLabel();
    int width = (int) (label.getPreferredSize().getWidth());
    int height = label.getFontMetrics(label.getFont()).getHeight();
    label.setPreferredSize(new Dimension(width, height));
    return label;
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    BuggySwing panel = new BuggySwing();
    panel.messageBar = makeMessageBar();
    panel.messageBar.setText("The list is empty");

    frame.getContentPane().add(panel.messageBar, "South");
    frame.getContentPane().add(panel, "Center");
    
    frame.pack();
    frame.show();
  }
}

_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to