tags:

views:

55

answers:

3

Hi,

I'm using Java Swing and I have the following problem:

I have a class TnaiPanel that extends JPanel. In this class I am creating 3 components and then lay them out in a horizontal line using a BoxLayout.

Also, I have a class TnaimDinamimPanel that also extends JPanel. This class contains multiple occurances of TnaiPanel, layed out vertically using a BoxLayout.

Also, I have a class MainFrame that extends JFrame. This frame contains a menu-bar and one main panel. The main panel can change (when choosing a certain menu-item, I create a new panel and set it to show as the main panel of the frame).

Now, for some reason I get "BoxLayout can't be shared" when I add the newly created TnaimDinamimPanel to the components of the frame.

I don't mind using different layout objects. The result I want to get is a sort of "table" of components, where each TnaiPanel will have fixed component sizes and spacing, essentially serving the role os a "row" in the "table".

Thanks, Malki.

+3  A: 

You probably create only a single BoxLayout instance. Create a new one each time you need one (i.e. one per TnaiPanel, one per TnaimDinamimPanel and probably one per MainFrame).

Aaron Digulla
+1  A: 

Try MiGLayout instead.

Skeptic
+2  A: 

To answer the second part of your question, ie "having a table of components", I would say that you can't do it with different panels, except if you start setting the individual min, pref and max sizes of components and panels, which is highly unadvised.

If you need to have correct alignment as in a table of components, then you need to put all your components in one panel, which also means you need to use only one layout. However, the only default swing layout that can allow you to do what you want is the GridBagLayout. Actually the GroupLayout (java 6) would also fit the bill but it absolutely requires a graphical designer (eg the one within netbeans).

If, like me, you are allergic to builders, then you'd better use one 3rd-party LayoutManager that is intended to be use programmatically (I would not consider GridBagLayout to be in this category although I have already used it that way in the past).

MigLayout (as suggested by Skeptic) is one option. Another option is DesignGridLayout which might fit your purpose better and is easier to use than MigLayout.

jfpoilpret
Sadly, DesignGridLayout seems to be dead (inactive for a year or so).
Skeptic
+1 Inactive but fairly mature; there's a nice JWS demo, too.
trashgod
It's correct that DesignGridLayout didn't have much activity recently, but I wouldn't say the project is dead; it's rather that its author is over-busy and has to manage priorities as well as can be (like for all Open Source authors I believe). As long as the author is still alive and hasn't officially announced any intention to drop the project, the project can itself be considered alive!
jfpoilpret