0

I have a Swing window. At the top I have a drop down based on the drop down value selection specific content should display.

Which is the best layout to use?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Perhaps this will help: [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – Sujay Sep 08 '12 at 23:14

2 Answers2

3

Your question suggests that you are not very familiar with swing so i will suggest to go with the default layout which is FlowLayout.

MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45
  • And why would FlowLayout be good for the OP's requirement? – Sujay Sep 08 '12 at 23:21
  • As he does not have to be concerned about what is actually happening. If one wants to learn driving at first he should try cars rather than lorry in my opinion. – MD. Sahib Bin Mahboob Sep 08 '12 at 23:28
  • 3
    This is very subjective and I wouldn't dive deep into it. However, the best way to learn is to consult the Java Tutorials in my opinion. – Sujay Sep 08 '12 at 23:33
  • 1
    @Sujay I do agree with you :) – MD. Sahib Bin Mahboob Sep 08 '12 at 23:36
  • 2
    @Sujay: I concur and urge experimentation. Also consider `JToolBar`, shown [here](http://stackoverflow.com/a/11949899/230513); it uses a `BoxLayout` internally, but it also works well with `FlowLayout`. – trashgod Sep 09 '12 at 00:32
  • @trashgod It seems quite ironic to me that adding a component like a `JComboBox` to a tool bar is one of the 'very (very) rare' cases I might call `combo.setMaximumSize(combo.getPreferredSize());` – Andrew Thompson Sep 09 '12 at 02:26
  • 1
    @AndrewThompson no, it isn't :-) As always - toolbar or not - the solution is an appropriate LayoutManager. With the slight catch that naughty (again .. sigh) Nimbus unconditionally replaces it. On the other side of the collaboration, the combo sizing hints are ... crap, so require fixing by subclassing. – kleopatra Sep 09 '12 at 07:18
  • @kleopatra I can think of a variety of 'components starting with J' that have the same characteristics.. It seems calling the size methods (or extending) is more indicative of a workaround than a tweak. :( – Andrew Thompson Sep 09 '12 at 07:31
  • 1
    @AndrewThompson maybe :-) Just to clarify: what I regard as buggy behaviour of single-line textComponents (plus combo) is that they don't limit their max _height_ to their pref. As to its width, I can't see much wrong in reporting it unbounded: it's the task of the LayoutManager to limit it depending on context (aka: developer requirements). One of the plus points of BoxLayout is that is _does_ respect max, it could be even better if it allowed constraints to stick to pref instead of stretching - which brings it into the realm of a more advanced manager than it was designed to be :-) – kleopatra Sep 09 '12 at 09:19
  • 1
    @AndrewThompson: I had the same temptation trying to tame the combo's width in a tool bar, shamelessly recycled [here](http://stackoverflow.com/a/11949899/230513); `FlowLayout` was the alternative, seen [here](https://sites.google.com/site/drjohnbmatthews/graphpanel). – trashgod Sep 09 '12 at 12:29
3

The Swing window should have Border Layout. For the North constraint you should put your combo Box wrapped into a simple JPanel, which has a FlowLayout. Based on the ComboBox changes you will change the Center of your Swing window. Please see the sample code at http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

you will need this code in your combo box action:

button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);

I hope it helps.

Mathesoft
  • 111
  • 2
  • 3
    `button.setPreferredSize(new Dimension(200, 100));` What is the intended result of doing that? It is very (very) rare we should set the (preferred/maximum/minimum) size of a component. Other than that, this answer is worthy of a +1. – Andrew Thompson Sep 08 '12 at 23:32
  • It's certainly better than the guy recommending FlowLayout. 1+ – Hovercraft Full Of Eels Sep 09 '12 at 00:33
  • 2
    warning: this might get -1 for the (always incorrect) setPrefSize - please edit your answer and remove that line:-) – kleopatra Sep 09 '12 at 07:21
  • 1
    @HovercraftFullOfEels: The chosen dimensions _may_ be adequate on a particular platform/L&F, but either `FlowLayout` or `BorderLayout.NORTH` will surely use the preferred size. Also, this answer cites `JButton`, while the (somewhat vague) question mentions drop down, presumably a `JConboBox` or `JList`. I'm waiting for Mathsoft's return visit before going for the down-vote. – trashgod Sep 09 '12 at 12:45