SpringLayout problem

Why doesn't the button appear?

package springlayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class SpringLayoutTest extends JPanel{

	public SpringLayoutTest() {
		
		SpringLayout mySpringLayout = new SpringLayout();
		JPanel myPanel = new JPanel(mySpringLayout);

		JButton helloButton = new JButton("hello");
		myPanel.add(helloButton);

		mySpringLayout.putConstraint(SpringLayout.WEST, helloButton, 10, SpringLayout.WEST, myPanel);
		mySpringLayout.putConstraint(SpringLayout.NORTH, helloButton, 10, SpringLayout.NORTH, myPanel);

		this.add(myPanel);
	}

	/**
	 * 
	 * @param s
	 */
	public static void main(String s[]) {

		JFrame frame = new JFrame("SpringLayoutTest");

		SpringLayoutTest springLayoutTest = new SpringLayoutTest();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(springLayoutTest);
		frame.setSize(300, 300);
		frame.setLocation(300,300);
		frame.setVisible(true);
	}
	
}

Open in new window

LVL 1
allelopathAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
Sorry - you do.

If you want to use an extra panel, you'll have two layout managers, so you need to ensure that the bottom one allows the top one to be visible. Try the following as the first line of your ctor
setLayout(new java.awt.BorderLayout());

Open in new window

0
 
CEHJCommented:
You didn't set the layout on the Container. Try the following
    public SpringLayoutTest() {
        SpringLayout mySpringLayout = new SpringLayout();
	setLayout(mySpringLayout);

        JButton helloButton = new JButton("hello");
        add(helloButton);

        mySpringLayout.putConstraint(SpringLayout.WEST, helloButton, 10,
            SpringLayout.WEST, this);
        mySpringLayout.putConstraint(SpringLayout.NORTH, helloButton, 10,
            SpringLayout.NORTH, this);

    }

Open in new window

0
 
allelopathAuthor Commented:
That's not what I'm looking to do. I want a JPanel that contains a JPanel that is using SpringLayout.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
OK. Just use your code, but do
myPanel.setLayout(mySpringLayout);

Open in new window

0
 
allelopathAuthor Commented:
Does this accomplish that?
JPanel myPanel = new JPanel(mySpringLayout);

Open in new window

0
 
CEHJCommented:
Yes, that'll do it
0
 
allelopathAuthor Commented:
I already have that, in the original code
0
 
allelopathAuthor Commented:
That works, but I don't understand why. JPanel has FlowLayout as a default layout manager. Why do I have to explicitly set a layout manager?
0
 
CEHJCommented:
It's a question of how the subpanel behaves as far as sizing is concerned
0
 
objectsConnect With a Mentor Commented:
> JPanel has FlowLayout as a default layout manager.

because the default flow won't resize the panel to fill the available space (a BorderLayout does)
(i'm assuming thats what you want)
0
All Courses

From novice to tech pro — start learning today.