Centering a Jpanel on screen

Hi.

I'd like to center my JPanel to the center of the screen.

I've looked around, however i can't find out how to do it.

I found about setting the "bounds", however that is based on x, y Co-ordinates, and i'd like it to be centered for people on 800 x 600 resolutions, as well as 1024 x 768 (for example).

The other way i found is by:

Login.setVerticalAlignment(JFrame.CENTER);

However that results in an error during compilation:

Compiling 1 source file to W:\Java\Covenant\build\classes

W:\Java\Covenant\src\covenant\Login.java:81: cannot find symbol

symbol : variable CENTER

location:class javax.swing.JFrame

Login.setHorizontalAlignment(JFrame.CENTER);

W:\Java\Covenant\src\covenant\Login.java:82: cannot find symbol

symbol : variable CENTER

location:class javax.swing.JFrame

Login.setVerticalAlignment(JFrame.CENTER);

2 errors

BUILD FAILED (total time: 0 seconds)

So my question is, how can i do it?

Thanks for any help.

[1118 byte] By [allydma] at [2007-11-26 21:38:54]
# 1
assuming your JPanel is the contentPane,AFTER setting the contentPane's size, either setSize(..) or pack()contentPane.setLocationRelativeTo(null);
Michael_Dunna at 2007-7-10 3:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

You could try something like this.

JFrame frame = new JFrame("Test");

frame.setSize(800, 300);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

int x = Math.max(( screenSize.width / 2 - frame.getSize().width / 2),0);

int y = Math.max((screenSize.height / 2 - frame.getSize().height / 2),0);

frame.setLocation(x,y);

frame.getContentPane().add(new JButton("test"));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

dmontia at 2007-7-10 3:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi.

I have tried both the suggestions, however the window dosn't appear at the center, more towards the bottom right hand corner. I'd say about half way between the center of the screen and the bottom right hand corner.

Do you know why this may be? It has happened with both ways posted above.

Thankyou :)

allydma at 2007-7-10 3:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 4
> Do you know why this may be?not without you posting your code, but a guess - check you don't have anothersetLocation(), or setBounds() later in your code[edit]guess #2 - did you size the frame first?
Michael_Dunna at 2007-7-10 3:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

I havn't got another setLocation() or setBounds() in my code, and i havn't sized my frame.

The code is:

public Login() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

Title = new javax.swing.JLabel();

Username = new javax.swing.JTextField();

UsernameLabel = new javax.swing.JLabel();

PasswordLabel = new javax.swing.JLabel();

Password = new javax.swing.JPasswordField();

LoginButton = new javax.swing.JButton();

FooterText = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("Login");

setBounds(new java.awt.Rectangle(0, 0, 0, 0));

setLocationRelativeTo(null);

setResizable(false);

Title.setFont(new java.awt.Font("Tahoma", 1, 12));

Title.setText("The Covenant Farm Program");

Username.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

UsernameActionPerformed(evt);

}

});

UsernameLabel.setText("Username:");

PasswordLabel.setText("Password:");

LoginButton.setText("Login");

LoginButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

logincheck(evt);

}

});

FooterText.setText("You must be a Covenant Member to use this Program");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(65, 65, 65)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(UsernameLabel)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addComponent(PasswordLabel)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(LoginButton)

.addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addGroup(layout.createSequentialGroup()

.addGap(18, 18, 18)

.addComponent(FooterText))

.addGroup(layout.createSequentialGroup()

.addGap(57, 57, 57)

.addComponent(Title)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(Title, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(17, 17, 17)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(UsernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(14, 14, 14)

.addComponent(LoginButton)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(FooterText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}

Thanks for the help :)

allydma at 2007-7-10 3:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 6
setBounds(new java.awt.Rectangle(0, 0, 0, 0));setLocationRelativeTo(null);Remove the first line. Move the second line to the end of the method (after everything has been created).
kirillga at 2007-7-10 3:22:19 > top of Java-index,Desktop,Core GUI APIs...