问题
On one of projects that I'm working on, we decided to change our presentation of some data. Now we are using Netbeans ListView, and works great so far. But now we want to change/expand ListView to works like accordion. We can expand ListView to looks like accordion, but if we add buttons or text inputs to each cell/row we can't attach actionListener to that button. It seems like that ListView listeners are always on top of swing components.
Does anyone have any suggestions how to create accordion with using Netbeans Explorer API?
We could create custom Swing component which acts like that, actually there already exists one on the web. If we create one, we need to connect Lookup with our custom Swing component to works properly with Netbeans APIs. We want to do this only if there are no other options. Googling for such component which uses Netbeans APIs didn't give any useful results.
回答1:
Taken from Toni Epples blog
Take note that:
- This uses JXTaskPane
- This is an example is not dynamic
code example
public class TaskPaneView extends JScrollPane {
private transient ExplorerManager manager;
// create a taskpanecontainer
JXTaskPaneContainer taskpanecontainer = new JXTaskPaneContainer();
/** Listener to nearly everything */
transient Listener managerListener;
/** weak variation of the listener for property change on the explorer manager */
transient PropertyChangeListener wlpc;
/** True, if the selection listener is attached. */
transient boolean listenerActive;
// UI Settings:
Font labelFont = new Font("Segoe UI", Font.BOLD, 14);
Painter backgroundPainter = new MattePainter(Color.white);
public TaskPaneView() {
setViewportView(taskpanecontainer);
}
public void setBackground(Painter background) {
this.backgroundPainter = background;
}
public void setLabelFont(Font labelFont) {
this.labelFont = labelFont;
}
@Override
public void addNotify() {
super.addNotify();
ExplorerManager em = ExplorerManager.find(this);
if (em != manager) {
if (manager != null) {
manager.removePropertyChangeListener(wlpc);
}
manager = em;
manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(managerListener, manager));
Node root = manager.getExploredContext();
setRootNode(root);
} else {
// bugfix #23509, the listener were removed --> add it again
if (!listenerActive && (manager != null)) {
manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(managerListener, manager));
}
}
}
/** Removes listeners.
*/
@Override
public void removeNotify() {
super.removeNotify();
listenerActive = false;
// bugfix #23509, remove useless listeners
if (manager != null) {
manager.removePropertyChangeListener(wlpc);
}
}
private void setRootNode(Node root) {
//throw new UnsupportedOperationException("Not yet implemented");
taskpanecontainer.removeAll();
System.out.println("root node set " + root);
Node[] children = root.getChildren().getNodes();
for (int i = 0; i < children.length; i++) {
Node node = children[i];
JXTaskPane taskPane = new JXTaskPane();
taskPane.setName(node.getName());
taskPane.setCollapsed(true);
taskPane.setTitle(node.getDisplayName());
taskPane.setIcon(new ImageIcon(node.getIcon(BeanInfo.ICON_COLOR_16x16)));
Action [] actions = node.getActions(true);
for (int j = 0; j < actions.length; j++) {
Action action = actions[j];
taskPane.add(action);
}
taskpanecontainer.add(taskPane);
}
}
private final class Listener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
if (ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) {
setRootNode(manager.getExploredContext());
return;
}
}
}
}
回答2:
May be this http://www.codeproject.com/KB/java/java-accordion-menu.aspx ?
回答3:
As it can be used independently, NetBeans' Outline, illustrated here, may be a good alternative. See also the Node
views available in the NetBeans Platform.
来源:https://stackoverflow.com/questions/8724154/netbeans-accordion-component