Can I set a normal JFrame size at maximum magnification?

TASK:

I would like to have the frame visible in the Frame.MAXIMIZED_BOTH state, but I also want it to have a predefined normal size, so when the user does not maximize it, the size of the frame will resize to the size I want it to be, AND I want the frame to be maximally displayed first, without visually resizing . And I want the frame to remain maximized until the user maximizes it.

Things I've tried so far:

one)

 frame.setExtendedState( Frame.MAXIMIZED_BOTH ); frame.setVisible( true ); frame.setSize( 500, 500 ); 

Evaluation: the frame will switch to normal (will not remain maximized) => not good.

2)

 frame.setSize( 500, 500 ); frame.setExtendedState( Frame.MAXIMIZED_BOTH ); frame.setVisible( true ); 

Rating. A window (or just its frame) will appear with a size of 500x500, after which the visual size (to the maximum) will be visible => not good. The reason for this is that if the frame is not displayed, the call to setExtendedState() will be deferred until the frame is visible.

3)

 frame.setSize( 500, 500 ); frame.setVisible( true ); frame.setExtendedState( Frame.MAXIMIZED_BOTH ); 

Rating: just like in attempt No. 2, the frame will have a visual resizing => not good.

4) I tried to add a listener component to a frame overriding componentResize() and changing its state. I also tried adding a window state listener overriding windowStateChange() . I also tried adding a window listener overriding windowOpened() .

Rating: all this led to a visual resizing, as in the attempt # 2 => not good.

So what can I do if I want my frame to have a predefined normal size, but I want it to display as much as possible?

EDIT: The whole question has been edited to be more clear, and added code examples (SSCCE).

+7
source share
5 answers

I'm not sure what you mean, so I made an example to demonstrate what you think you need.

Basically a JFrame is created with setExtendedState(JFrame.MAXIMIZED_BOTH) , and after 5000miliseconds it changes state to JFrame.NORMAL without flickering.

 import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.Timer; public class Test { public Test() { initComponents(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } private void initComponents() { final JFrame frame = new JFrame() { @Override public Dimension getPreferredSize() {//used for testing purposes so JFrame has a size return new Dimension(300, 300); } }; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setVisible(true); new Timer(5000, new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) { frame.setExtendedState(JFrame.NORMAL); } }).start(); } } 
+3
source

The following order works for me

 setSize(100,100); setExtendedState(JFrame.MAXIMIZED_BOTH); setVisible(true); 
+2
source

You were close to the answer in your attempt # 2 . There is a setMaximizedBounds() method in the JFrame class. It is used by the frame on MAXIMIZED_BOTH extendedState to show the frame with a concrete size without flickering. What works for me:

 frame.setLocation(location); frame.setSize( 500, 500 ); frame.setMaximizedBounds(calculateMaximizedBounds(frame)) frame.setExtendedState( Frame.MAXIMIZED_BOTH ); frame.setVisible( true ); 

calculateMaximizedBounds() calculates maximized borders (thanks, KO!) for the frame, depending on the current location and frame size - is it important if U has 2+ monitors, especially if they have different sizes.

+1
source
 this.addWindowStateListener(new WindowStateListener() { @Override public void windowStateChanged(WindowEvent e) { if (e.getNewState() == Frame.NORMAL) { if (getSize().width == 0 || getSize().height == 0) { setSize(500, 500); } } } }); 
0
source

The following code works for me. I used Perzers idea of ​​a WindowEvent listener, but I am checking if the state of the window has been changed from maximized but not minimized (therefore, after restoring the program, the full previous state is restored):

  setExtendedState(JFrame.MAXIMIZED_BOTH); this.addWindowStateListener(new WindowStateListener() { @Override public void windowStateChanged(WindowEvent e) { // In Windows10 state 7 is returned if you minimize, usually ICONIFIED should mean the window is minimized, I used JDK1.6 on W10 (don't ask why).. if (e.getNewState() != JFrame.MAXIMIZED_BOTH && e.getNewState() != 7 && e.getNewState() != JFrame.ICONIFIED) { setSize(768, 500); } } }); setVisible(true); 

I'm not sure what state 7 really is.

UPDATE: using JDK1.8 in W10 - here it is actually "JFrame.ICONIFIED", as it should be. State 7 is actually "NE_RESIZE_CURSOR" which is deprecated. https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.Frame

0
source

All Articles