Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UrlyBird status bar

 
Pablo Aravena
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends

I'm trying to implement a Status bar class that displays messages on the left and other things on the right like a green icon that indicates that the bookable rooms are green, but something annoying is happening with this code, the labels are always centered I'dont know exactly what is happenning.

Thanks in advance for any help

Bye

public class StatusBar extends JPanel {
private JLabel status = new JLabel("Connected");

public StatusBar() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(status, constraints);
add(status);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(status, constraints);
add(new JLabel("Bookable"));
}
}
 
Roy Mallard
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would *guess* it's because gridbaglayout doesn't stretch components to the width of the parent container.
Maybe try drawing a border around your jpanel to confirm this.
Try using a different layoutmanager, such as springlayout.
 
Pablo Aravena
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a solution easier than using Grid Bag

Thanks

public class StatusBar extends JPanel {

private static final String LABEL_BOOKABLE_ROOMS = "Bookable rooms";

private JLabel status = new JLabel();

public StatusBar() {
setLayout(new GridLayout(1,2));
add(status);
ImageIcon icon = createImageIcon("green.gif");
add(new JLabel(LABEL_BOOKABLE_ROOMS, icon, JLabel.RIGHT));
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic