Um herauszufinden, welcher Knoten selektiert wurde, muss ein TreeSelectionListener registriert werden. Über diesen kann auch durch folgenden Code auf ein Doppelklick reagiert werden:
final JTree tree = ...;
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
mySingleClick(selRow, selPath);
}
else if(e.getClickCount() == 2) {
myDoubleClick(selRow, selPath);
}
}
}
};
tree.addMouseListener(ml);
Um das Anzeigen (Rendern) zu beeinflussen, kann man seinen eigenen TreeCellRenderer über die Methode setCellRenderer registrieren. Seinen eigenen Editor (TreeCellEditor) kann man mit Hilfe der Methode setCellEditor setzen.
Wie bei allen JComponents wird eine InputMap und eine ActionMap dafür benutzt, eine Aktion (Action) mit einem Tastendruck (KeyStroke) zu verbinden.
Methoden |
protected static TreeModel | getDefaultTreeModel() |
protected static TreeModel | createTreeModel(Object value) |
public TreeUI | getUI() |
public void | setUI(TreeUI ui) |
public void | updateUI() |
public String | getUIClassID() |
public TreeCellRenderer | getCellRenderer() |
public void | setCellRenderer(TreeCellRenderer x) |
public void | setEditable(boolean flag) |
public boolean | isEditable() |
public void | setCellEditor(TreeCellEditor cellEditor) |
public TreeCellEditor | getCellEditor() |
public TreeModel | getModel() |
public void | setModel(TreeModel newModel) |
public boolean | isRootVisible() |
public void | setRootVisible(boolean rootVisible) |
public void | setShowsRootHandles(boolean newValue) |
public boolean | getShowsRootHandles() |
public void | setRowHeight(int rowHeight) |
public int | getRowHeight() |
public boolean | isFixedRowHeight() |
public void | setLargeModel(boolean newValue) |
public boolean | isLargeModel() |
public void | setInvokesStopCellEditing(boolean newValue) |
public boolean | getInvokesStopCellEditing() |
public void | setScrollsOnExpand(boolean newValue) |
public boolean | getScrollsOnExpand() |
public void | setToggleClickCount(int clickCount) ![[1.3]](../../images/marg13.gif) |
public int | getToggleClickCount() ![[1.3]](../../images/marg13.gif) |
public void | setExpandsSelectedPaths(boolean newValue) ![[1.3]](../../images/marg13.gif) |
public boolean | getExpandsSelectedPaths() ![[1.3]](../../images/marg13.gif) |
public void | setDragEnabled(boolean b) ![[1.4]](../../images/marg14.gif) |
public boolean | getDragEnabled() ![[1.4]](../../images/marg14.gif) |
public boolean | isPathEditable(TreePath path) |
public String | getToolTipText(MouseEvent event) |
public String | convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) |
public int | getRowCount() |
public void | setSelectionPath(TreePath path) |
public void | setSelectionPaths(TreePath[] paths) |
public void | setLeadSelectionPath(TreePath newPath) ![[1.3]](../../images/marg13.gif) |
public void | setAnchorSelectionPath(TreePath newPath) ![[1.3]](../../images/marg13.gif) |
public void | setSelectionRow(int row) |
public void | setSelectionRows(int[] rows) |
public void | addSelectionPath(TreePath path) |
public void | addSelectionPaths(TreePath[] paths) |
public void | addSelectionRow(int row) |
public void | addSelectionRows(int[] rows) |
public Object | getLastSelectedPathComponent() |
public TreePath | getLeadSelectionPath() |
public TreePath | getAnchorSelectionPath() ![[1.3]](../../images/marg13.gif) |
public TreePath | getSelectionPath() |
public TreePath[] | getSelectionPaths() |
public int[] | getSelectionRows() |
public int | getSelectionCount() |
public int | getMinSelectionRow() |
public int | getMaxSelectionRow() |
public int | getLeadSelectionRow() |
public boolean | isPathSelected(TreePath path) |
public boolean | isRowSelected(int row) |
public Enumeration | getExpandedDescendants(TreePath parent) |
public boolean | hasBeenExpanded(TreePath path) |
public boolean | isExpanded(TreePath path) |
public boolean | isExpanded(int row) |
public boolean | isCollapsed(TreePath path) |
public boolean | isCollapsed(int row) |
public void | makeVisible(TreePath path) |
public boolean | isVisible(TreePath path) |
public Rectangle | getPathBounds(TreePath path) |
public Rectangle | getRowBounds(int row) |
public void | scrollPathToVisible(TreePath path) |
public void | scrollRowToVisible(int row) |
public TreePath | getPathForRow(int row) |
public int | getRowForPath(TreePath path) |
public void | expandPath(TreePath path) |
public void | expandRow(int row) |
public void | collapsePath(TreePath path) |
public void | collapseRow(int row) |
public TreePath | getPathForLocation(int x, int y) |
public int | getRowForLocation(int x, int y) |
public TreePath | getClosestPathForLocation(int x, int y) |
public int | getClosestRowForLocation(int x, int y) |
public boolean | isEditing() |
public boolean | stopEditing() |
public void | cancelEditing() |
public void | startEditingAtPath(TreePath path) |
public TreePath | getEditingPath() |
public void | setSelectionModel(TreeSelectionModel selectionModel) |
public TreeSelectionModel | getSelectionModel() |
protected TreePath[] | getPathBetweenRows(int index0, int index1) |
public void | setSelectionInterval(int index0, int index1) |
public void | addSelectionInterval(int index0, int index1) |
public void | removeSelectionInterval(int index0, int index1) |
public void | removeSelectionPath(TreePath path) |
public void | removeSelectionPaths(TreePath[] paths) |
public void | removeSelectionRow(int row) |
public void | removeSelectionRows(int[] rows) |
public void | clearSelection() |
public boolean | isSelectionEmpty() |
public void | addTreeExpansionListener(TreeExpansionListener tel) |
public void | removeTreeExpansionListener(TreeExpansionListener tel) |
public TreeExpansionListener[] | getTreeExpansionListeners() ![[1.4]](../../images/marg14.gif) |
public void | addTreeWillExpandListener(TreeWillExpandListener tel) |
public void | removeTreeWillExpandListener(TreeWillExpandListener tel) |
public TreeWillExpandListener[] | getTreeWillExpandListeners() ![[1.4]](../../images/marg14.gif) |
public void | fireTreeExpanded(TreePath path) |
public void | fireTreeCollapsed(TreePath path) |
public void | fireTreeWillExpand(TreePath path) |
public void | fireTreeWillCollapse(TreePath path) |
public void | addTreeSelectionListener(TreeSelectionListener tsl) |
public void | removeTreeSelectionListener(TreeSelectionListener tsl) |
public TreeSelectionListener[] | getTreeSelectionListeners() ![[1.4]](../../images/marg14.gif) |
protected void | fireValueChanged(TreeSelectionEvent e) |
public void | treeDidChange() |
public void | setVisibleRowCount(int newCount) |
public int | getVisibleRowCount() |
public TreePath | getNextMatch(String prefix, int startingRow, Position.Bias bias) ![[1.4]](../../images/marg14.gif) |
public Dimension | getPreferredScrollableViewportSize() |
public int | getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) |
public int | getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) |
public boolean | getScrollableTracksViewportWidth() |
public boolean | getScrollableTracksViewportHeight() |
protected void | setExpandedState(TreePath path, boolean state) |
protected Enumeration | getDescendantToggledPaths(TreePath parent) |
protected void | removeDescendantToggledPaths(Enumeration toRemove) |
protected void | clearToggledPaths() |
protected TreeModelListener | createTreeModelListener() |
protected boolean | removeDescendantSelectedPaths(TreePath path, boolean includePath) ![[1.3]](../../images/marg13.gif) |
protected String | paramString() |
public AccessibleContext | getAccessibleContext() |