Every time i make a new JFrame object, the frame appears in the 0,0 location. I have tried looking up how to center the window using setLocationRelativeTo(), but everything I’ve tried invoking on my container shows as non-compilable code. here is one of my classes that I am using:
package Ginfo;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EditUsers extends JFrame implements ActionListener
{
Container EU;
JPanel modUsers, button, output;
JLabel user, password;
JTextField tuser, tpassword;
JTextArea toutput;
JButton addAdmin, addStand, editPass, removeUser, exit;
ObjectOutputStream oout;
ObjectInputStream oin;
Message m;
ConnectInfo c;
public EditUsers (ObjectOutputStream oout2, ObjectInputStream oin2, Message m2, ConnectInfo a)
{
super("User Modifications");
EU = getContentPane();
oout = oout2;
try
{
oout.reset();
}
catch (IOException e)
{
e.printStackTrace();
}
oin = oin2;
m = m2;
c = a;
buildUserInfoPanel();
buildOutputPanel();
buildButtonPanel();
EU.add(modUsers, BorderLayout.NORTH);
EU.add(output, BorderLayout.CENTER);
EU.add(button, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildUserInfoPanel()
{
modUsers = new JPanel();
modUsers.setLayout(new GridLayout (2,2));
user = new JLabel ("Enter the username you wish to edit: ");
tuser = new JTextField (15);
password = new JLabel("Enter their new password: ");
tpassword = new JTextField(15);
modUsers.add(user);
modUsers.add(tuser);
modUsers.add(password);
modUsers.add(tpassword);
}
private void buildOutputPanel()
{
output = new JPanel();
toutput = new JTextArea();
toutput.setPreferredSize(new Dimension (400,100));
output.add(toutput);
}
private void buildButtonPanel()
{
button = new JPanel();
addAdmin = new JButton ("Add user as administrator");
addStand = new JButton ("Add user as standard");
editPass = new JButton ("Change password");
removeUser = new JButton ("Remove user");
exit = new JButton ("Back");
button.add(addAdmin);
button.add(addStand);
button.add(editPass);
button.add(removeUser);
button.add(exit);
addAdmin.addActionListener(this);
addStand.addActionListener(this);
editPass.addActionListener(this);
removeUser.addActionListener(this);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == addAdmin)
{
m.type = Message.ADDADMIN;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
m.setPermission = 1;
}
else if (e.getSource() == addStand)
{
m.type = Message.ADDSTANDARD;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
m.setPermission = 2;
}
else if (e.getSource() == editPass)
{
m.type = Message.CHANGEPASSWORD;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
}
else if (e.getSource() == removeUser)
{
m.type = Message.REMOVEUSER;
m.main = tuser.getText();
}
else if (e.getSource() == exit)
{
new WhatToDo (oout, oin, m, c);
dispose();
return;
}
try
{
oout.writeObject(m);
m = (Message)oin.readObject();
toutput.setText(m.response);
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
}
}
When I run this code, the window starts in the (0,0) location. what i wish to do, is re size the window to 1/4 the size of the resolution of the computer it’s running on, and to reset the location of the window so the center of the window is in the center of the screen. If resolution 1000×1000, window should be 250×250 and be located at (500 – 250/2)X,(500 – 250/2)Y (top left corner) by (500 + 250/2)X, (500 + 250/2)Y (bottom right corner)
(please note, i’m using a different class for each window I’m opening and closing it when the process is done, so without the other classes this will not be fully compilable. I am passing my ObjectOutputStream, ObjectInputStream, an object I call Message, and another object I call ConnectInfo. The output & input streams are to keep the connection alive to the server, Message is the serialized information being sent back and forth, and ConnectionInfo holds essencially cookie information (user connected, session info, permission level, etc).
Ok now i have
package Ginfo;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EditUsers implements ActionListener
{
Container EU;
JFrame myJFrame;
JPanel modUsers, button, output;
JLabel user, password;
JTextField tuser, tpassword;
JTextArea toutput;
JButton addAdmin, addStand, editPass, removeUser, exit;
ObjectOutputStream oout;
ObjectInputStream oin;
Message m;
ConnectInfo c;
public EditUsers (ObjectOutputStream oout2, ObjectInputStream oin2, Message m2, ConnectInfo a)
{
myJFrame = new JFrame("User Modifications");
EU = myJFrame.getContentPane();
oout = oout2;
try
{
oout.reset();
}
catch (IOException e)
{
e.printStackTrace();
}
oin = oin2;
m = m2;
c = a;
buildUserInfoPanel();
buildOutputPanel();
buildButtonPanel();
EU.add(modUsers, BorderLayout.NORTH);
EU.add(output, BorderLayout.CENTER);
EU.add(button, BorderLayout.SOUTH);
myJFrame.add(EU);
myJFrame.pack();
myJFrame.setVisible(true);
}
private void buildUserInfoPanel()
{
modUsers = new JPanel();
modUsers.setLayout(new GridLayout (2,2));
user = new JLabel ("Enter the username you wish to edit: ");
tuser = new JTextField (15);
password = new JLabel("Enter their new password: ");
tpassword = new JTextField(15);
modUsers.add(user);
modUsers.add(tuser);
modUsers.add(password);
modUsers.add(tpassword);
}
private void buildOutputPanel()
{
output = new JPanel();
toutput = new JTextArea();
toutput.setPreferredSize(new Dimension (200,100));
output.add(toutput);
}
private void buildButtonPanel()
{
button = new JPanel();
addAdmin = new JButton ("Add user as administrator");
addStand = new JButton ("Add user as standard");
editPass = new JButton ("Change password");
removeUser = new JButton ("Remove user");
exit = new JButton ("Back");
button.add(addAdmin);
button.add(addStand);
button.add(editPass);
button.add(removeUser);
button.add(exit);
addAdmin.addActionListener(this);
addStand.addActionListener(this);
editPass.addActionListener(this);
removeUser.addActionListener(this);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == addAdmin)
{
m.type = Message.ADDADMIN;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
m.setPermission = 1;
}
else if (e.getSource() == addStand)
{
m.type = Message.ADDSTANDARD;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
m.setPermission = 2;
}
else if (e.getSource() == editPass)
{
m.type = Message.CHANGEPASSWORD;
m.main = tuser.getText();
m.setPassword = tpassword.getText();
}
else if (e.getSource() == removeUser)
{
m.type = Message.REMOVEUSER;
m.main = tuser.getText();
}
else if (e.getSource() == exit)
{
new WhatToDo (oout, oin, m, c);
myJFrame.dispose();
return;
}
try
{
oout.writeObject(m);
m = (Message)oin.readObject();
toutput.setText(m.response);
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
}
}
now the problem is with myJFrame.add(EU) it says “java.lang.IllegalArgumentException: adding container’s parent to itself”. Thanks for any help in advance.
Try
myJFrameObject.setLocationRelativeTo(null);from JavaDoc
Edit
I just tested your code on this (less advanced) constructor and it seems to works like you wanted (at least thats what I hope). I placed
setLocationRelativeTo(null)just beforesetVisible(true)to make sure Frame will contains all its elements and know what size to use when centering.