views:

328

answers:

3

I've found that my container is actually changing it's size a short while after being constructed

When it's constructed, I set my components to be at the place I want (like 30px away from the right edge) but later after a short while, I find that it turns from 1008x730 to 1018x740...

(My JFrame is 1024x768)

Does anyone know why this happens and how I can stop this auto-resizing thing?

Thank you.

I just did a -

while (true) {
        System.out.println(c.getSize());
}

And the size changed after a few iterations.

~Edited

+1  A: 

If you can run this from your IDE or attach remotely with the debugger it would be pretty easy to just set a breakpoint on the methods that set the size.

Or alternately, you could subclass JFrame with your own class and similarly override those methods and do

try { throw new Exception("here!"); } catch(Exception e) { e.printStackTrace(); }

That would tell you what is causing the change in size.

Alex Miller
@Alex Miller - an easier way to get the stack trace is to use `Thread.dumpStack();`
McDowell
The thing is, there is only one area where I set the size. It seems like it just happens after waiting a short while. I've printed it right before I enter the loop and it's after a few iterations in the loop (plenty of iterations actually because it's looping so fast) that the size changes.I've tried inserting a JOptionPane.showInputDialog and now it always changes size after I've put in my input.
Dois
+1  A: 

Sounds like the layout manager kicking in. Try running with the Swing Explorer to see what it thinks of the world.

Thorbjørn Ravn Andersen
+1 for Swing Explorer: https://swingexplorer.dev.java.net/
trashgod
+2  A: 

It sounds like you're doing something that changes a component's size and revalidates after calling pack() on the JFrame. Also, rather than calling setSize(), it's often better to set a component's preferred size and let the LayoutManager arrange things.

Addendum: It's generally better to invoke pack() on a top-level container in order to get the initial sizes correct. Also, the content pane of a JFrame defaults to BorderLayout, which may not be what you want.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

    public MyPanel() {
        this.setLayout(new GridLayout(2, 2));
        this.setPreferredSize(new Dimension(320, 240));
        for (int i = 0; i < 4; i++) {
            JLabel label = new JLabel(String.valueOf(i));
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setBorder(BorderFactory.createLineBorder(Color.blue));
            this.add(label);
        }

    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                create();
            }
        });
    }
}
trashgod
I've tried using setPreferredSize (but also with setSize right before it, does that affect anything?) and it didn't work.Also, I'm not calling pack()... I'm working everything around a container from getContentPane() actually.By the way, after I started about a week ago, I read that people prefer to use a JPanel like how you're doing it so I'll try that next time but yeah, it's a container that increases in size for some reason but the JFrame stays the right size - setPreffered/Max/Min size didn't work...
Dois
I rarely use `setSize()` for these very reasons. I've added some links above.
trashgod