?
Solved

SpringLayout problem

Posted on 2010-08-20
10
Medium Priority
?
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

777 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