Center JFrame on the screen

I've been trying to figure this out using every different example I find online, but nothing is working. I'm using version 1.4.2 so I know it's a bit different than version 1.6. Currently, when I open my application it'll open to bottom right corner of the screen at an 800x600 size. I want it at the 800x600, but I think what's happening is it is opening at 0x0 and is centered, but then is resizing to 800x600 which no longer makes it center. Since it's version 1.4.2 I can't set a prefered size either.
I've tried adding:
this.setLocationRelativeTo(null);
this.setVisible(true);but that isn't working either.

another way:
        Dimension s, c = Toolkit.getDefaultToolkit().getScreenSize();
        s = frame.getSize();
        frame.setLocation((c.width - s.width) / 2,(c.height - s.height) / 2);

Similar Messages

  • Resizing and/or Moving the JFrame on the Screen

    Is there any way to resize a JFrame when the undecorated property is set to true.
    I would like to have a JFrame that is both undecorated and resizable.
    Similarly, I was also wondering how to move the JFrame on the screen if the
    undecorated property is set to true since there is not Border to select the
    JFrame to initiate the Move.
    Thanks!

    yes... setSize(), setLocation().
    And if you want the user to be able to do it with the mouse, you need to add a border to the rootpane to prevent anyone adding things over the whole rootpane, and add a mouse listener to it to catch mouse click and drag events and do the calculations.
    If you look at the BasicInternalFrameUI, or some thing in there should have some code that does the same thing.

  • Move a JFrame over the screen by specifying coordinates

    Hi,
    I have a JPanel within a JFrame.
    All I wanted to do is this. I have an image loaded in that JPanel
    using MediaTracker. I can move that image anywhere inside the JPanel.
    Now ,I want to "simulate" what is happening within this JPanel in the
    entire Screen. That is, whenvever I move the image inside the JPanel,
    there should be a (different) JFrame application on the screen and it should move according to the "relative" screen coordinates.
    I thought I could use SwingUtilities.convertPointToScreen()
    function , but really dont know how to proceed exactly.ie., how to
    move a JFrame application anywhere in the screen not manually but
    moving it based on the relative coordinates I retrieve from the JPanel.
    The "simulation" here is that a JFrame in the screen
    is equated to an image in a JPanel.
    Any help would be REALLY appretiated.
    Thanks,
    Pullu

    Don't know what you're really looking for, but try:
    Rectangle parentRect=SwingUtilities.getRoot(myJPanel).getBounds();
    and see if it gives you the info you need.
    ;o)
    V.V.

  • Cant hear sound from the long center speaker under the screen on pavilion dv6

    can't heaar sound from the long center speaker under the screen on pavilion dv6

    carlosherrera_1 wrote:
    Look at p.12, items #2.  I am talking about those speakers.
    Hi,
    Sorry I misunderstood, central (center) speaker is the one sit at the middle, we normally call  Left, Right and Center speakers. Anyway, what is your speaker icon (bottom right corner) says ? Does it look like this ?
    1. If NO, double click it & turn it ON
    2. If YES, right click it, select "Playback devices" and turn it ON, it should look like this:
    3. Otherwise, please post back
    Regards,
    BH
    **Click the KUDOS thumb up on the left to say 'Thanks'**
    Make it easier for other people to find solutions by marking a Reply 'Accept as Solution' if it solves your problem.

  • Locking a JFrame in the screen

    Help!!!!!, How can i lock a JFrame in the screen???
    I mean, no matter what others applications are open (or start the user after my java app) my frame will be at the front of all always.

    thats what I though too, but I has never done that.
    Do you know where could i find some documentation or code examples of programs than manipulate the OS in java???

  • How to centre a JFrame on the screen?

    hi,
    i'm just wondering how i could place a JFrame at the centre of the screen when i run the application?
    thank you very much!

    something like this should do it:
         int ix, iy;
         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
         Dimension dd = getSize();
         ix = (d.width-dd.width)/2;
         iy = (d.height-dd.height)/2;
         if(iy < 10) iy = 10;
         if(ix < 10) ix = 10;
         setLocation(ix,iy);If you have 1.4 then there are some additional stuff like the start menu on Windows that you might want to deal with.
    Mark.

  • I need my Popup Window to be at the Center of the Screen - help

    Please tell me how can I popup a new window at the center of the screen. I know to position at particular x,y point, using top, left parameters in the window.open() function, but I need to get the center point of the screen. I need to get maxX and maxY value.
    Thanks,
    Guhan

    use this javascript function in ur code,it will work
    function alignToCenter(w, h)     {
         var e = openWin(w, h);
         var x = (screen.width-w)/2;
         var y = (screen.height-h)/2;
         e.moveTo(x,y);
    }

  • Making A Xterm Use All Of The Screen In RatPoison

    After asking for some suggestions, I just gave RatPoison a try and I really like it. I do have a quick question that I hope someone will be able to help me with.
    When I run an Xterm, I can still see some of the background on the right and the bottom, like this:
    Is there a way to make this not happen? Or at least center perfectly on the screen?
    By the way, is it a bad idea to change the key bindings for switching windows in RatPoison? I am so used to ALT-Tab that I am struggling.
    Thank you in advance!

    As far as I remember it's a combination of your font size and the windows gravity.  And as far as alt-tab goes, not at all.  That's actually the way I've got mine setup.  Alt-tab goes one way, and (whatever the name of that windows key is)-tab goes the other.

  • How to place the JFrame in center of the screen

    Hi.. please any one tell me how to place the jframe in the center of the screen.. i am working on windows.
    setLocationByPlotform is not working...

    //GETS SCREEN SIZE
            Dimension screen_Dimension =Toolkit.getDefaultToolkit ().getScreenSize ();
            //GETS WINDOWS SIZE
            Dimension frame_Dimension = window.getSize ();
            //PUTS FRAME IN THE "MIDDLE" OF THE SCREEN
            window.setLocation ( new Point (
                    (screen_Dimension.width / 2-frame_Dimension.width / 2),
                    (screen_Dimension.height / 2-frame_Dimension.height/ 2)));Not sure if you want the raw version of it.......

  • How to center a JFrame object on the screen?

    Does somebody know how to center a JFrame object on the screen. Please write an example because I'm new with java.
    Thank you.

    //this will set the size of the frame
    frame.setSize(frameWidth,frameHeigth);
    //get screen size
    Toolkit kit=Toolkit.getDefaultToolkit();
    //calculate location for frame
    int x=(kit.getScreenSize().width/2)-(frameWidth/2);
    int y=(kit.getScreenSize().height/2)-(frameHeigth/2);
    //set location of frame at center of screen
    frame.setLocation(x,y);

  • How do I make a JFrame appear in the middle of the screen

    When I use the setVisible method on a GUI (object which extends JFrame), it always pops up in the top left corner of the screen. What can I do to center it in the middle?

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);

  • Displaying a JFrame at the centre of the screen

    Hi,
    I ve developed a GUI,
    i want it to display at center of the screen each time i run the application,
    i ve tried several options, but none of them works..
    first i set the location of jframe..didnt work!!
    {color:#ff0000}
    this.setLocation(500,500);{color}
    then i used this.. not working..
    {color:#ff0000}
    this.setLocationRelativeTo(null);{color}
    then i wrote separate method n call it in my initialize().. still not working
    {color:#ff0000}
    Toolkit tk = Toolkit.getDefaultToolkit ();
    Dimension screen = tk.getScreenSize ();
    int lx = (int) (screen.getWidth () * 3/8);
    int ly = (int) (screen.getHeight () * 3/8);
    f.setLocation (lx,ly);{color}
    if anyone can help me, pls reply..
    thanks,
    dush

    hi,
    here is the complete code,
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JSplitPane;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import java.awt.Dimension;
    import java.awt.*;
    public class index extends JFrame {
         private static final long serialVersionUID = 1L;
         private JSplitPane jSplitPane = null;
         private JSplitPane jSplitPane1 = null;
         private JPanel jPanel = null;
         private JButton jButton = null;
         private JButton jButton1 = null;
         private JLabel jLabel = null;
         public index() {
              super();
              initialize();
         private void initialize() {
              this.setLocation(500,500);
              this.setContentPane(getJSplitPane());
              this.setLocationRelativeTo(null);
              this.setBounds(new Rectangle(500, 500, 462, 305));
              this.setTitle("GUI");
              this.setSize(new Dimension(462, 305));
              this.setResizable(false);
              this.setPreferredSize(new Dimension(350, 500));
              this.setVisible(true);
              centerFrame (this);
         private JSplitPane getJSplitPane() {
              if (jSplitPane == null) {
                   jSplitPane = new JSplitPane();
                   jSplitPane.setDividerLocation(150);
                   jSplitPane.setDividerSize(0);
                   jSplitPane.setRightComponent(getJSplitPane1());
              return jSplitPane;
         private JSplitPane getJSplitPane1() {
              if (jSplitPane1 == null) {
                   jSplitPane1 = new JSplitPane();
                   jSplitPane1.setDividerLocation(250);
                   jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
                   jSplitPane1.setTopComponent(getJPanel());
                   jSplitPane1.setDividerSize(0);
              return jSplitPane1;
         private JPanel getJPanel() {
              if (jPanel == null) {
                   jLabel = new JLabel();
                   jLabel.setBounds(new Rectangle(66, 39, 179, 38));
                   jLabel.setText("  GUI ");
                   jPanel = new JPanel();
                   jPanel.setLayout(null);
                   jPanel.add(getJButton(), null);
                   jPanel.add(getJButton1(), null);
                   jPanel.add(jLabel, null);
              return jPanel;
         private JButton getJButton() {
              if (jButton == null) {
                   jButton = new JButton();
                   jButton.setBounds(new Rectangle(98, 92, 104, 50));
                   jButton.setIcon(new ImageIcon(getClass().getResource("/images/mail.jpg")));
                   jButton.setText("GUI");
                   jButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                             first ff=new first();
                             ff.setVisible(true);
                             dispose();
              return jButton;
         private JButton getJButton1() {
              if (jButton1 == null) {
                   jButton1 = new JButton();
                   jButton1.setText("Exit");
                   jButton1.setBounds(new Rectangle(134, 184, 55, 26));
                   jButton1.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                             System.exit(0);
              return jButton1;
          void centerFrame (JFrame f) {
                  // Need the toolkit to get info on system.
                  Toolkit tk = Toolkit.getDefaultToolkit ();
                  // Get the screen dimensions.
                  Dimension screen = tk.getScreenSize ();
                  // Make the frame 1/4th size of screen.
                  int fw =  (int) (screen.getWidth ()/3);
                  int fh =  (int) (screen.getWidth ()/4);
                  f.setSize (fw,fh);
                  // And place it in center of screen.
                  int lx =  (int) (screen.getWidth ()  * 3/8);
                  int ly =  (int) (screen.getHeight () * 3/8);
                  f.setLocation (lx,ly);
                } // centerFrame
    }  //  @jve:decl-index=0:visual-constraint="56,39"Edited by: dush82 on Oct 3, 2007 11:02 PM
    Edited by: dush82 on Oct 3, 2007 11:10 PM

  • Application startup at the center of the screen

    Is there any method in the Java API that will startup a window at the center of the screen?
    Is not, how do I get the desktop resolutions?
    Thanks in advance.

    JFrame frame = new JFrame("Hey, I'm a JFrame") ;
    Rectangle rect = new Rectangle(600, 400) ;
    frame.setBounds(rect) ;
    Dimension screen = Toolkit.DefaultToolkit().getScreenDimension() ;
    int xPos = Integer.parseInt(String.valueOf(Math.round((screen.getWidth() - frame.getWidth())/2))) ;
    int yPos = Integer.parseInt(String.valueOf(Math.round((screen.getHeight() - frame.getHeight())/2))) ;
    frame.setLocation(xPos, yPos) ;There you go ...

  • How I can get a JFrame to open in the middle of the screen?

    Okay, I have been looking for an answer on how I can make a JFrame open in the center of the screen like the JOptionpane's "null" argument does. Maybe, I am putting the wrong words in the search but I can't find any help on this.
    Thanks
    ***Sample***
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    public class JFrameProb extends JFrame
         private JLabel someText;
         private JLabel someText2;
         private JLabel someText3;
         public JFrameProb()
              super("Why is this always going to the Corner?");
              setLayout( new FlowLayout() );
              someText = new JLabel("This Window Popped open in the top-left Corner...");
              add(someText);
              someText2 = new JLabel("Is there a way to open in the Center of the Screen?");
              add(someText2);
              someText3 = new JLabel("I know JOptionPane does.");
              add(someText3);
    }

    JosAH,
    I tried using your code but it errored out:
    C:\Program Files\Java\jdk1.5.0\PimpThatHo\PimpNameGetter.java:78: cannot find symbol
    symbol : variable Centralizer
    location: class PimpNameGetter.TextFieldHandler
    Centralizer.centralize(comboBox);
    ^
    1 error
    Process completed.
    So I made a java file for the Cebtralizer Class but it gave me this:
    --------------------Configuration: <Default>--------------------
    C:\Program Files\Java\jdk1.5.0\PimpThatHo\Centralizer.java:3: cannot find symbol
    symbol : class Component
    location: class Centralizer
    public static void centralize(Component c) {
    ^
    C:\Program Files\Java\jdk1.5.0\PimpThatHo\Centralizer.java:5: cannot find symbol
    symbol : class Dimension
    location: class Centralizer
    Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize();
    ^
    C:\Program Files\Java\jdk1.5.0\PimpThatHo\Centralizer.java:5: cannot find symbol
    symbol : variable Toolkit
    location: class Centralizer
    Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize();
    ^
    C:\Program Files\Java\jdk1.5.0\PimpThatHo\Centralizer.java:6: cannot find symbol
    symbol : class Dimension
    location: class Centralizer
    Dimension compSize= c.getPreferredSize();
    ^
    4 errors
    Process completed.
    I know I am missing packages but I don't know which ones.
    Roman03_

  • Positioning a JFrame on the center!!

    how do i posiiton a JFrame on the center of a screen???

    this is a method for centering the frame in the middle. Have a look at it
      private void centerFrame () {
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        this.setLocation( (screenSize.width - frameSize.width) / 2,
                         (screenSize.height - frameSize.height) / 2);
        this.setVisible(true);
      }

Maybe you are looking for

  • How to sync iphone with a different itunes and different apple ID without erasing

    I have 2 iphones and both have different contents on them, and both are synced to one apple id and itunes account. What im looking to do is "unlink" one of my iphones and connect that to a different apple id and itunes account on a different computer

  • I am using Acrobat 9 Pro.  Update 9.2.0 - CPSID_50026

    I installed this update yesterday and since then I have not been able to use Acrobat. I cannot uninstall the UPdate (help anyone?) and I cannot uninstall Acrobat either . Help again please? Thanks for any guisdance on this.

  • Workflow not working correctly when removing resource attributes

    (Comment: I have to say that posting to this forum is a real pita. It takes me upwards of 20 minutes to figure out why various words are not allowed, with no indication of what is wrong. For some reason ba lt im ore is a forbidden word so I replaced

  • Error in flat file data loading

    Hi All, I have a flat file which has 20000 records, when I tried to do Read Preivew data in Data source creation Iam getting the following error. I have the field name GROSSMARGIN in flat file I have given the properties of Grossmargin as a keyfigure

  • Error Report - Skype is dropping calls

    I've been having this problem since Microsoft took over the company.  I use the iPhone version of Skype and have been paying for an unlimited membership for over a year now. I will call someone, using my paid Skype- landline service, the phone will r