Skip to main content



      Home
Home » Archived » Visual Editor (VE) » Center JFrame?
Center JFrame? [message #114685] Thu, 05 January 2006 19:25 Go to next message
Eclipse UserFriend
Originally posted by: david.welford-costelloe.com

Hi,
Been trying to get this to work as per code below:
No matter what I try I can't get the JFrame to center on the screen.


private void initialize() {
// Configure Platform GUI
SetUpGUIApplication();
this.setJMenuBar(getJJMenuBar());
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getCl ass().getResource( "/resources/FLGCAN.png")));
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLO SE);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("My Sample App");
this.setVisible(true);
this.setSize(482, 342);
this.setLocationRelativeTo(null);


}



private static void SetUpGUIApplication(){
String errMsg = null;
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClass Name());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFe elClassName());
// javax.swing.SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
errMsg = "Error invoking PlatForm Look and Feel";
JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());

}

}

Any help would be appreciated.

Thanks
Re: Center JFrame? [message #114709 is a reply to message #114685] Thu, 05 January 2006 19:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

No where in this code are you doing a this.setLocation(). Without that
you can't center it. You need to calculate the center of the screen and
then do a setLocation to it.

David Costelloe wrote:
> Hi,
> Been trying to get this to work as per code below:
> No matter what I try I can't get the JFrame to center on the screen.
>
>
> private void initialize() {
> // Configure Platform GUI
> SetUpGUIApplication();
> this.setJMenuBar(getJJMenuBar());
> this.setIconImage(Toolkit.getDefaultToolkit().getImage(getCl ass().getResource( "/resources/FLGCAN.png")));
> this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLO SE);
> this.setResizable(false);
> this.setContentPane(getJContentPane());
> this.setTitle("My Sample App");
> this.setVisible(true);
> this.setSize(482, 342);
> this.setLocationRelativeTo(null);
>
>
> }
>
>
>
> private static void SetUpGUIApplication(){
> String errMsg = null;
> try {
> // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClass Name());
> UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFe elClassName());
> // javax.swing.SwingUtilities.updateComponentTreeUI(this);
> } catch (Exception e) {
> errMsg = "Error invoking PlatForm Look and Feel";
> JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
>
> }
>
> }
>
> Any help would be appreciated.
>
> Thanks

--
Thanks,
Rich Kulp
Re: Center JFrame? [message #114721 is a reply to message #114685] Thu, 05 January 2006 19:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: david.welford-costelloe.com

I use:

this.setLocationRelativeTo(null);

To help a little more on run it starts at center then shoots back to the left.

Thanks
Re: Center JFrame? [message #114734 is a reply to message #114721] Fri, 06 January 2006 03:16 Go to previous messageGo to next message
Eclipse UserFriend
Hello,

Yes, setLocationRelativeTo(null) centers the window in the screen. This
works for me.

I observe you call setVisible(true) before setting the window size. I
think this don't will work.

May be interesting you center the window before do a setVisible(true). If
not, the window is showed before be centered and you can see how the
window is centered in the screen.

IMHO I think the initialize method is not a good place for a setVisible
call. If is an application, is better invoke setVisible from the main
method and even the setLocationRelativeTo(null).

Francesc Rosés
Re: Center JFrame? [message #114788 is a reply to message #114734] Fri, 06 January 2006 10:55 Go to previous message
Eclipse UserFriend
Originally posted by: david.welford-costelloe.com

Thanks!

That worked well also I forgot to add the Jframe myframe = new Jframe(); in my main.
I had been running it as a jbean in eclipse which seems to add a launcher. Add the above to main and removed the set visible.

One of my co-workers pointed this out :-(

public static void main(String[] args) {
frmMain myMain = null;
myMain = new frmMain();
myMain.setVisible(true);

}

/**
* This is the default constructor
*/
public frmMain() {

super();
initialize();


}

I had left it blank as I thought Eclipse had generated the code using the JFrame template.


Appreciate the help thanks

:-)
Re: Center JFrame? [message #611740 is a reply to message #114685] Thu, 05 January 2006 19:38 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

No where in this code are you doing a this.setLocation(). Without that
you can't center it. You need to calculate the center of the screen and
then do a setLocation to it.

David Costelloe wrote:
> Hi,
> Been trying to get this to work as per code below:
> No matter what I try I can't get the JFrame to center on the screen.
>
>
> private void initialize() {
> // Configure Platform GUI
> SetUpGUIApplication();
> this.setJMenuBar(getJJMenuBar());
> this.setIconImage(Toolkit.getDefaultToolkit().getImage(getCl ass().getResource( "/resources/FLGCAN.png")));
> this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLO SE);
> this.setResizable(false);
> this.setContentPane(getJContentPane());
> this.setTitle("My Sample App");
> this.setVisible(true);
> this.setSize(482, 342);
> this.setLocationRelativeTo(null);
>
>
> }
>
>
>
> private static void SetUpGUIApplication(){
> String errMsg = null;
> try {
> // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClass Name());
> UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFe elClassName());
> // javax.swing.SwingUtilities.updateComponentTreeUI(this);
> } catch (Exception e) {
> errMsg = "Error invoking PlatForm Look and Feel";
> JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
>
> }
>
> }
>
> Any help would be appreciated.
>
> Thanks

--
Thanks,
Rich Kulp
Re: Center JFrame? [message #611741 is a reply to message #114685] Thu, 05 January 2006 19:46 Go to previous message
Eclipse UserFriend
I use:

this.setLocationRelativeTo(null);

To help a little more on run it starts at center then shoots back to the left.

Thanks
Re: Center JFrame? [message #611742 is a reply to message #114721] Fri, 06 January 2006 03:16 Go to previous message
Eclipse UserFriend
Hello,

Yes, setLocationRelativeTo(null) centers the window in the screen. This
works for me.

I observe you call setVisible(true) before setting the window size. I
think this don't will work.

May be interesting you center the window before do a setVisible(true). If
not, the window is showed before be centered and you can see how the
window is centered in the screen.

IMHO I think the initialize method is not a good place for a setVisible
call. If is an application, is better invoke setVisible from the main
method and even the setLocationRelativeTo(null).

Francesc Rosés
Re: Center JFrame? [message #611746 is a reply to message #114734] Fri, 06 January 2006 10:55 Go to previous message
Eclipse UserFriend
Thanks!

That worked well also I forgot to add the Jframe myframe = new Jframe(); in my main.
I had been running it as a jbean in eclipse which seems to add a launcher. Add the above to main and removed the set visible.

One of my co-workers pointed this out :-(

public static void main(String[] args) {
frmMain myMain = null;
myMain = new frmMain();
myMain.setVisible(true);

}

/**
* This is the default constructor
*/
public frmMain() {

super();
initialize();


}

I had left it blank as I thought Eclipse had generated the code using the JFrame template.


Appreciate the help thanks

:-)
Previous Topic:VE 1.2M1 : how to set input for TableViewer
Next Topic:Error trying to set new file into editor
Goto Forum:
  


Current Time: Thu May 08 21:05:49 EDT 2025

Powered by FUDForum. Page generated in 0.08947 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top