Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SpringLayout problem

Posted on 2010-08-20
10
Medium Priority
?
432 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

636 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