1

I need to refactor my application, since I'm running into rendering issues which are probably a result of not properly using the event dispatch thread. In order to do things right, I try to gather information. I already started this thread, which was about the EDT: When exactly are components realized in Swing

Now I would like to know more about the best way to nest Panels.

Let's say I have the following structure:

[PanelA [PanelB [PanelC ]]]

What would be more performant (less internal calls to invalidate())

Order 1 (first inner components then outer):

PanelB.add(PanelC);
PanelA.add(PanelB);

Order 2 (first outer components then inner):

PanelA.add(PanelB);
PanelB.add(PanelC);

If someone also has more info/links/hints etc on how to get the most performant UI I would really appreciate that. Most Tutorial just explain the basics.

A related question: Since all JComponents are Containers, I consider saving some JPanels, by adding components to let's say a JButton. Is this good practice:

JButton b=new JButton();
b.setLayout(new BorderLayout(),BorderLayout.Right);
b.add(new MyComponent());

How can I know which layout a Component uses by default and what could possibly happen, when I change the Component's Layout?

Thanks a lot for your help.

Community
  • 1
  • 1
haferblues
  • 2,155
  • 4
  • 26
  • 39
  • I don't think the order matters when it comes to performance. –  Jan 08 '13 at 14:44
  • If you use `revalidate()`, it does not matter. Otherwise, it is better to start adding the more "nested" panel since a change in a child component can result in a change in the parent container and this, recursively – Guillaume Polet Jan 08 '13 at 14:45
  • Before, calling functions like `pack()/setVisible()`, no matter which path you take. Though calls like `pack()/setVisible()`, must come after you have added everything to the Container. But if you wanted to add a `Component` to an already visible `Container`, then you have to use `revalidate()/repaint()`. Adding `Component`s to a `JButton` looks weird to me, but since I learned such situations are not always trivial, as noted in this thread [Adding JPanel to a JLabel](http://stackoverflow.com/q/10140800/1057230), I guess you can do it :-) – nIcE cOw Jan 08 '13 at 15:50
  • 1
    as to b) - don't. While possible, mis-using components that are not meant to be containers as such is asking for pain. Typically, they have a null layoutManager and laying out their properties (like icon, text) is done by the ui-delegate, which may or may not respect a manager if set - no way to predict the actual outcome. Anyway, why would you want to do that? – kleopatra Jan 08 '13 at 15:51

1 Answers1

2
  1. You should not worry about the order of adding the components, the difference will not be noticeable to the user.

  2. You should not worry about the performance of the UI in general. Swing code in itself will be "fast enough". Performance/responsiveness gets interesting only if you are starting long-running non-UI tasks from the UI.

  3. If you add panels to buttons, it will confuse the user. You can check the source code of the components to see their layout managers (but this is very rarely necessary)

lbalazscs
  • 17,474
  • 7
  • 42
  • 50