Tzafrir
Tzafrir

Reputation: 669

JFrame size change unintentionally during runtime

I'm working on a complex java swing project. during initialization I set the frame size to 1280X1024. sometimes when I launch my app the frame starts with the expected dimensions other times the frame actual size is 1282X1053 (always this size).

I have two question, while trying to pinpoint the source for this issue.

1) is there a way to set a watch point when the frame size is changed? I'm using eclipse, and when I tried to set a watch point, it breaks when every component I have changes size. this is unacceptable as I have too many components to manually follow.

2) due to the fact that the issue doesn't reproduce every time, I'm worried that maybe somewhere in the code I access Java swing component outside the EDT. is there a way to verify that all the calls to all the swing components in my code are done from the EDT?

EDIT: the below code is a sample of what I use.
I can't attach the code to build the panels as it is too complex to fit here

EDIT 2: the code below works. the problem is happens because of the commented lines before calling setVisible

package com.earlysense.nursestation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyFrame extends JFrame {

    private JPanel west;
    private JPanel center;

    /**
     * Initializes the panels
     */
    public void init() {

        setLocation(0, 0);
        setPreferredSize(new Dimension(1280, 1024));
        setUndecorated(true); // The frame is fixed. It cannot be moved or resized.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel(new BorderLayout());
        west = new JPanel();
        west.add(new JLabel("west"));
        center = new JPanel();
        center.add(new JLabel("center"));
        p.add(BorderLayout.WEST, west);
        p.add(BorderLayout.CENTER, center);
        getContentPane().add(p);
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    MyFrame frame = new MyFrame();
                    frame.init();
                    frame.pack();
                    // at this point frame.getSize() returns 1280X1024
                    // add components to west and center panel which depends on the frame size to set self size
                    frame.setVisible(true);
                    // at this point frame.getSize() sometimes returns 1282X1053
                }
            });
        } catch (InterruptedException e) {/* Do nothing */

        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 3

Views: 551

Answers (2)

camickr
camickr

Reputation: 324197

I set the frame size to 1280X1024.

Why? My screen is 1024 x 768, so I guess I can't use your application? Don't hardcode values. Instead use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

SwingUtilities.invokeAndWait(new Runnable() {

Why are you using invokdeAndWait? All the Swing tutorial and suggestions by people in the forum use invokeLater(..).

Upvotes: -1

Tzafrir
Tzafrir

Reputation: 669

It seems like a bug in java. was known in java 1.3, but apparently still happens. I'm using ubuntu 13.10 64bit, jdk: OpenJDK Runtime Environment (IcedTea6 1.12.6) (6b27-1.12.6-1ubuntu2.1) link

Upvotes: 2

Related Questions