Solved

SpringLayout problem

Posted on 2010-08-20
10
425 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:allelopath
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33486867
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
 
LVL 1

Author Comment

by:allelopath
ID: 33486923
That's not what I'm looking to do. I want a JPanel that contains a JPanel that is using SpringLayout.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33487089
OK. Just use your code, but do
myPanel.setLayout(mySpringLayout);

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 33487347
Does this accomplish that?
JPanel myPanel = new JPanel(mySpringLayout);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33487444
Yes, that'll do it
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:allelopath
ID: 33488864
I already have that, in the original code
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 33489025
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
 
LVL 1

Author Comment

by:allelopath
ID: 33489130
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 33489158
It's a question of how the subpanel behaves as far as sizing is concerned
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 33489224
> 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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now