Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overlooked something silly in swing 1

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hi,

I am new to Java and my swing GUI is resizing :(

a = new JFrame();
a.setSize(x,y); <-- desire fixed size
a.setVisible(true);

...

a.setLayout(new LayoutFlow()); // <-- suspect problem
a.add(stuff);
a.pack();
a.setVisible(true); // <-- the size is no longer x,y


Do I have to setSize(x,y) every time the content changes, or is there an easier way?

Thanks,

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
I believe that the pack() method resizes your frame according to the contents of the frame.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Nothing appears if I don't use pack()

Is there a way to add content without resizing the frame?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
The Java API Docs state that pack() 'Causes this Window to be sized to fit the preferred size and layouts of its subcomponents'. So that's definitely the culprit. It also makes everything visible if not already, so you don't have to explicitly set the visibility to true if using it.

When I do GUIs, I put the pack or the show (depending upon which is appropriate) right at the end of all the build code. I use show instead of setVisible since it validates the container's contents before making it visible.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
This show method is on the java.awt.Window class and has not been deprecated. It's what is used when calling it on the JFrame class in the OP's case.

The show method on a java.awt.Component has been deprecated in favour of setVisible, but that isn't the one I mean.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top