We help IT Professionals succeed at work.

How to position a frame (window) in the center of the screen

Vanavah Edwards
on
How do I position a frame (window) in the center of the screen using Java.  Is there a built in Java system command.  
Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
look at these suggestions:

http://www.javaprogrammingforums.com/awt-java-swing/867-frame-center-position.html

   Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int left = (d.width - this.getWidth()) / 2;
        int top = (d.height - this.getHeight()) / 2;
        this.setLocation(left, top);

Open in new window



setLocationRelativeTo (null);

Author

Commented:
I weill try this now and get back to you.

Author

Commented:
This doesn't do a good job.  This positions the frame at the bottom near right on my laptop screen.

Author

Commented:
I apologize you are right.  You have to put the contentPane.seLocation(left, top) after the contentPane.Pack() method for it work.  Thank very much.  I will award your points.  
Awarded 2011
Awarded 2011

Commented:

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().getScreenSize();


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)


Awarded 2011
Awarded 2011

Commented:
This posituions it ideally in the center

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();
    }

}

Open in new window

Author

Commented:
Did you see my last post, your first method works.  Would it work for any regualr monitor and smart phones screens.
Awarded 2011
Awarded 2011

Commented:
smart phonees  I don't knwo - it may be different stuff, on regular computers it should work

Author

Commented:
Thanks very much and goodbye.