Reputation: 2895
I'm trying to make a simple layout where the labels are next to the buttons and close. Here is the order I am adding them:
add(label_1, cons);
cons.gridx = 1;
add(textArea,cons);
cons.gridx = 0;
cons.gridy = 2;
add(button_1,cons);
cons.gridx= 0;
cons.gridy = 4;
add(button_2,cons);
cons.gridx=0;
cons.gridy=6;
add(button_3,cons);
cons.gridx = 1;
cons.gridy = 2;
add(label_2, cons);
cons.gridy=4;
add(label_3,cons);
cons.gridy=6;
add(label_4,cons);
Which looks like this:
I just need the labels positioned closer to the buttons, but it seems the closest x value I can give them is 1, it seems relatively positioned - is there a way I could absolutely position them?
Upvotes: 0
Views: 5038
Reputation: 4283
The window above was produced with the code below, which uses 3 external classes.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GridBagLayout{
JFrame frame;
public GridBagLayout() {
initComponents();
}
private void initComponents(){
JTextField text = new JTextField("",10);
JButton but1 = new JButton("button 1");
JButton but2 = new JButton("button 2");
JButton but3 = new JButton("button 3");
JLabel lab0 = new JLabel("Enter a sentence");
JLabel lab1 = new JLabel("test 1");
JLabel lab2 = new JLabel("test 2");
JLabel lab3 = new JLabel("test 3");
frame = new JFrame("TestGridBagLayout");
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
frame.setLayout(new java.awt.GridBagLayout());
frame.add(lab0, new GBConstraints(0,0));
frame.add(text, new GBConstraints(1,0));
frame.add(but1, new GBConstraints(0,1));
frame.add(lab1, new GBConstraints(1,1));
frame.add(but2, new GBConstraints(0,2));
frame.add(lab2, new GBConstraints(1,2));
frame.add(but3, new GBConstraints(0,3));
frame.add(lab3, new GBConstraints(1,3));
frame.setVisible(true);
frame.pack();
}
public static void main(String[] args) {
new GridBagLayout();
}
}
The window below was produced by changing the new GBConstraints
lines above to the lines shown below the picture, the point being that there are several fairly easy ways to tweak the layout to make it look just the way you want--how close together and how vertically aligned, for two issues.
frame.add(lab0, new GBConstraints(0,0).anchor(EAST));
frame.add(text, new GBConstraints(1,0).ipad(100, 0).anchor(WEST));
frame.add(but1, new GBConstraints(0,1));
frame.add(lab1, new GBConstraints(1,1));
frame.add(but2, new GBConstraints(0,2));
frame.add(lab2, new GBConstraints(1,2).insets(15, -15, 5, 5));
frame.add(but3, new GBConstraints(0,3).anchor(EAST));
frame.add(lab3, new GBConstraints(1,3).anchor(WEST));
Note that the second line above has two constraints added onto the GBConstraint; the flexibility is provided by classes Fill
, Anchor
, and GBConstraints
that were provided by @SplungeBob in this thread.
Upvotes: 2
Reputation: 47608
When using GridBagLayout, using relative positioning is always easier to maintain.
Now regarding the left/right positioning, you need to properly use
Both properties will help you in there (there is another option using fill and playing with text alignment on JLabel, but it's a lot less elegant because your layout depends on both component properties and layout properties):
And eventually the final outcome:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TestGridBagLayout {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestGridBagLayout().initUI();
}
});
}
private JLabel label_1 = new JLabel("Enter a sentence");
private JLabel label_2 = new JLabel("test");
private JLabel label_3 = new JLabel("test");
private JLabel label_4 = new JLabel("test");
private JTextField textArea = new JTextField(15);
private JButton button_1 = new JButton("button 1");
private JButton button_2 = new JButton("button 2");
private JButton button_3 = new JButton("button 3");
protected void initUI() {
JFrame frame= new JFrame(TestGridBagLayout.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
left.weightx = 1;
left.insets = new Insets(5, 5, 5, 5);
GridBagConstraints right = new GridBagConstraints();
right.weightx=1;
right.gridwidth = GridBagConstraints.REMAINDER;
right.anchor = GridBagConstraints.WEST;
right.insets = new Insets(5, 5, 5, 5);
panel.add(label_1, left);
panel.add(textArea,right);
panel.add(button_1 ,left);
panel.add(label_2, right);
panel.add(button_2,left);
panel.add(label_3,right);
panel.add(button_3,left);
panel.add(label_4,right);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 2
Reputation: 3884
You have several options available to you, depending on how you want this to look like, all explained in the already posted tutorial.
fill
field of the constraints to HORIZONTAL
and alignment of the labels to WEST
(Or LINE_START
, whichever you're used to), along with a positive weightx. This will make both the labels and buttons occupy all available horizontal space, and their alignment will mean the text will be on the left edge.anchor
field. This controls where a smaller component would be located in it's cell - you likely want a right or central alignment for the buttons, and left for the labels.BorderLayout
much?), or a more intelligent layout managerUpvotes: 3
Reputation: 23
Try to play with the following properties of GridBagConstraints: gridheight and weighty. I would try to modify this part of your code:
cons.gridx = 1;
cons.gridy = 2;
cons.weighty = 0;
add(label_2, cons);
For more information: http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Check this code, maybe it helps you:
public class GUITest {
private JPanel mainPanel;
private JLabel l1;
private JLabel l2;
private JLabel l3;
private JButton b1;
private JButton b2;
private JTextField text;
private JFrame guiFrame;
private Container pane;
public GUITest() {
// TODO Auto-generated constructor stub
guiFrame = new JFrame();
pane = new Container();
pane.setLayout(new BorderLayout());
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Example GUI");
guiFrame.setSize(300,250);
mainPanel = new JPanel(new GridBagLayout());
l1 = new JLabel("Label 1");
b1 = new JButton("Button 1");
l2 = new JLabel("Label 2");
b2 = new JButton("Button 2");
l3 = new JLabel("Enter a sentence");
text = new JTextField(50);
GridBagConstraints c = new GridBagConstraints();
c.gridheight = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
Insets i = new Insets(5, 5, 5, 5);
c.insets = i;
c.gridx= 0;
c.gridy = 0;
mainPanel.add(l3,c);
c.gridx = 1;
c.gridy = 0;
mainPanel.add(text, c);
c.gridx = 0;
c.gridy = 1;
mainPanel.add(b1,c);
c.gridx = 1;
c.gridy = 1;
mainPanel.add(l1,c);
c.gridx= 0;
c.gridy = 2;
mainPanel.add(b2,c);
c.gridx = 1;
c.gridy = 2;
mainPanel.add(l2, c);
pane.add(mainPanel,BorderLayout.NORTH);
guiFrame.setContentPane(pane);
guiFrame.setVisible(true);
}
public static void main(String[] args) {
GUITest g = new GUITest();
}
}
Upvotes: 1