Vanavah Edwards
asked on
How to position a frame (window) in the center of the screen
How do I position a frame (window) in the center of the screen using Java. Is there a built in Java system command.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This doesn't do a good job. This positions the frame at the bottom near right on my laptop screen.
ASKER
I apologize you are right. You have to put the contentPane.seLocation(lef t, top) after the contentPane.Pack() method for it work. Thank very much. I will award your points.
It is strange - check within your code - put System.out.println() - what are the values of
d.width and d.height
after
Dimension d = Toolkit.getDefaultToolkit(
I guess this should be about your resolution values your resolution
I normally do not go into these details - if it is a small frame I use
this.setLocation(400,400);
before
this.setVisible(true);
if it is bigger than
this.setLocation(100,100)
This posituions it ideally in the center
Make sure you call setSize() before thsi calculation
Make sure you call setSize() before thsi calculation
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FocusListenerTry extends JFrame implements FocusListener, KeyListener
{
private JTextField goodTry;
int countEvents = 0;
public FocusListenerTry() {
JPanel panel;
JTextField dummy;
panel = new JPanel();
goodTry = new JTextField(10);
dummy = new JTextField(10);
goodTry.addFocusListener(this);
goodTry.addKeyListener(this);
dummy.addKeyListener(this);
panel.setLayout(new FlowLayout());
panel.add(goodTry);
panel.add(dummy);
Container c = this.getContentPane();
c.add(panel);
this.setSize(150,100);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int left = (d.width - this.getWidth()) / 2;
int top = (d.height - this.getHeight()) / 2;
this.setLocation(left, top);
// this.setLocation(300,300);
this.setVisible(true);
}
public void focusGained(FocusEvent fe) {
}
public void focusLost(FocusEvent fe) {
if(countEvents > 0)return;
goodTry.removeFocusListener(this);
if(fe.getSource().equals(goodTry)){
countEvents++;
if(goodTry.getText().trim().length() == 0)return;
String text = goodTry.getText().trim();
if(text.indexOf("good") == -1){
System.out.println("Not good - try again!");
JOptionPane.showMessageDialog(this, "Not good - try again!");
goodTry.requestFocus();
return;
} else {
countEvents = 0;
}
}
}
public void keyReleased(KeyEvent ke){
}
public void keyTyped(KeyEvent ke){
if(ke.getSource().equals(goodTry)){
if(goodTry.getText().trim().length() == 0)return;
countEvents = 0;
goodTry.addFocusListener(this);
}
}
public void keyPressed(KeyEvent ke){
if(!(ke.getSource().equals(goodTry)) && countEvents > 0) {
JOptionPane.showMessageDialog(this, "Not good - try again!");
}
}
public static void main(String[] args) {
new FocusListenerTry();
}
}
ASKER
Did you see my last post, your first method works. Would it work for any regualr monitor and smart phones screens.
smart phonees I don't knwo - it may be different stuff, on regular computers it should work
ASKER
Thanks very much and goodbye.
ASKER