📄 jtree.java
字号:
*/ public String getUIClassID() { return "TreeUI"; } /** * Gets the AccessibleContext associated with this * <code>JTree</code>. * * @return the associated context */ public AccessibleContext getAccessibleContext() { return new AccessibleJTree(); } /** * Returns the preferred viewport size. * * @return the preferred size */ public Dimension getPreferredScrollableViewportSize() { return new Dimension (getPreferredSize().width, getVisibleRowCount()*getRowHeight()); } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 1; } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 1; } public boolean getScrollableTracksViewportHeight() { if (getParent() instanceof JViewport) return ((JViewport) getParent()).getHeight() > getPreferredSize().height; return false; } public boolean getScrollableTracksViewportWidth() { if (getParent() instanceof JViewport) return ((JViewport) getParent()).getWidth() > getPreferredSize().width; return false; } /** * Adds a <code>TreeExpansionListener</code> object to the tree. * * @param listener the listener to add */ public void addTreeExpansionListener(TreeExpansionListener listener) { listenerList.add(TreeExpansionListener.class, listener); } /** * Removes a <code>TreeExpansionListener</code> object from the tree. * * @param listener the listener to remove */ public void removeTreeExpansionListener(TreeExpansionListener listener) { listenerList.remove(TreeExpansionListener.class, listener); } /** * Returns all added <code>TreeExpansionListener</code> objects. * * @return an array of listeners */ public TreeExpansionListener[] getTreeExpansionListeners() { return (TreeExpansionListener[]) getListeners(TreeExpansionListener.class); } /** * Notifies all listeners that the tree was collapsed. * * @param path the path to the node that was collapsed */ public void fireTreeCollapsed(TreePath path) { TreeExpansionEvent event = new TreeExpansionEvent(this, path); TreeExpansionListener[] listeners = getTreeExpansionListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].treeCollapsed(event); } /** * Notifies all listeners that the tree was expanded. * * @param path the path to the node that was expanded */ public void fireTreeExpanded(TreePath path) { TreeExpansionEvent event = new TreeExpansionEvent(this, path); TreeExpansionListener[] listeners = getTreeExpansionListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].treeExpanded(event); } /** * Adds a <code>TreeSelctionListener</code> object to the tree. * * @param listener the listener to add */ public void addTreeSelectionListener(TreeSelectionListener listener) { listenerList.add(TreeSelectionListener.class, listener); } /** * Removes a <code>TreeSelectionListener</code> object from the tree. * * @param listener the listener to remove */ public void removeTreeSelectionListener(TreeSelectionListener listener) { listenerList.remove(TreeSelectionListener.class, listener); } /** * Returns all added <code>TreeSelectionListener</code> objects. * * @return an array of listeners */ public TreeSelectionListener[] getTreeSelectionListeners() { return (TreeSelectionListener[]) getListeners(TreeSelectionListener.class); } /** * Notifies all listeners when the selection of the tree changed. * * @param event the event to send */ protected void fireValueChanged(TreeSelectionEvent event) { TreeSelectionListener[] listeners = getTreeSelectionListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].valueChanged(event); } /** * Adds a <code>TreeWillExpandListener</code> object to the tree. * * @param listener the listener to add */ public void addTreeWillExpandListener(TreeWillExpandListener listener) { listenerList.add(TreeWillExpandListener.class, listener); } /** * Removes a <code>TreeWillExpandListener</code> object from the tree. * * @param listener the listener to remove */ public void removeTreeWillExpandListener(TreeWillExpandListener listener) { listenerList.remove(TreeWillExpandListener.class, listener); } /** * Returns all added <code>TreeWillExpandListener</code> objects. * * @return an array of listeners */ public TreeWillExpandListener[] getTreeWillExpandListeners() { return (TreeWillExpandListener[]) getListeners(TreeWillExpandListener.class); } /** * Notifies all listeners that the tree will collapse. * * @param path the path to the node that will collapse */ public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException { TreeExpansionEvent event = new TreeExpansionEvent(this, path); TreeWillExpandListener[] listeners = getTreeWillExpandListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].treeWillCollapse(event); } /** * Notifies all listeners that the tree will expand. * * @param path the path to the node that will expand */ public void fireTreeWillExpand(TreePath path) throws ExpandVetoException { TreeExpansionEvent event = new TreeExpansionEvent(this, path); TreeWillExpandListener[] listeners = getTreeWillExpandListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].treeWillExpand(event); } /** * Returns the model of this <code>JTree</code> object. * * @return the associated <code>TreeModel</code> */ public TreeModel getModel() { return treeModel; } /** * Sets the model to use in <code>JTree</code>. * * @param model the <code>TreeModel</code> to use */ public void setModel(TreeModel model) { if (treeModel == model) return; // add treeModelListener to the new model if (treeModelListener == null) treeModelListener = createTreeModelListener(); if (model != null) // as setModel(null) is allowed model.addTreeModelListener(treeModelListener); TreeModel oldValue = treeModel; treeModel = model; firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model); updateUI(); } /** * Checks if this <code>JTree</code> object is editable. * * @return <code>true</code> if this tree object is editable, * <code>false</code> otherwise */ public boolean isEditable() { return editable; } /** * Sets the <code>editable</code> property. * * @param flag <code>true</code> to make this tree object editable, * <code>false</code> otherwise */ public void setEditable(boolean flag) { if (editable == flag) return; boolean oldValue = editable; editable = flag; firePropertyChange(EDITABLE_PROPERTY, oldValue, editable); } /** * Checks if the root element is visible. * * @return <code>true</code> if the root element is visible, * <code>false</code> otherwise */ public boolean isRootVisible() { return rootVisible; } public void setRootVisible(boolean flag) { if (rootVisible == flag) return; boolean oldValue = rootVisible; rootVisible = flag; firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, flag); } public boolean getShowsRootHandles() { return showsRootHandles; } public void setShowsRootHandles(boolean flag) { if (showsRootHandles == flag) return; boolean oldValue = showsRootHandles; showsRootHandles = flag; firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue, flag); } public TreeCellEditor getCellEditor() { return cellEditor; } public void setCellEditor(TreeCellEditor editor) { if (cellEditor == editor) return; TreeCellEditor oldValue = cellEditor; cellEditor = editor; firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor); } public TreeCellRenderer getCellRenderer() { return cellRenderer; } public void setCellRenderer(TreeCellRenderer newRenderer) { if (cellRenderer == newRenderer) return; TreeCellRenderer oldValue = cellRenderer; cellRenderer = newRenderer; firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, newRenderer); } public TreeSelectionModel getSelectionModel() { return selectionModel; } public void setSelectionModel(TreeSelectionModel model) { if (selectionModel == model) return; if (selectionModel != null) selectionModel.removeTreeSelectionListener(selectionRedirector); TreeSelectionModel oldValue = selectionModel; selectionModel = model; if (selectionModel != null) selectionModel.addTreeSelectionListener(selectionRedirector); firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model); revalidate(); repaint(); } public int getVisibleRowCount() { return visibleRowCount; } public void setVisibleRowCount(int rows) { if (visibleRowCount == rows) return; int oldValue = visibleRowCount; visibleRowCount = rows; firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, rows); } public boolean isLargeModel() { return largeModel; } public void setLargeModel(boolean large) { if (largeModel == large) return; boolean oldValue = largeModel; largeModel = large; firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large); } public int getRowHeight() { return rowHeight; } public void setRowHeight(int height) { if (rowHeight == height) return; int oldValue = rowHeight; rowHeight = height; firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height); } public boolean isFixedRowHeight() { return rowHeight > 0; } public boolean getInvokesStopCellEditing() { return invokesStopCellEditing; } public void setInvokesStopCellEditing(boolean invoke) { if (invokesStopCellEditing == invoke) return; boolean oldValue = invokesStopCellEditing; invokesStopCellEditing = invoke; firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY, oldValue, invoke); } /** * @since 1.3 */ public int getToggleClickCount() { return toggleClickCount; } /** * @since 1.3 */ public void setToggleClickCount(int count) { if (toggleClickCount == count) return; int oldValue = toggleClickCount; toggleClickCount = count; firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count); } public void scrollPathToVisible(TreePath path) { if (path == null) return; Object[] oPath = path.getPath(); TreePath temp = new TreePath(oPath[0]); boolean stop = false; int i = 1; while (!stop) { while (isVisible(temp)) if (i < oPath.length) temp = temp.pathByAddingChild(oPath[i++]); else { stop = true; break; } makeVisible(temp); } Rectangle rect = getPathBounds(path); scrollRectToVisible(rect); revalidate(); repaint(); } public void scrollRowToVisible(int row) { scrollPathToVisible(getPathForRow(row)); } public boolean getScrollsOnExpand() { return scrollsOnExpand; } public void setScrollsOnExpand(boolean scroll) { if (scrollsOnExpand == scroll) return; boolean oldValue = scrollsOnExpand; scrollsOnExpand = scroll; firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll); } public void setSelectionPath(TreePath path) { selectionModel.setSelectionPath(path); } public void setSelectionPaths(TreePath[] paths) { selectionModel.setSelectionPaths(paths); } public void setSelectionRow(int row) { TreePath path = getPathForRow(row); if (path != null) selectionModel.setSelectionPath(path); } public void setSelectionRows(int[] rows) { // Make sure we have an UI so getPathForRow() does not return null. if (rows == null || getUI() == null) return; TreePath[] paths = new TreePath[rows.length]; for (int i = rows.length - 1; i >= 0; --i) paths[i] = getPathForRow(rows[i]); setSelectionPaths(paths); } public void setSelectionInterval(int index0, int index1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -