Java Swing: JFrame Window Maximisation

W

Whats up dog!

Hi

I have a JFrame that I would like to maximize automatically on execution
(without clicking on the maximise button). The problem is that when it
maximises to screen size, and goes over the windows taskbar. Is there a way
of doing this without hard coding it? Sample code would be nice.


Thanks

AaA
 
C

Chris Smith

Whats said:
I have a JFrame that I would like to maximize automatically on execution
(without clicking on the maximise button). The problem is that when it
maximises to screen size, and goes over the windows taskbar. Is there a way
of doing this without hard coding it? Sample code would be nice.

Yes, you can maximize a window without manually setting the size. Just
do this in an AWT Frame or any subclass.

setExtendedState(getExtendedState() | MAXIMIZE_BOTH);

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Phaero

It still goes over the windows taskbar using this code, both with jvm
1.4.2_04 and 1.5 beta 2

/Johan
 
W

Whats up dog!

Chris Smith said:
Yes, you can maximize a window without manually setting the size. Just
do this in an AWT Frame or any subclass.

setExtendedState(getExtendedState() | MAXIMIZE_BOTH);

doesnt compile.

| <---- suppose to be || or a .?

AaA
 
W

Whats up dog!

Chris Smith said:
Yes, you can maximize a window without manually setting the size. Just
do this in an AWT Frame or any subclass.

setExtendedState(getExtendedState() | MAXIMIZE_BOTH);

I tried the following:

mainFrame.setExtendedState(MAXIMIZED_BOTH);

and i get a minimized frame! the other states do not change the behaviour im
getting - except for ICONIFIED. what am i doing wrong? very annoying.


AaA
 
P

Phaero

Click a lite to fast on send... change MAXIMIZE_BOTH to
Frame.MAXIMIZE_BOTH and import java.awt if you haven't already and it
should work, but it still covers the taskbar...

/Johan
 
W

Whats up dog!

Chris Smith said:
Yes, you can maximize a window without manually setting the size. Just
do this in an AWT Frame or any subclass.

setExtendedState(getExtendedState() | MAXIMIZE_BOTH);


i assume the states dont worked on windowsXP since nothing happens -
according to doc.

anyone can verify?


AaA
 
W

Whats up dog!

Click a lite to fast on send???

not sure how u got it to compile when there are some serious syntax errors:

first one is that it should be Frame.MAXIMIZED_BOTH and not MAXIMIZE_BOTH

second is: why is there a "|" in between shown below?

getExtendedState() | MAXIMIZE_BOTH


AaA
 
P

Phaero

It seems like it's is a bug in javas look and feel, if I call
JFrame.setDefaultLookAndFeelDecorated(true) then the frame will cover
the taskbar and if I don't call it and use the default decoration for
windows xp then it won't cover the taskbar.

I'll try to look in the bug databas at sun for a workaround if nobody
here is fast enough to post one...

/Johan

BTW setUnDecorated don't apear to work either if
JFrame.setDefaultLookAndFeelDecorated(true) is called.
 
P

Phaero

Okej, here is my code I'm using, I've tried it in both j2sdk 1.4.2_4 and
1.5 beta 2 without any errors:

import java.awt.*;
import javax.swing.*;

public class test extends JFrame
{
public test()
{
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}

public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);

test t = new test();
t.setVisible(true);
}
}

This should use the default decoration on windows xp (which is what im
using) and the maximized window won't cover the taskbar, however if you
uncomment the "JFrame.set..." it will use java default decoration and
the maximized window will cover the taskbar.

/Johan
 
L

Liz

Whats up dog! said:
Hi

I have a JFrame that I would like to maximize automatically on execution
(without clicking on the maximise button). The problem is that when it
maximises to screen size, and goes over the windows taskbar. Is there a way
of doing this without hard coding it? Sample code would be nice.

What you mean "hard code" ?
 
T

Tor Iver Wilhelmsen

Whats up dog! said:
second is: why is there a "|" in between shown below?

It's the binary "or" operator. Basically it takes the bits in the
value from getExtendedState() and sets the bits in MAXIMIZE_BOTH as
well.
 
C

Chris Smith

Whats said:
doesnt compile.

Yep. You've already figured out, most likely, that it should be
MAXIMIZED_BOTH rather than MAXIMIZE_BOTH. Everything else is correct.
I'm not familiar with the problem Phaero identified with the LaF frame
decorations, but chances are you aren't using those anyway.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Phaero said:
BTW setUnDecorated don't apear to work either if
JFrame.setDefaultLookAndFeelDecorated(true) is called.

It does "work", but apparently not as you expect it to work.
java.awt.Frame.setUndecorated controls whether the native window manager
will decorate a window. If you're calling
setDefaultLookAndFeelDecorated, then the specified behavior of that
method is to call setUndecorated on new JFrame so as to prevent the
window manager from decorating the window, and then to call
JRootPane.setWindowDecorationStyle to add Swing decorations. If
setUndecorated did not work, you would see two different title bars on
the window!

As for the maximize behavior, I expect that windows without native title
bars in Windows XP probably can't be maximized by the operating system,
so the AWT implementation falls back to a manual resize, which doesn't
behave in exactly the same way as the OS maximize option does.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Phaero

