• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with JSeparator together with GridBagLayout

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
 
Emil Karlsson
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Nate!
"Sometimes it's hard to see the forest, because of all the trees"

Emil
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic