Solved

SpringLayout problem

Posted on 2010-08-20
10
428 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
Android development question 2 55
jsp login check 12 43
Selenium docs api java index 3 48
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

820 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