Link to home
Start Free TrialLog in
Avatar of justinw
justinwFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Maximising a JFrame window at start-up

What code do I need to make my javax.swing.JFrame based application window start maximised on screen ?

Thanks

ASKER CERTIFIED SOLUTION
Avatar of yerkan
yerkan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yerkan
yerkan

Oopppss,...

This will be:

int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

Regards,

    Yagiz Erkan
Avatar of justinw

ASKER

OK what I'm looking for is the same appearance as when the maximise button is clicked in the window title bar i.e. The applications main frame loses its enclosing rectangle and becomes full screen.
Is there some way I can trigger this maximise event from code before I draw the frame ?

No, there is no 100% Java option.
The Java equivalence of what you are trying to is given in my "Rejected Answer".

Regards,

    Yagiz Erkan
Avatar of justinw

ASKER

I have come across the following at http://java.sun.com/people/linden/faq_c.html#AWT,
Does anyone know any more ?

"Sun left maximize / minimize / deiconify methods out of the AWT, and resisted putting them in for a long time. They finally added setState(NORMAL) and setState (ICONIFIED) in JDK 1.2, and the maximize call came in JDK 1.3 (the so-called Merlin" release).

So get the screen size using Toolkit.getScreenSize(), and then use setBounds() or setLocation() / setSize() to move and resize the frame."


Avatar of justinw

ASKER

My current solution is:

int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
Point pt = getLocationOnScreen();
         
Insets insets = this.getInsets();
if (null != pt && null != insets)
{
     pt.x -= (insets.left);
     pt.y -= (insets.left);
     setLocation(pt);
               
     setSize(screenWidth+(2*insets.left), screenHeight+insets.left+(2*insets.left));
     setVisible(true);
}
         
This puts the main frame in the same position as if the application were starting maximised - however I feel this looks a bit unprofessional because under Windows the maximise icon show the window state as being not maximised.  Clicking the windows maximise icon changes the icons state and the window stays put.
I would prefer the maximise icon to toggle between a smaller 3/4 screen view and the fully maximised view.
Any ideas ?
For the 3rd time, as Yerkan said, there is no way from within Java to trigger a 'maximize' event or to force a frame to maximized state. The best you can do is fake it by setting it to the size of the screen. When java displays a frame, it actually rests on an OS specific peer. That peer is what takes care of the maximizing etc.. Java never interacts with the peer at that event level.

Kylar
Avatar of justinw

ASKER

For anyone else struggling with the maximise problem a suggestion is to use the Java Native Interface to access Win32 functionality.
There is a sample win32-specific java utility class at: http://www.sys-con.com/java/source/4-6/index.html.

This provides maximise functionality and actually works quite well.  You just need to test for the OS you are running on and only make use of these extentions for Win32.
(Note you also need to have installed your implementation DLL on the host machine).
You have to be aware that this is not a 100% Java solution (as I stated before)!
Your code is not portable across different platforms.

Regards,

    Yagiz Erkan