This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add Components To JPanel  RSS feed

 
Mark Gowdy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I am new to Java/Swing, and I am trying to add several Components(JList, JButton, 2 x JTextFields and JSlider) to a Panel. I want them to be stacked above each other, but when I use GridLayout the Components fill the Panel. Is there an easy way to get them stacked without using GridBagLayout?
Thanks
Mark
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add each of the components to a panel , and then add the panel to the container with GridLayout... The components will display at their preferred size due to FlowLayout being the default LayoutManager of a panel, and each of the panels will stack on top of each other in the GridLayout, and take up the whole size of the grid "cell" they are in... but that doesn't matter because they are a Panel...

HTH,
N8
 
Mark Gowdy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have at the moment. Not sure what you mean.
Where do I go from here?
Thanks
Mark
-----------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame {
public static void main(String[] args) {
JFrame frame = new SwingCassette2("Swing Cassette");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(400, 400);
frame.setLocation(100, 100);
String[] listItems = new String[] {"one", "two", "three"};
JList list = new JList(listItems);
JButton button1 = new JButton("button1");
JTextField text1 = new JTextField("cassette name");
JTextField text2 = new JTextField("owner's name");
JSlider slider = new JSlider(0, 30, 15);
JPanel left = new JPanel(new GridLayout(0, 1));
//left.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
left.add(list);
left.add(button1);
left.add(text1);
left.add(text2);
left.add(slider);
JTextArea right = new JTextArea();
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
frame.setContentPane(splitPane);
frame.setVisible(true);
splitPane.setDividerLocation(0.5);
}
}
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A BoxLayout would be perfect in this case. You can stack components either vertically or horizontally and they will retain their preferred size. Here is a link to the tutorial for BoxLayout:
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html
 
Mark Gowdy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Angela Ann
It all makes perfect sense now.
Regds
Mark
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Nathan was saying to put each component in there own panel before placing in the grid. The Boxlayout is much better for this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!