Maximized JFrame
Hello!
I'm wondering how can I launch JFrame already maximized ?
jdk1.3.1
I was told that in jdk1.4 there is special method for this operation.
Hello!
I'm wondering how can I launch JFrame already
maximized ?
jdk1.3.1
I was told that in jdk1.4 there is special method for
this operation.Not directly possible in 1.3.1. If you do a forum search you'll find two workarounds: via screensize and using robot.
Similar Messages
-
Maximized JFrame obscuring task bar
I have an application that obscures the windows taskbar when it is maximized (using the JFrame's own maximize button - NOT programmatically). The same happens when this.setExtendedState(JFrame.MAXIMIZED_BOTH) is called.
J2SE 1.4.0 in windows 2000 is the environment (from what have read, maximize should work well in 1.4). I can create a brand new JFrame and mostly it works - is there some undocumented property that is causing it to maximize to the fullscreen instead of the usable screen?
Thanks in advance.I was hoping that there was a way that made sure the JVM itself would do it (it works most of the time) - such a simple but important thing to end users should NOT require JNI code. Maximising on at least windows and macintosh (and most linux flavours) is pretty well defined - anyone that says it is a platform specific feature is just wrong - it can apply to any GUI system at least in some form.
-
I've been looking around the API and I haven't found a way to maximize a JFrame programmatically. The best I've done is figure out the screen size and setSize it, manually, but it's sill shows a maximize button. Anybody with any ideas? Or is this simply not possible?
Try using Toolkit...
Toolkit theKit = window.getToolkit();
Dimension wndSize = theKit.getScreenSize();
window.setSize(wndSize.width, wndSize.height);
where window is a JFrame object...
hope this helps... -
This is the same topic as in this thread
http://forum.java.sun.com/thread.jspa?threadID=734904&tstart=0
thanksDear Akshay,
When using a Linux OS, in transaction DC20 a separate frontend type and a path for processing originals in according with Linux OS specifications must be defined.
In addition, the users must then select this front end types before they display an original. They can do this in the "Extras" menu with the menu command "Change frontend type". For this change it is important that the "HOSTNAME" variable is maintained for the MAC computer. On Windows computers, you can maintain this in the system control panel. This HOSTNAME value is then entered for the new frontend types and in future the user should then always use the new frontend type.
Best regards,
Christoph -
JFrame (extended) state, iconified/maximized/normal question
Greetings and salutations,
I've been removing some rough edges from my last application and I noticed the
following: before an application quits every location/size of the top level components
is written to a file. When the application starts again everything is read back in
again and the application shows itself just as it was before it closed the last time.
There's one little quirck though:
A JFrame can be in one of three states when an application quits:
1) iconified; nothing is wrong here, i.e. the application comes back on in the same
iconified state and after de-iconification the JFrame shows up the same as
before the previous quiting.
2) 'normal': same as above: the JFrame shows up identical as before quiting
the application the previous time.
3) maximized: the new incarnation shows up as a maximized JFrame, but after
'demaximalization' (sorry) the JFrame keeps its maximum size, i.e. if still fills up
the entire screen.
Before quiting I save the bounds, the state and the extended state of a JFrame
and reinstate these values after a new incarnation of the application. I think I've
tried all 2^3 == 8 possible permutations of this all, but option 3) keeps giving
me trouble, i.e. the bounds don't work after 'demaximalization'.
Ideas anyone?
kind regards,
JosHere's a test program for people who want to experiment around a bit, but are reluctant to create a test class:import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Test extends JFrame {
public Test () {
setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
setTitle ("Test");
WindowInfo windowInfo;
try {
windowInfo = read ();
} catch (Exception exception) {
System.err.println ("couldn't read window.info");
windowInfo = new WindowInfo ();
setLocation (windowInfo.location);
setSize (windowInfo.size);
setExtendedState (windowInfo.extendedState);
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent event) {
try {
write (new WindowInfo (getLocation (), getSize (), getExtendedState ()));
} catch (Exception exception) {
System.err.println ("couldn't write window.info");
setVisible (true);
public static void main (String... parameters) {
new Test ();
private WindowInfo read () throws ClassNotFoundException, IOException {
ObjectInputStream in = null;
try {
in = new ObjectInputStream (new FileInputStream ("window.info"));
return (WindowInfo) in.readObject ();
} finally {
if (in != null) {
try {
in.close ();
} catch (IOException exception) {}
private void write (WindowInfo windowInfo) throws IOException {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream (new FileOutputStream ("window.info"));
out.writeObject (windowInfo);
} finally {
if (out != null) {
try {
out.close ();
} catch (IOException exception) {}
private class WindowInfo implements Serializable {
public Point location;
public Dimension size;
public int extendedState;
public WindowInfo () {
size = new Dimension (600, 400);
Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
location = new Point (screenSize.width / 2 - size.width / 2, screenSize.height / 2 - size.height / 2);
extendedState = NORMAL;
public WindowInfo (Point location, Dimension size, int extendedState) {
this.location = location;
this.size = size;
this.extendedState = extendedState;
}Note that I fooled around myself a bit too. -
Making components NOT resizable, forcing certain components to be resized
Basic setup -
A maximized JFrame with BorderLayout, a JPanel added to the contentPane in location BorderLayout.WEST, and another container in BorderLayout.EAST (not that it should matter much).
In the JPanel in the western side, I have it set up with a BoxLayout going down along the Y axis, and in order, I have a JPanel (with a few JLabels inside), a JPanel with a JLayeredPane that uses GridLayout, and a blank JPanel to fill up space (remember, the parent frame is maximized).
What happens is that my JPanel with the JLayeredPane gets stretched (and distorted) out to the bottom of the frame, when what I want is that the empty JPanel gets stretched and fills up the empty space.
Is there a way to "lock" the layered pane's size so that it doesn't get stretched? Which layout should I use, or how can I make the JPanel take up the empty space? I suppose I could set the empty panel's preferred height to Integer.MAX_VALUE, but that seems like a rather messy solution.
Also... sometimes when I click on the JLayeredPane (which is a gridlayout of JPanels) and I remove a component from one of the JPanels, the whole layout gets shifted over and really distorted (not stretched, but jumbled). I've narrowed the problem down to something to do with validation/revalidate. Would anyone happen to have an idea of what's going on?
ThanksI had already tried using glue, but it did nothing so I switched over to the filler JPanel, but that obviously didn't help much, but wonderfully enough, once I set the JPanel with the JLayeredPane to have a FlowLayout instead of a BorderLayout, glue worked!
So, now that that's over with, anyone have any ideas about the validation issues... like how to prevent things from revalidating, etc...? -
JFrame which does not allow resizing (maximizing, minimizng)
I want to develop a login frame which does not allow resizing (maximizing, minimizing as this does not make sense) and with 2 fields, JTextField and JPasswordField.
The frame is developed but has the usual resizing handles.
How to do?
Many tks for any information.
John PashleyDon't use a JFrame. Also, don't expect code like this again; I did it for fun.
It requires your modification.
* Title: Login Screen<p>
* Description: Login Screen<p>
* Class: Login ScreenLoginScreen<p>
* Copyright: who cares<p>
* Company: who cares<p>
* @author who cares
* @version who cares
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Vector;
import java.io.File;
import javax.swing.*;
public class LoginScreen extends JDialog implements ActionListener, FocusListener
private JTextField name;
private JPasswordField password;
private JButton loginButton;
private JButton cancelButton;
private JDialog thisDialog = this;
private ImageIcon splashImage;
private String appTitle;
private int logCounter;
public LoginScreen()
super();
this.toFront();
setTitle("Login");
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
getContentPane().setLayout(new BorderLayout());
splashImage = new ImageIcon( getClass().getResource("images" + File.separator + "image.jpg")) );
getContentPane().add(new JLabel(splashImage), "North");
getContentPane().add(makeLoginPanel(), "Center");
getContentPane().add(makeButtonPanel(), "South");
pack();
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
* make login panel
private JPanel makeLoginPanel()
JPanel loginPanel = new JPanel();
JLabel label;
loginPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
loginPanel.setLayout(gbl);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 5, 10, 5);
gbc.gridx = 0;
gbc.gridy = 0;
label = new JLabel("Login Name:", SwingConstants.LEFT);
gbl.setConstraints(label, gbc);
loginPanel.add(label);
gbc.gridx = 1;
name = new JTextField("insider", 10);
name.addFocusListener(this);
gbl.setConstraints(name, gbc);
loginPanel.add(name);
gbc.gridx = 0;
gbc.gridy = 1;
label = new JLabel("Password:", SwingConstants.LEFT);
gbl.setConstraints(label, gbc);
loginPanel.add(label);
gbc.gridx = 1;
password = new JPasswordField("insider",10);
password.addFocusListener(this);
gbl.setConstraints(password, gbc);
loginPanel.add(password);
return loginPanel;
* make button panel
private JPanel makeButtonPanel()
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//make LogIn button
loginButton = new JButton("Login");
loginButton.setActionCommand("Login");
buttonPanel.add(loginButton);
rootPane.setDefaultButton(loginButton);
loginButton.addActionListener(this);
this.getRootPane().setDefaultButton(loginButton);
//make Cancel button
cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPanel.add(cancelButton);
cancelButton.addActionListener(this);
this.addKeyListener(new KeyAdapter()
public void keyReleased(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
cancelButton.doClick();
return buttonPanel;
* Action handler for login and cancel buttons
public void actionPerformed(ActionEvent e)
JButton btn = (JButton) e.getSource();
if (btn.getActionCommand().equals("Login"))
// Because login() process happens before swing process (above),
// force it to happen "later"
SwingUtilities.invokeLater(new Runnable()
public void run()
login(); //create this method, okay?!
else if (btn.getActionCommand().equals("Cancel"))
System.exit(0);
* Focus gained handler for username and password buttons
public void focusGained(FocusEvent e)
JTextField tf = (JTextField) e.getSource();
tf.selectAll();
* Focus lost handler for username and password buttons
public void focusLost(FocusEvent e) {}
private void showErrorMessage(String message, String header)
JOptionPane.showMessageDialog(getContentPane(),
message, header,
JOptionPane.ERROR_MESSAGE);
* This method controls the cursor for login window. If isWait is set to
* true, dialog's cursor is set to Cursor.WAIT_CURSOR. If isWait is set
* to false, the cursor is set ta Deafult.
* While the window is in WAIT mode, this method will also disable Login
* and Cancel buttons to ensure the user does not accidently request
* a cancel while loging in or launching a second login.
private void setWaitCursor(boolean isWait)
/* In order to disable login and cancel buttons while logging in to the
application, this method will temporarely change action commands and
reset them when login process failed to give user another chance.
Note: Disabling the buttons by calling setEnabled(false) did not work
since login() method is called from an action event handler and the
process will not be released to AWT until login method is complete.
if (isWait)
this.getGlassPane().setCursor(new Cursor(Cursor.WAIT_CURSOR));
this.getGlassPane().setVisible(true);
loginButton.setActionCommand("none");
cancelButton.setActionCommand("none");
else
this.getGlassPane().setCursor(Cursor.getDefaultCursor());
this.getGlassPane().setVisible(false);
loginButton.setActionCommand("Login");
cancelButton.setActionCommand("Cancel");
} //end loginscreen -
How to bring a JFrame up maximized?
if you have a class like this:
public class myClass extends JFrame {
//some components
public myClass() {
//add some components
setVisible(true);
how can you make this JFrame to appear maximized?
SJGThere may be another way, but try this:
this.setSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight() ); -
JFrame maximizing - non deterministic behaviour
Hi there,
I wonder why in the following code the componentResized Method gets a JFrame instance wthich's extended state is MAX_BOTH and the componentMoved Method NORMAL.
Does someone have an explanation or an work around for storing the last position of the JFrame (to store in preferences in order to load them again).
Here is a minimal example.
import javax.swing.JFrame;
* @author Karlheinz Toni
public class Test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.addComponentListener(new ComponentAdapter() {
* (non-Javadoc)
* @see java.awt.event.ComponentAdapter#componentMoved(java.awt.event.ComponentEvent)
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
super.componentMoved(e);
printExtendedState(e.getComponent(), "moving");
* (non-Javadoc)
* @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent)
public void componentResized(ComponentEvent e) {
// TODO Auto-generated method stub
super.componentResized(e);
printExtendedState(e.getComponent(), "resizing");
private void printExtendedState(Component component,
String whatDoIDo) {
if (component instanceof JFrame) {
System.out.println("Extended state for " + whatDoIDo + ":"
+ ((JFrame) component).getExtendedState());
mainFrame.setVisible(true);
}I would be greatful to any help ;)
Sincerely
CharlyI was playing with exactly this kind of thing last night and discovered that the order you get the events isn't really guaranteed. I solved this by using a swing Timer to wait for 1 second of inactivity before querying the Frame and updating the user preferences. Here's the chopped down version of my code:
I register a GeometryTracker object as a ComponentListener and a WindowStateListener in the frame to be monitored.
This all seems to work quite well, but I'm still seeing differences between windows and OS-X for example. (on OS-X the maximized state doesn't seem to correlate with reality. If anyone knows of a trick I'd love to see it.
Thanks and I hope this helps!
Cheers,
Seve
import javax.swing.Timer;
public class GeometryTracker
extends ComponentAdapter
implements WindowStateListener
private Frame frame;
private Preferences framePrefs;
private int frameState = 0;
private Rectangle frameBounds;
private Timer updateTimer;
// TODO: problem with setting up the preferences once like this: changes to screen size aren't handled
public GeometryTracker( Frame targetFrame ) {
frame = targetFrame;
framePrefs = Preferences.userNodeForPackage( frame.getClass() );
frameState = frame.getExtendedState();
frameBounds = frame.getBounds();
updateTimer = new Timer( 1000, new AbstractAction() { public void actionPerformed( ActionEvent e ) { updatePrefs(); } } );
private synchronized void updatePrefs() {
frameState = frame.getExtendedState();
// don't update the window size/location if it has been maximized
if( ( frameState & Frame.MAXIMIZED_BOTH ) == 0 ) {
frameBounds = frame.getBounds();
framePrefs.putInt( "x", frameBounds.x );
framePrefs.putInt( "y", frameBounds.y );
framePrefs.putInt( "width", frameBounds.width );
framePrefs.putInt( "height", frameBounds.height );
framePrefs.putInt( "state", frameState );
updateTimer.stop();
public void setBounds( Rectangle defaultBounds ) {
Rectangle r = new Rectangle();
r.x = framePrefs.getInt( "x", defaultBounds.x );
r.y = framePrefs.getInt( "y", defaultBounds.y );
r.width = framePrefs.getInt( "width", defaultBounds.width );
r.height = framePrefs.getInt( "height", defaultBounds.height );
frameState = framePrefs.getInt( "state", Frame.NORMAL );
frame.setBounds( r );
if( ( frameState & Frame.MAXIMIZED_BOTH ) != 0 ) {
frame.setExtendedState( frameState );
public void componentMoved( ComponentEvent e ) {
updateTimer.setDelay( 1000 );
updateTimer.start();
public void componentResized( ComponentEvent e ) {
updateTimer.setDelay( 1000 );
updateTimer.start();
public void windowStateChanged( WindowEvent e ) {
updateTimer.setDelay( 1000 );
updateTimer.start();
} -
Maximized decorated JFrame covers task bar on WinXP. How to avoid?
Hello,
I have found that decorated JFrame covers task bar on Win XP and jdk 1.5 when maximized. I see this problem affects many users. It is annoying for most users of my applications to change window size before accessing task bar. So I have to avoid decorations, inspite the fact that I really like how decorated windows look like.
I have read forum topic which discussed the same problem (http://forum.java.sun.com/thread.jspa?forumID=57&threadID=709917), but it contains no solution.
Here is sample code to reproduce the problem:
import java.awt.Dimension;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
Test frame = new Test();
frame.setPreferredSize(new Dimension(200,200));
frame.pack();
frame.setVisible(true);
}Run it on Win XP and click maximize button. You will see that maximized window covers task bar.
Am I doing something wrong? Maybe it is possible somehow to correct this behaviour programmatically or at least any workaround exists?
Regards,
RomanSolved the problem finally. It seems I have not read previous forum topic (http://forum.java.sun.com/thread.jspa?forumID=57&threadID=709917) with enough attention.
For thouse who are interested solution is following:
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
Test frame = new Test();
frame.setPreferredSize(new Dimension(200,200));
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
frame.setMaximizedBounds(env.getMaximumWindowBounds());
frame.pack();
frame.setVisible(true);
}Regards,
Roman -
Hi,
does anyone know, how to maximize JFrame window?
JFrame frame = new JFrame();
// frame.pack();
frame.setSize( ??? );
frame.setVisible(true);Thanks in advance. Lokutus.Another approach:
frame.setExtendedState(Frame.MAXIMIZED_BOTH);although I'm not sure this will work on all platforms. This way has the additional advantage that the frame will be "truly" maximized, i.e. its maximize/restore button will be updated. -
Starting jframe in maximized state
I am wanting to run my jframe in a maximized state when it first runs. I was looking for a way to do this but wasn't able to find away. Also is there a way to detect the users screen resolution? Any help would be appreciated. Thanks
setExtendedState(JFrame.MAXIMIZED_BOTH);http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Toolkit.html#getScreenSize()
-
I've been looking for a way to maximize a JFrame with JDK 1.3 in pure Java and found out that I can't. Now I want to place my JFrame on my Desktop at maximum size in a way that it does not interfere with e.g. the Windows Taskbar.
While researching I came across this article:
http://forums.java.sun.com/thread.jsp?forum=57&thread=116814
I took the code from jane_jk's post and modified it a bit. Now i have this:
Dimension dimScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dimScreenSize);
setVisible(true);
Rectangle rectVisible = getRootPane().getVisibleRect();
int iWidth = (int)rectVisible.getWidth();
int iHeight = (int)rectVisible.getHeight();
int iX = (int)rectVisible.x;
int iY = (int)rectVisible.y;
setBounds(iX,iY,iWidth,iHeight);I'm working with a windows desktop resolution of 1024x768 and my taskbar is attached to the left of the screen. But getVisibleRect does not consider that my taskbar is at the left of the screen. The funktion thinks the taskbar is at the botom of the screen.
I get:
iWidth = 1016
iHeight = 741
iX = 0
iY = 0
I've tried this woth the taskbar at the top and at the right of the screen with similar results.
Has anybody encountered this too? Is this a bug in the getVisibleRect function?You (and jane_jk) misunderstodd what getVisibleRect() does. Take a look at the javadoc:
Returns ... the intersection of this component's visible rectangle and all of its ancestors' visible rectangles.
It cannot take any windows into account that are not in the hierarchy. Native windows like the task bar are something different altogether.
Up to JDK 1.4 there simply is no reliable, portable way to maximize a frame.
Each of the non-portable or simply hacky ways of maximizing have been discussed before.
Stephen -
Hi,
I tried this code
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize( screenSize.width, screenSize.height - 80 );
frame.validate();
and the jframe is now almost cover the whole screen, the problems are
- I need to substract with 80, since otherwise my windows toolnar will cover the frame.
- In macosx it is ok but some parts of the table on the bottom of the jframe looks like cut.
Is there any simple way to make the screen maximized?
Thank for the help.
DanielDear Andrew,
Thank you for the quick response. Agree, code snippets would be readable if we put them inside . Apologise for the mistake.
Once again, thanks a lot. -
Maximizing a JFrame - a problem
Hello
I am using JDK1.4 and as instructed in precious answers, I have used
frame.setExtendedState(JFrame.MAXIMIZED_BOTH)
this is my code:
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
The problem is the it seems like the frame gets maximized for a splint second, and then automatically returns to the original size (according to its content)
What am I doing wrong?Yes I have. Nobody seems to know the answer. The only think I found was there could be an answer to it in closed question #849 in the lang group but I can't find a way to get to it and as far as I can tell no one else has been able to get to it either. Supposedly this was a bug in earlier versions of Java but maybe there's a way now. So if anybody knows the answer to my question or knows how to get to closed question #849, i would appreciate your help. Thanks.
Maybe you are looking for
-
Paguei o Adobe muse CC, mas não passo da tela de Avaliação
Eu paguei uma assinatura do adobe muse CC, mas quando eu abrir o programa ele diz que a licença gratuita expirou. Eu clico em LICENCIAR ESTE SOFTWARE, aparece a mensagem parabéns por comprar, mas depois volta para a tela de AVALIAÇÃO, como se o meu s
-
Repeated retrieval and parsing of XML file causes IE to display an error message
I have a flash application that makes a HTTP call every 120 seconds to retrieve a xml file. This file is being generated with fresh data every few minutes or so and pushed to the apache web root with a unix mv command. I'm using the standard Flash XM
-
Difference between SAP 4.7EE and SAP ECC 6.0 in terms of ABAP
Hi all, I would like to know the differences between the SAP-4.7EE and SAP ECC 6.0 in terms of ABAP? can any body Please Help me?
-
Not sure how to get WMP11 to see my Treo Pro. Active sync works fine, but WMP doesn't detect the Pro when connected via USB. How can I get this to work? Thanks. Post relates to: Treo 750 (Cingular) This question was solved. View Solution.
-
Wifi is either painfully slow or won't connect.
If I am in a hotspot, I try to use Safari, and although I am connected to the internet, actually getting a site is so slow that it is not really useful. Email too is very slow. A computer connects and runs normally. Anything to do here? Thanks SK