Changed the example a bit, this works even if
setDefaultLookAndFeelDecorated is called or not:

import java.awt.*;
import javax.swing.*;

public class test extends JFrame
{
public test()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
this.setMaximizedBounds(env.getMaximumWindowBounds());
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}

public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);

test t = new test();
t.setVisible(true);
}
}

If you can find a better solution, please tell me...

/Johan
Okej, here is my code I'm using, I've tried it in both j2sdk 1.4.2_4 and
1.5 beta 2 without any errors:

import java.awt.*;
import javax.swing.*;

public class test extends JFrame
{
public test()
{
this.setExtendedState(this.getExtendedState() |
this.MAXIMIZED_BOTH);
}

public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);

test t = new test();
t.setVisible(true);
}
}

This should use the default decoration on windows xp (which is what im
using) and the maximized window won't cover the taskbar, however if you
uncomment the "JFrame.set..." it will use java default decoration and
the maximized window will cover the taskbar.

/Johan
Click a lite to fast on send???

not sure how u got it to compile when there are some serious syntax
errors:

first one is that it should be Frame.MAXIMIZED_BOTH and not MAXIMIZE_BOTH

second is: why is there a "|" in between shown below?

getExtendedState() | MAXIMIZE_BOTH


AaA
 
W

Whats up dog!

I have a feeling your not using windowsXP, because I did a different
behaviour using your exact code. I get a minimized frame in the top left
corner of screen - which suggest that this state is not supported on this
OS.

Anyway, wouldnt it be easier to just do:

mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

AaA

Phaero said:
Changed the example a bit, this works even if
setDefaultLookAndFeelDecorated is called or not:

import java.awt.*;
import javax.swing.*;

public class test extends JFrame
{
public test()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
this.setMaximizedBounds(env.getMaximumWindowBounds());
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}

public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);

test t = new test();
t.setVisible(true);
}
}

If you can find a better solution, please tell me...

/Johan
Okej, here is my code I'm using, I've tried it in both j2sdk 1.4.2_4 and
1.5 beta 2 without any errors:

import java.awt.*;
import javax.swing.*;

public class test extends JFrame
{
public test()
{
this.setExtendedState(this.getExtendedState() |
this.MAXIMIZED_BOTH);
}

public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);

test t = new test();
t.setVisible(true);
}
}

This should use the default decoration on windows xp (which is what im
using) and the maximized window won't cover the taskbar, however if you
uncomment the "JFrame.set..." it will use java default decoration and
the maximized window will cover the taskbar.

/Johan
Click a lite to fast on send???

not sure how u got it to compile when there are some serious syntax
errors:

first one is that it should be Frame.MAXIMIZED_BOTH and not MAXIMIZE_BOTH

second is: why is there a "|" in between shown below?

getExtendedState() | MAXIMIZE_BOTH


AaA



Click a lite to fast on send... change MAXIMIZE_BOTH to
Frame.MAXIMIZE_BOTH and import java.awt if you haven't already and it
should work, but it still covers the taskbar...

/Johan

Phaero wrote:


It compile fine for me, however it doesn't solve your problem...

/Johan

Whats up dog! wrote:


:



I have a JFrame that I would like to maximize automatically on
execution
(without clicking on the maximise button). The problem is that when


it

maximises to screen size, and goes over the windows taskbar. Is
there


a


way


of doing this without hard coding it? Sample code would be nice.



Yes, you can maximize a window without manually setting the size.


Just

do this in an AWT Frame or any subclass.

setExtendedState(getExtendedState() | MAXIMIZE_BOTH);




doesnt compile.

| <---- suppose to be || or a .?

AaA
 
C

Chris Smith

Whats said:
I have a feeling your not using windowsXP, because I did a different
behaviour using your exact code. I get a minimized frame in the top left
corner of screen - which suggest that this state is not supported on this
OS.

Have you tried Toolkit.isFrameStateSupported? On XP, it returns true
for MAXIMIZED_BOTH. I just tried this out and discovered that the frame
needs to be realized before setExtendedState is called.
Anyway, wouldnt it be easier to just do:

mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

Two things.

First, Phaero is talking about getting maximization to work well when a
frame is not natively decorated. I've seen no indication that this is
your problem, so you can probably ignore most of that.

However, it would be good form to use the longer form:

myFrame.setExtendedState(
myFrame.getExtendedState() | Frame.MAXIMIZED_BOTH);

This means that any *other* extended state is preserved when you
maximize the window. That's good because you don't know what other
extended state bits might be added in the future, and you probably don't
want to clear them out without a thought.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
W

Whats up dog!

Toolkit.isFrameStateSupported(Frame.MAXIMIZED_BOTH);

this gives me a compile eror saying nonstatic method cannot be reference
from a static ontext blah blah

help.

AaA
 
C

Christophe Vanfleteren

Whats said:
Toolkit.isFrameStateSupported(Frame.MAXIMIZED_BOTH);

this gives me a compile eror saying nonstatic method cannot be reference
from a static ontext blah blah
Please don't toppost.

You are trying to call a nonstatic method without an instance:
<http://mindprod.com/jgloss/errormessages.html#NONSTATICCANTBEREF>

You need to call this method on a Toolkit instance, which you can get by
calling getDefaultToolkit():

<http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#getDefaultToolkit()>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top