0

I want to have JFrame with size that fits my screen perfectly. I used getScreenSize(); to get resolution of my computer and set it to JFrame's size. However i found that Jframe's size is actually bigger than my computer's resolution because of the titlebar. (which mean u will find bottom of the jframe is behind window taskbar)

The following code demostrate my problem :

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Titlebar extends JFrame {

    private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public void run(){
        this.setTitle("TitleBar"); 
        this.setSize(_screenSize);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    } 

    public static void main(String[] args) {
        Titlebar test = new Titlebar();
        test.run();
    }
}

is that possible to set the jframe size minus titlebar's size ?

LOK CARD
  • 293
  • 2
  • 16
  • 3
    You mean something like [this](http://stackoverflow.com/questions/15677245/jframe-maximized-to-screen-size-with-dimension/15677382#15677382) or [this](http://stackoverflow.com/questions/27031807/how-to-set-present-screensize-in-java-swing/27031968#27031968)? – MadProgrammer Apr 28 '15 at 02:44
  • 2
    Try `this.setExtendedState(JFrame.MAXIMIZED_BOTH);` Edit: What @MadProgrammer said. :P – Andrew Thompson Apr 28 '15 at 02:45
  • For my money, don't use `Toolkit.getDefaultToolkit().getScreenSize()`, it's to limited, it only gives you the "default" screen size and doesn't take into account multiple screen systems and things like the task bar or dock – MadProgrammer Apr 28 '15 at 02:47
  • quoting from [here](http://stackoverflow.com/questions/18969085/jframe-setextendedstate-doesnt-actually-maximise/18970402#18970402). I don wan it to be 'normal' state when i move the JFrame. I tried `frame.setSize(frame.getSize())` during 'maximized' state, doesn't work – LOK CARD Apr 28 '15 at 06:46
  • i think i will figure it out eventually though. Thank you Andrew and MadProgrammer – LOK CARD Apr 28 '15 at 06:53

1 Answers1

2

Two ways are there: 1. Just "Maximize" the window using the code i.e. add the following:

this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );

Your code will look like this:

public class Titlebar extends JFrame {

    private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public void run(){
        this.setTitle("TitleBar"); 
        this.setSize(_screenSize);
        this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    } 

    public static void main(String[] args) {
        Titlebar test = new Titlebar();
        test.run();
    }
}

2. This option is to workaround i.e. by keeping always at the top then you will see this over the task bar i.e. adding the following:

this.setAlwaysOnTop(true);

Your code will be like this:

public class Titlebar extends JFrame {

    private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public void run(){
        this.setTitle("TitleBar"); 
        this.setSize(_screenSize);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    } 

    public static void main(String[] args) {
        Titlebar test = new Titlebar();
        test.run();
    }
}

Depending upon your need you can choose the option. But I believe option 1 will be good for you.

comrench
  • 248
  • 2
  • 9
  • 1
    thanks ! your answer is very precise and useful :D i would like to know further that how do i deal with the 'normal' state situation like [this](http://stackoverflow.com/questions/18969085/jframe-setextendedstate-doesnt-actually-maximise/18970402#18970402) ? – LOK CARD Apr 28 '15 at 06:50
  • Do you mean ... you want to maximize the frame in normal state (i.e. zero width and zero length) so that should work with this or are you referring to something else? – comrench Apr 29 '15 at 03:39
  • i want the frame to have certain width and height in 'normal' state, so it wont get packed into one small region. – LOK CARD Apr 30 '15 at 02:29