java gui – overlapping panels?

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

Answer

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());

Leave a Reply

Your email address will not be published. Required fields are marked *