GOAL:
I'd like to have a frame visible in Frame.MAXIMIZED_BOTH
state, but I also want it to have a predefined normal size so when the user un-maximizes it, the frame will resize to the size I want it to be. And I want the frame to first appear maximized, without a visual resize. And I want the frame to remain maximized until the user un-maximizes it.
Things that I've tried so far:
1)
frame.setExtendedState( Frame.MAXIMIZED_BOTH );
frame.setVisible( true );
frame.setSize( 500, 500 );
Evaluation: The frame will switch to normal state (will not remain maximized) => not good.
2)
frame.setSize( 500, 500 );
frame.setExtendedState( Frame.MAXIMIZED_BOTH );
frame.setVisible( true );
Evaluation: The window (or just its frame) will appear with a size of 500x500, and after that a visual resize (to maximum) will be visible => not good.
The reason for this is that if a frame is not visible, calling setExtendedState()
will be deferred until the frame is visible.
3)
frame.setSize( 500, 500 );
frame.setVisible( true );
frame.setExtendedState( Frame.MAXIMIZED_BOTH );
Evaluation: just like in attempt #2, the frame will have a visual resize => not good.
4)
I've tried to add a component listener to the frame overriding componentResize()
and change state there. I've also tried adding a window state listener overriding windowStateChange()
. I've also tried adding a window listener overriding windowOpened()
.
Evaluation: All resulted in a visual resize just like in 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 appear maximized?
EDIT: edited the whole question to be more clear, and added example codes (SSCCE).
Im not sure what you mean so I made an example to demonstrate what I think you would want.
Basically creates a JFrame
with setExtendedState(JFrame.MAXIMIZED_BOTH)
and after 5000miliseconds it changes state to JFrame.NORMAL
with no 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();
}
}
For me the following order works
setSize(100,100);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
You were close to answer in your #2 attempt. There is the setMaximizedBounds()
method in JFrame
class. It is used by frame on MAXIMIZED_BOTH
extendedState set to show the frame of 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 bounds (thanks, KO!) for frame, depending on current frame location and size - is it important if U have 2+ monitors, especially if they have different dimensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With