views:

857

answers:

2

I have some troubles with positioning my label/password field.
With this code they both get positioned in the center next to each other, while I actually want them in the middle of my panel on top of each other.

Does anyone know how I should do that?

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class Paneel_Pincode extends JPanel {


    Paneel_Pincode() {
        setLayout(new FlowLayout());

        JPasswordField pincode = new JPasswordField(15);
        pincode.setLocation(500, 500);
        JLabel pinInvoer = new JLabel();

        ImageIcon pin1 = new ImageIcon("images/voerPincodeIn.jpg");

        pinInvoer.setIcon(pin1);
        pinInvoer.setLocation(500,700);

        add(pincode);
        add(pinInvoer);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000,1000);
        f.setLocationRelativeTo(null);

        f.add(new Paneel_Pincode());
        f.setVisible(true);
    }

}
+6  A: 

To get the hang of layouts, I'd recommend reading my article on them (http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/). It's old, but the concepts and how FlowLayout works are detailed.

What do you mean by "on top of each other"?

If you mean like

   Password
   <field>

EDIT: I REMEMBERED AN EASIER WAY TO DO THIS (completely in the JDK/JRE)... (This is similar to what I'm doing with the BoxBeans below, but you don't need the BoxBeans. I created BoxBeans to be able to use BoxLayout in a UI builder a long time ago...)

JLabel label = new JLabel("Password") {
    @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
    }
};
JPasswordField field = new JPasswordField() {
    @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
    }
};
field.setColumns(10);
Box verticalBox = Box.createVerticalBox();
verticalBox.add(Box.createVerticalGlue());
verticalBox.add(label);
verticalBox.add(field);
verticalBox.add(Box.createVerticalGlue());
//
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(Box.createHorizontalGlue());
horizontalBox.add(verticalBox);
horizontalBox.add(Box.createHorizontalGlue());
add(horizontalBox);

Previous answer for reference...

I DO NOT RECOMMEND THE FOLLOWING BUT IT MAY HELP OTHER READERS WITH IDEAS

You can do something like

setLayout(FlowLayout());
JPanel group = new JPanel(new BorderLayout());
group.add(new JLabel("Password"), BorderLayout.NORTH);
group.add(passwordField, BorderLayout.SOUTH);
add(group);

This will create a little panel in the top-center of the overall UI that contains the Password and field.

Note that the nested BorderLayout ensures that the label and field each get their preferred size. You'll need to call setColumns on the field to the number of chars you'd like displayed.

If you want to center the label/field vertically as well, you could do the following

setLayout(new GridBagLayout());
//
add(new JLabel("Password"), 
    new GridBagConstraints(0,0,1,1,1,1,
        GridBagConstraints.SOUTH,GridBagConstraints.NONE, 
        new Insets(3,3,3,3), 0,0));
field.setColumns(10);
add(field, new GridBagConstraints(0,1,1,1,1,1,
    GridBagConstraints.NORTH,GridBagConstraints.NONE, 
    new Insets(3,3,3,3), 0,0));

I hate using GridBagLayout in general, so I'll add a version using BoxLayout (but it's a bit trickier due to the preferred size settings)

    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    //
    JPanel stuffH = new JPanel();
    f.add(stuffH, BorderLayout.CENTER);
    stuffH.setLayout(new BoxLayout(stuffH, BoxLayout.X_AXIS));
    //
    JPanel stuffV = new JPanel();
    stuffV.setLayout(new BoxLayout(stuffV, BoxLayout.Y_AXIS));
    //
    JLabel label = new JLabel("Password");
    BoxAdapter labelAdapter = new BoxAdapter();
    labelAdapter.add(label);
    JPasswordField field = new JPasswordField();
    field.setColumns(10);
    BoxAdapter fieldAdapter = new BoxAdapter();
    fieldAdapter.add(field);
    //
    stuffV.add(new VerticalGlue()); // for vertical spacing
    stuffV.add(labelAdapter);
    stuffV.add(fieldAdapter);
    stuffV.add(new VerticalGlue()); // for vertical spacing
    //
    stuffH.add(new HorizontalGlue()); // for horizontal spacing
    stuffH.add(stuffV);
    stuffH.add(new HorizontalGlue()); // for horizontal spacing
    //
    f.setVisible(true);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

A few notes on this:

  • I'm using my BoxBeans helper classes - see http://javadude.com/tools/boxbeans. This page is based on VisualAge for Java, but the jar at the bottom of the page can be used outside VAJ. I just tried it in eclipse, for example.
  • AFAICS, you cannot set a jframe's layout directly to BoxLayout, so I added an extra panel in between. There's a check in BoxLayout that has trouble with the automatic indirection of the content pane.
  • I nested the BoxLayouts so there's horizontal centering (the stuffH panel) containing a vertical centering (the stuffV panel). They are centered by surrounding them with "Glue" components, which are simply components that allow themselves to expand.
  • I had to put the label and field in a BoxAdapter which limits their maximum size to their preferred size. If you don't want to use BoxAdapter, you can acheive the same effect by using the following for the field and label:

    JLabel label = new JLabel("Password") {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    JPasswordField field = new JPasswordField() {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    

Hope this proves helpful to you and anyone else! -- Scott

Scott Stanchfield
I came here to recommend layouts, but I see you've beaten me to it. +1
Paul Tomblin
Yes, that is what I ment with on top of eachother. And I indeed want to center them vertically as well :)Tried it with both setLocation and setBounds, but both didn't work
Yes this is working great :D Thanks a lot for your help and time. I've never worked with gridbag before, so i'll study on it now so I can use it with my other panels as well. Again, thanks a lot :)
make sure to read the article I wrote -- it'll really help make layout management click for you. glad I could help!
Scott Stanchfield
don't make me shudder... make sure you learn how to nest layouts and use combinations of simpler layouts rather than grid bag. for this example you can also use nested box layouts.
Scott Stanchfield
I should mention that the Big Bad Evil BarfBagLayout always makes me shudder...
Scott Stanchfield
+1  A: 

I would recommend the JGoodies FormLayout. Once you learn it, it's quite powerful and easy to do by hand coding.

Joshua