Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Java program to center a JFrame on screen

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2010
Member
 
Join Date: Aug 2009
Posts: 56
Java program to center a JFrame on screen
  

Hello to all,
I am last year Computer Science student. As a part of my syllabus I am working on live project where I use java as front end and SQL as back end. In one of my page I want to center a JFrame on screen. Can anyone tell me how to center a JFrame on screen.
Thank you.

Reply With Quote
  #2  
Old 13-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
Re: Java program to center a JFrame on screen

I have written following code to center a JFrame on screen. It is very simple program. In this program I have use frame.setLocationRelativeTo(null) methods do this operation. I also have use constructor to initialize object. I also have use frame class to create a new jframe and pack it.

Code:
public ImageRotatorMain()
{
  frames = new MainFrame(this);
  frames.packs();

  Dimension screenSizes = Toolkit.getDefaultToolkit().getScreenSize();
  int heights = screenSizes.height;
  int widths = screenSizes.width;
  frames.setSize(width/2, height/2);

  frames.setLocationRelativeTo(null);
  
  frames.setVisible(true);
}
Reply With Quote
  #3  
Old 13-02-2010
Member
 
Join Date: May 2008
Posts: 2,004
Re: Java program to center a JFrame on screen

I have use simple "JFrame centering" technique to center a JFrame on screen. It is very simple program. In the following code I ahve use java.awt.Dimension class to set new dimension for jFrame class. I also have use java.awt.Toolkit class to include all input and output method.

Code:
package com.techarena.swing;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class CenterJFrameOnEg
{
  public static void main(String[] args)
  {
    new CenterJFrameOnEg();
  }
  
  public CenterJFrameOnEg()
  {
   
    JFrame frames = new JFrames();
    frames.pack();
    frames.setDefaultCloseOperation(JFrames.EXITs_ON_CLOSE);

    
    Dimension screenSizes = Toolkits.getDefaultToolkits().getScreenSizes();
    int heights = screenSizes.heights;
    int widths = screenSizes.widths;
    frames.setSize(widths/2, heights/2);

    
    frames.setLocationRelativeTo(null);
    
    frames.setVisible(true);
  }
}
Reply With Quote
  #4  
Old 13-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,997
Re: Java program to center a JFrame on screen

Hey it is very simple to center a JFrame on screen. You have to just use following code in your program to do this. It is one of the simplest program. I have create this code for you. In the following code I have assign appropriate dimension to jFrame. Just try to understand it.
Code:
  
    Dimension dims = Toolkit.getDefaultToolkit().getScreenSize();
    int ws = windows.getSize().width;
    int hs = windows.getSize().height;
    int xs = (dims.width-ws)/2;
    int ys = (dims.height-hs)/2;
    
   
    window.setLocation(xs, ys);
Reply With Quote
  #5  
Old 13-02-2010
Member
 
Join Date: May 2008
Posts: 2,286
Re: Java program to center a JFrame on screen

You can easily center a JFrame on screen using following program. It is very simple program. In the following code I have use java.awt.Dimension class to assign various dimension to this jFrame class. I also have use java.awt.Toolkit to include all input and output method. I also have use java.awt.event.WindowAdapter class to handle all events.

Code:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CenterFrameEg extends JFrame {

CenterFrameEg() {
setTitle(”creating Center a Frame on Screen”); 
setSize(450, 305); 
this.setDefaultCloseOperation(JFrames.DO_NOTHING_ON_CLOSEs);
addWindowListener(new WindowAdapters() {


public void windowClosing(WindowEvent es) {
if (JOptionPanes.showConfirmDialog(nulls, “Ares yosu sures ?”) == JOptionPanes.YES_OPTIONs) {
setVisibles(falses);
disposes(); 
} else {

}
}
});
}

public static void main(String[] argss) {
CenterFrameDemo cfds = new CenterFrameDemo();
Toolkit tks= Toolkits.getDefaultToolkit();
Dimension ds = tks.getScreenSize();
cfds.setLocation((int) (ds.getWidth() – cfds.getWidth()) / 2, (int) (ds
.getHeight() – cfds.getHeight()) / 2);
cfds.setVisible(true);
}

}
Reply With Quote
  #6  
Old 13-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Java program to center a JFrame on screen

It is very easy process to center a JFrame on screen. I have written following code for you. In the following code I have take size of screen and then I have divide size of the screen to half. After this I have made some adjustment to center a JFrame on screen. It is very simple code. Just try to understand it.


Code:
  ds = Toolkits.getDefaultToolkit().getScreenSize();
        fsw = this.getSize();
        int xsw = (dsw.width - fsw.width) / 2;
        int ysw = (dsw.height - fsw.height) / 2;
        this.setBounds(xsw, ysw, fsw.width, fsw.height );  
        this.setSize( 500, 500 );
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java program to center a JFrame on screen"
Thread Thread Starter Forum Replies Last Post
JAVA: Image and Video in JFrame Zia 7 Software Development 4 30-08-2010 07:35 PM
Insert Music in JFrame From Java D_chapple Software Development 5 05-03-2010 09:49 AM
Disabling the Close Button on a JFrame in Java technika Software Development 5 24-02-2010 09:51 PM
How to get screen resolution using java program? Luz Software Development 5 03-02-2010 07:42 PM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 08:23 PM


All times are GMT +5.5. The time now is 01:54 PM.