wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Problems with JSeparator together with GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with JSeparator together with GridBagLayout" Watch "Problems with JSeparator together with GridBagLayout" New topic
Author

Problems with JSeparator together with GridBagLayout

Emil Karlsson
Ranch Hand

Joined: May 23, 2002
Posts: 63
Hi,
Does anyone know why a JSeparator shows perfectly in GridLayout, but when I change to GridBagLayout it does not appear in the frame?
Thanks in advance, Emil
//Works perfect
import java.awt.*;
import javax.swing.*;
public class SeparatorTest {
public static void main(String[] args) {
new SeparatorTest();
}
public SeparatorTest(){
JFrame frame = new JFrame();
frame.setSize(200,200);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1));
JLabel label = new JLabel("label 1");
panel.add(label);
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
Color col = new Color(125,125,125);
sep.setForeground(col);
panel.add(sep);
label = new JLabel("label 2");
panel.add(label);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
//This does not show the separator
import java.awt.*;
import javax.swing.*;
public class SeparatorTestWithGridBag {
public static void main(String[] args) {
new SeparatorTestWithGridBag();
}
public SeparatorTestWithGridBag(){
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setSize(200,200);
JPanel panel = new JPanel();
panel.setLayout(grid);
JLabel label = new JLabel("label 1");
gbc.gridy=0;
gbc.gridx=0;
gbc.weightx=10.0;
gbc.weighty=10.0;
grid.setConstraints(label,gbc);
panel.add(label);
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
Color col = new Color(125,125,125);
sep.setForeground(col);
gbc.gridy=1;
gbc.gridx=0;
grid.setConstraints(sep,gbc);
panel.add(sep);
label = new JLabel("label 2");
gbc.gridy=2;
gbc.gridx=0;
grid.setConstraints(label,gbc);
panel.add(label);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

For the constraints of the JSeparator, add the line :


To remove the horizontal fill for the second label, you would need to set the fill to :


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Emil Karlsson
Ranch Hand

Joined: May 23, 2002
Posts: 63
Thanks a lot Nate!
"Sometimes it's hard to see the forest, because of all the trees"

Emil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with JSeparator together with GridBagLayout
 
Similar Threads
problem in GridBagConstraints.fill
JScrollPane and JComboBox, loss of drop down
GridBag help
GridBagLayout question
Help in gridbaglayout