set the weightx property of GridBagConstraints as 0 this is will not strech
the component in cell

Thanks and Regards,
Gary Grewal

-----Original Message-----
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: 10/24/2001 10:24 AM
Subject: How to stop dynamic component resizing in GBL?





OK, here's another problem with GridBagLayout.  Hopefully this is a
simpler one than the previous one I posted.

Some of the componets I'm arranging using GridBagLayout have
dynamically varying contents.  The problem is that sometimes, when the
contents of one of these components change, GBL resizes the
components!  (The sample code below illustrates the problem.  Clicking
on the button causes its text to change, which results in a change in
the sizes of the GUI's components.)  I want the resizing of components
to happen if and only if the user resizes application window.  How can
I configure GBL to enforce this constraint?

Thanks,

KJ

/* Class Sick2.java */
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;

public class Sick2 extends JPanel {

  private static JFrame frame;
  private JScrollPane scrollPane;
  private JPanel panel;
  private JTree tree;
  private JButton button;
  private GridBagLayout gbl;

  public Sick2() {
    super();
    gbl = new GridBagLayout();
    setLayout(gbl);

    GridBagConstraints gbc;

    tree = new JTree
      (new DefaultTreeModel(new DefaultMutableTreeNode("Minimal
tree")));
    scrollPane = new JScrollPane(tree);
    scrollPane.setMinimumSize(scrollPane.getPreferredSize());

    gbc = getBaseConstraints();
    gbc.gridx = 0;
    add(scrollPane, gbc);

    button = new JButton("short");
    button.addActionListener(new MyButtonListener());

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

    gbc = getBaseConstraints();
    gbc.gridx = 1;
    add(panel, gbc);
  }

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

  private class MyButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String text = button.getText();
      if(text.equals("short")) {
        button.setText("v e r y   l o n g");
      }
      else {
        button.setText("short");
      }
    }
  }

  public static void main(String[] args) {
    frame = new JFrame();
    frame.getContentPane().add(new Sick2(), "Center");
    frame.pack();
    frame.show();
  }
}



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

Reply via email to