17

I created java GUI using myEclipse Matisse. when my Screen Resolution is 1024x768 it works fine but when i change resolution my GUI is not working fine. I want my GUI window should be re-sized according to the screen Resolution I am extending JFrame to create the main window.

public class MyClass extends JFrame {

    //I am putting some controls here.

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0,0,screenSize.width, screenSize.height);
    setVisible(true);

    pack();
}

this is not working, what ever i do, setting size hardcoded or by ToolKit using, the Frame Size Remains same.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Asghar
  • 2,336
  • 8
  • 46
  • 79
  • BTW - it is not surprising that it is not working, considering that code would not compile. In future, please consider posting an [SSCCE](http://pscode.org/sscce.html) to demonstrate coding problems. – Andrew Thompson Jul 21 '11 at 14:49
  • As an aside, when running with multiple displays, `Toolkit.getScreenSize()` will return the size of the primary display which may not be the one showing your frame and may not have the same size (e.g. a laptop with external monitor). – Rangi Keen Jan 10 '22 at 16:32

7 Answers7

36

You can try using this to maximize the frame:

this.setExtendedState(JFrame.MAXIMIZED_BOTH);
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
Kowser
  • 8,123
  • 7
  • 40
  • 63
17

You are calling pack() which changes the frame size so it just fits the components inside. That's why it is shrinking back I think. Remove the pack() line and it should work.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • 1
    -1, really?!?! I am explaining why it doesn't work... Sometimes you don't need the pack. It is up to him... – Petar Minchev Jul 21 '11 at 14:05
  • *"Sometimes you don't need the pack."* Sure, but besides "when you don't add *anything* to the frame", what other situations are included in that 'sometimes'? – Andrew Thompson Jul 21 '11 at 14:47
  • http://download.oracle.com/javase/tutorial/uiswing/components/frame.html See point 4. pack() is "preferable" to setSize. This doesn't make it wrong to not use it. – developmentalinsanity Jul 21 '11 at 15:20
  • 2
    @ That same point goes on to add "..pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size."* So.. now we're adding `null` layouts to that 'sometimes'? That wreaks even more. Even if you are doing custom painting across the entire content area of the frame, that painting needs to be done in a `JComponent` that is then added to the frame. When you add it, it is simplicity itself to size & position the component using a *layout manager.* – Andrew Thompson Jul 21 '11 at 15:47
  • The fact that the layout manager isn't in charge of the frame size doesn't mean you're not using a layout manager. In a maximized window (like the answer by Kowser you like), the layout manager isn't in charge of the window size and has to lay out the component within a fixed size. Not using pack results in the same scenario, just with the window size not necessarily taking up the entire screen. – developmentalinsanity Jul 21 '11 at 15:49
  • 3
    *"the layout manager isn't in charge of the window size and has to lay out the component within a fixed size."* Nobody *ever* said the layout manager is (or should be) in charge of the size of the full-screen window. The layout manager is responsible for the size & layout of the **children** of the window. – Andrew Thompson Jul 23 '11 at 03:53
7

Another way to do this is:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
pack();
setSize(screenSize.width,screenSize.height);
Buck
  • 152
  • 1
  • 8
5

Calling pack() is vital to a correctly functioning GUI. Call it after all the components have been added, to have it validate the container and set it to it's natural size.

Then call setSize() & related methods like setBounds() afterwards.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Try this... Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close .

Dikaios
  • 9
  • 5
-2

Calling pack() will usually result in the window being resized to fit the contents' preferred size. Try removing the call to pack() and see what happens.

developmentalinsanity
  • 6,109
  • 2
  • 22
  • 18
-5

First, setbounds for frame then remove pack() method.