Center JFrame? [message #114685] |
Thu, 05 January 2006 19:25  |
Eclipse User |
|
|
|
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 #114788 is a reply to message #114734] |
Fri, 06 January 2006 10:55  |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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 #611742 is a reply to message #114721] |
Fri, 06 January 2006 03:16  |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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
:-)
|
|
|
Powered by
FUDForum. Page generated in 0.08947 seconds