tags:

views:

389

answers:

3

Hi all,

i'm just trying to create this little simulator. in a gui, i have two main components - a map, taking up most of the window, and a control panel on the right hand side. now, i'd like to add a time slider across the bottom of the window (running only under the map, not under the control panel). i can do it in the runner class (which initialises the main window), but it should logically belong to the control panel - it fires all the relevant events. is there a way to do it? what swing components should i use and how should i wrap them? (i tried using netbeans for this, but, this being the first time i ever used it, i had little luck).

thanks a lot for your help

A: 

A layout manager like MiG layout or DesignGridLayout will help you layout your controls better. Layout of the controls is one thing and logical separation is another. You can have all the controls in the control panel at the right side and the slider at the bottom be members of the same logical unit.

kgiannakakis
+1  A: 

You could use BorderLayout to achieve the desired result very simply; e.g.

JPanel pnl = new JPanel(new BorderLayout());

pnl.add(mapPnl, BorderLayout.CENTER);
pnl.add(controlPnl, BorderLayout.EAST);
pnl.add(timerPnl, BorderLayout.SOUTH);

If you anticipate more components being added to your top level JPanel I would suggest looking into GridBagLayout as it offers greater flexibility.

The fact that your controls and timer are related isn't relevant for the layout of the visual components. Typically you would maintain this relationship at the business object level; e.g.

Controller controller = new Controller();
JPanel controlPnl = new MyControlPanel(controller);
JPanel timerPnl = new MyTimerPanel(controller.getTimer());
Adamski
Why would you suggest that anyone looks at GridBagLayout? Yes, it's one of the most powerful. But it's also a pain to wrap your head around. I prefer breaking things into smaller panels and using the easier layout managers, just to keep things sane.
Thomas Owens
@Thomas: I typically break things into smaller panels when possible but find GridBagLayout particularly useful for complex dialogs. We've written some static utility methods to keep our code concise and readable - I would say mastering it is worth the learning curve.
Adamski
@Thomas - `GridBagLayout` is incredibly simple - and very powerful. Look at it again as it is the layout where you can achieve almost anything you want to
oxbow_lakes
thanks for the help - somewhere in my head i really mixed the gui and business logic. p.s. this was the first time i've used this place - the amount of brainpower here is a bit intimidating, but certainly helpful :)
A: 

Just use multiple panels to control the different regions like this:

JPanel centerContent = new JPanel(new BorderLayout());
centerContent.add(map, BorderLayout.CENTER);
centerContent.add(timeSlider, BorderLayout.SOUTH);

JPanel content = new JPanel(new BorderLayout());
content.add(controlPanel, BorderLayout.WEST);
content.add(centerContent, BorderLayout.CENTER);

Using this approach the layout manager - java.awt.BorderLayout in this case - takes care of the resizing of the window containing the controls.

Nick Holt