Re: Justifying box components



H.L wrote:
I want to put a JSlider underneath a label explaining what it is there
for. I have used a Box object instantiated with the createVerticalBox
constructor. The problem is that the text and the slider has a bad
alignment. Is there any way to make them both centered or justified to
either side of the column? Thanks in advance.

Håkan Lane

I haven't played with BoxLayout much but what you want to do is really easy with GridBagLayout.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0; c.insets = new Insets(2,2,2,2);
c.anchor = GridBagConstraints.CENTER;

JLabel l = new JLabel("JSlider Info");
f.add(l,c);

++c.gridy;
JSlider s = new JSlider(0,100,50);
f.add(s,c);

f.setSize(400,300);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: GridBagLayout isnt sitting right
    ... I'm trying to use a gridbaglayout to set the layout of a dialog ... >> borderlayout and the center panel contains the gridbaglayout ... > public class test1 { ... > public static void main{ ...
    (comp.lang.java.programmer)
  • last version
    ... i'm trying to make it draggable with a slider to set the fonts. ... public static void main ... int fontSize = source.getValue; ... Sometimes I'm in a good mood. ...
    (comp.lang.java.help)
  • Re: GridBagLayout question: Position 3 and 2 equal sized buttons problem ?
    ... So what I would do in a case like this is to use a Panel for the second row with a FlowLayout or GridBagLayout. ... public static void main{ ... JFrame f = new JFrame; ...
    (comp.lang.java.programmer)
  • Re: TotallyGridBag
    ... stuck with GridBagLayout. ... that I preferred nested layouts, ... public static void main{ ... GBL usually behaves until it strikes a component ...
    (comp.lang.java.gui)
  • Re: TextField
    ... All you can do is make sure the preferredSize is no bigger than ... and use a GridBagLayout or similar with right alignment. ...
    (comp.lang.java.programmer)