Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Components disappear on resize...

843804Oct 1 2003 — edited Apr 27 2005
I am having problems with components disappearing when resizing a JPanel.

The Parent JPanel contains a whole bunch of other child JPanels, and each of those contains a variety of compoenents (mostly JLables, JTextFields, JButtons and JCombBoxes). For esthetic reasons, I use different layout managers in each of the child JPanels, and a BorderLayout for the Parent. Each child pnaels components are layed out horizontally.

So, my problem is that when i use GridBagLayout or SpringLayout, if I resize the parent, the right most components disappear. Following advice on the forum, I have tried to set sensible minimum sizes for each component, but that has not helped. What I want is the same behaviour as a GridLayout, where the components become too small to use, but are still visible. I don't want to use GridLayout for some of these as it looks pretty cruddy having equispaced labels and combo-boxes, hence the attempt to use the two above.

Below is a quick code example demonstrating the behaviour the I current get....
import java.awt.*;
import javax.swing.*;
public class SpringFormTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Example");
    JPanel topPanel = new JPanel();
    topPanel.setBorder ( BorderFactory.createEtchedBorder() );
    topPanel.setLayout ( new GridLayout (1, 0, 10, 10) );

    JPanel lowPanel = new JPanel();
    lowPanel.setBorder ( BorderFactory.createEtchedBorder() );

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(new GridLayout(2, 0, 20, 20) );
    lowPanel.setLayout(layout);

    Component left1 = new JLabel("Left");
    Component middle1 = new JButton("Please do not press me!");
    Component right1 = new JTextField(15);

    Component left2 = new JLabel("Left");
    Component middle2 = new JButton("Please do not press me!");
    Component right2 = new JTextField(15);

    topPanel.add(left1);
    topPanel.add(middle1);
    topPanel.add(right1);
    lowPanel.add(left2);
    lowPanel.add(middle2);
    lowPanel.add(right2);

    contentPane.add(topPanel);
    contentPane.add(lowPanel);

    layout.putConstraint(SpringLayout.WEST,  left2, 10,
      SpringLayout.WEST,  lowPanel);
    layout.putConstraint(SpringLayout.NORTH, left2, 25,
      SpringLayout.NORTH, lowPanel);
    layout.putConstraint(SpringLayout.WEST,  middle2, 10,
      SpringLayout.EAST,  left2);
    layout.putConstraint(SpringLayout.NORTH, middle2, 25,
      SpringLayout.NORTH, lowPanel);
    layout.putConstraint(SpringLayout.NORTH, right2, 25,
      SpringLayout.NORTH, lowPanel);
    layout.putConstraint(SpringLayout.WEST, right2, 20,
      SpringLayout.EAST, middle2);

    frame.setSize(300, 200);
    frame.show();
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 25 2005
Added on Oct 1 2003
13 comments
850 views