Question

I am currently trying to build an expanding panel in Swing (akin the WPF's Expander control) and I'd like to retain the usual methods for manipulating it (i. e. setLayout, add, etc.). Only they should be routed to an embedded panel (the one being shown or hidden).

How would one do that? Overriding every method of JComponent and re-routing that to an embedded JPanel would be cumbersome, but that's the only way I see.

Or should I rather make the embedded panel visible to the outside and force users to use something like ExpanderPanel.getInnerPanel() instead. But then it's no drop-in replacement for JPanel which I think would be nice to have.

Was it helpful?

Solution

In 1.5(ish) Swing routed a few methods to the content pane in JFrame, JApplet, etc. Whilst there appeared to be some usability benefits for those just starting, it doesn't actually fix the problem. So everyone has to deal with a very strangely behaving API. So my advice is to avoid this approach.

OTHER TIPS

Take a look at the JXTaskPane from Swingx project. It already does what you need.

If you have a Container widget which holds a panel you want to show and hide, why not layout your inner panel however you want, then add it to the Container panel, then use static methods against the Container to say

JPanel p = new JPanel(); //do something with the JPanel...

ContainerWidget.setContent(p);

ContainerWidget.expandPanel(p,true);

Would somethign like this work?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top