Positioning in java swing
I have some problems positioning my label / password field.
With this code, they are both centered next to each other, while I really want them to be in the middle of my panel, one above the other.
Does anyone know how I am supposed to do this?
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);
}
}
To get the layout of the layouts, I would recommend reading my article on them ( http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/ ). This is old, but the details and methods of the FlowLayout are detailed.
What do you mean by "over each other"?
If you mean like
Password
<field>
EDIT: I REMEMBERED THE EASY WAY TO DO THIS (completely in JDK / JRE) ... (This is similar to what I am doing with BoxBeans below, but you don't need BoxBeans. I created BoxBeans to be able to use BoxLayout in the 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 small bar at the top of the general user interface that contains the password and field.
Note that the nested BorderLayout ensures that both the label and box get their preferred size. You will need to call setColumns on the field for the number of characters you want to display.
If you want to center the label / box vertically as well, you can 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 will add a version using BoxLayout (but this is a bit tricky due to preferred sizing 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 am 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 of VAJ. For example, I just tried this in eclipse.
- AFAICS, you cannot set the jframe layout directly to the BoxLayout, so I added an extra panel in between. There's a check in BoxLayout that has problems with auto-indirection of the content area.
- I nested the BoxLayouts so that the horizontal centering (stuffH panel) contains vertical centering (stuffV panel). They are centered by surrounding them with "Glue" components, which are simply components that allow them to expand.
-
I had to put the label and box in a BoxAdapter that limits their maximum size to their preferred size. If you don't want to use BoxAdapter, you can achieve the same effect using the following for the box 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 is helpful for you and everyone else! - Scott
a source to share