Java JFrame border bug

Hello! I came up with an idea, hope I’ll be able to do all this ^^

I’m making a game in Java, and since Java 7 I have the following problem:

Whenever I want to pack my JFrame after creating and filling it, it leaves a white “border” on the right and the bottom. The game is 800×600, but Java makes the frames content space a bit wider.

I can workaround this by using a one-time action event with Swings Timer class. But isn’t there a more elegant solution?


private void makeUI() {
ds = new Drawscreen();
window = new JFrame("Goatz! v"+VERSION+" - By AyCe for LD 25");
window.setIconImage(MediaLib.icon);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
content = window.getContentPane();
content.setPreferredSize(new Dimension(800, 600));
window.pack();
content.add(ds);
window.setLocationRelativeTo(null);
window.setVisible(true);
ds.requestFocusInWindow();
// hackfix for border bug
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.pack();
}
});
t.setRepeats(false);
t.start();
}

The bug

The bug

Comments

Caironater
15. Dec 2012 路 10:08 UTC
I would suggest that you use LWJGL’s Display class, but now probably isn’t really the time to learn a new api 馃槈

Unfortunatly I don’t have a better sollution for that problem other than Display though, sorry.
Joeslayer97
15. Dec 2012 路 10:15 UTC
just don’t pack ? xD
MAT4DOR
15. Dec 2012 路 10:18 UTC
im using like this

JFrame frame = new JFrame(“Goat Murderer”);

frame.setResizable(false);

GoatMurderer gm = new GoatMurderer();

frame.add(gm);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15. Dec 2012 路 10:19 UTC
Why you need to wait 1s for pack()? you only need to call pack() once, and it should be done right after everything set (content added, properties of frame set).
15. Dec 2012 路 10:33 UTC
Thanks for the answers!

@Caironater: I dont want to use extra libs for java, since its so simple to code games in it anyway ^^

@Joeslayer97: Um.. But how to set the correct resizing then?

@MAT4DOR: And GoatMurderer sets the preferredSize ?

@Drabiter: Yes, I did that. But then it bugs sometimes. So my workaround is to call it after 1 sec, and then it always works.
MAT4DOR
15. Dec 2012 路 16:07 UTC
Yeah man, the GoatMurderer is the Canvas that sets the prefrerredSize.