tags:

views:

50

answers:

4

I'm building a GUI application, and within a JFrame i have 2 jcombobox's and a JPanel to view certain data. Now when i call the pack() methode in the main class it puts the two jcombobox'es next to my JPanel, which i dont want, because I want them North. Ofcourse I've tried to hard-code it in my code, but it doesn't work after I've called the pack() method. Are there any alternatives to this method?

A: 

All pack does is resize the Window (in this case JFrame) to its preferred size and the preferred sizes of its sub-components. To control the actual location of the sub-components relative to one another you need to use an appropriate LayoutManager.

You might want to check out the Using Layout Managers tutorial.

Adamski
+1  A: 

Only one component can be NORTH, so if you want both ComboBoxes to be NORTH you have to add them into a separate container. This separate container can then be put NORTH.

(Post the source for more exact help.)

volley
A: 

The pack() method just causes the layouting to happen, it has abolutely nothing to do with what is put where.

Most likely you're not using layout managers correctly. Show us your code and we can tell you waht exactly you're doing wrong.

Michael Borgwardt
Well, the code is a bit too large to put it here, so here's a mediafire link to it. Just import the whole project in Eclipse, or whatever you're using. The problem is in the class 'WeerApp' (it's all in Dutch, so you might come across a few comments you don't understand :) ) Link: http://www.mediafire.com/?8t4r4rjbtwf4hcx
Marvin
Marvin, can you post a relevant extract of the code here? No one is going to download and unzip a 3Mb archive and look through all the code. We just need to see the section where you're adding components to your JFrame.
Adamski
A: 

You can avoid using pack by explicitly setting the frame size with setSize and setBounds. However, using pack is usually the preferred way as it leaves the frame layout manager in charge of the frame size.

That being said, the problem you are describing appears to be related to the correct use of a layout manager rather than the sizing of the frame. Have a look at the various layout managers for Swing and how to use them: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html.

bunting