views:

61

answers:

2

I'm new to Swing. This is the code I've written

import java.awt.*;
import javax.swing.*;
public class NewMain {
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("test");
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        addItem(panel, new JLabel("Label"), 0, 0, GridBagConstraints.EAST);
        frame.add(panel);
    }

    private static void addItem(JPanel p, JComponent gc, int i, int i0, int align) {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = i;
        c.gridy = i0;
        c.anchor = align;
        p.add(gc,c);

    }

When I run the program, irrespective of what I pass as the align parameter (GridBagConstraints.NORTH or GridBagConstraints.SOUTH etc ...), my label is aligned in the center of the panel.

How do I change the alignment of the label?

Thanks in advance.

A: 

This is because the JLabel is the only component in your GUI. If you add more components it will be laid out relative to them in accordance with its position. Heres an extension of your prog with an empty panel added:

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


public class NewMain {

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("test");
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        JPanel emptyArea = new JPanel();
        emptyArea.setPreferredSize(new Dimension(200, 200));
        addItem(panel, new JLabel("Label"), 0, 0, GridBagConstraints.WEST);
        addItem(panel, emptyArea, 0, 0, GridBagConstraints.CENTER);
        frame.add(panel);
    }

    private static void addItem(JPanel p, JComponent gc, int i, int i0, int align) {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = i;
        c.gridy = i0;
        c.anchor = align;
        p.add(gc,c);

    }
}
DrDipshit
hey Thanks a lot!!
Tarun
To clarify what @DrDipShit said: your label is really aligned the way you required inside your panel. However, this is your panel which is centered inside the JFrame content pane. What you may try out is to do frame.setLayout(new GridBagLayout()) and replace panel with frame.getContentPane() everywhere.
jfpoilpret
^okay will try that, also wanted to ask , if i have to place my component at the top-left, will GridBagConstraints.NORTHWEST work?
Tarun
Yes it should work. That would mean "at the top-left" of its cell, not of the whole panel of course.
jfpoilpret
A: 

as the default for weightx in the GridBagConstraint is zero, the whole layout is being kept as small as possible and centered on the parent component. Use a non-zero, positive weightx to have the component using the whole horizontal length, so the layout will occupy all available space. Same for weighty for the vertical direction.

...
c.anchor = align;
c.weightx = 1.0:
...

Javadoc: weightx

Carlos Heuberger
thanks for the answer, will try that !
Tarun