📄 jtable.java
字号:
if (c != null) c.addCellEditorListener(this); cellEditor = c; } /** * Set the value of the {@link #dragEnabled} property. * * @param d The new value of the dragEnabled property */ public void setDragEnabled(boolean d) { dragEnabled = d; } /** * Set the value of the {@link #gridColor} property. * * @param g The new value of the gridColor property */ public void setGridColor(Color g) { gridColor = g; repaint(); } /** * Set the value of the {@link #intercellSpacing} property. * * @param i The new value of the intercellSpacing property */ public void setIntercellSpacing(Dimension i) { rowMargin = i.height; columnModel.setColumnMargin(i.width); repaint(); } /** * Set the value of the {@link #preferredViewportSize} property. * * @param p The new value of the preferredViewportSize property */ public void setPreferredScrollableViewportSize(Dimension p) { preferredViewportSize = p; revalidate(); repaint(); } /** * <p>Set the value of the {@link #selectionBackground} property.</p> * * <p>Fire a PropertyChangeEvent with name {@link * #SELECTION_BACKGROUND_CHANGED_PROPERTY} to registered listeners, if * selectionBackground changed.</p> * * @param s The new value of the selectionBackground property */ public void setSelectionBackground(Color s) { Color tmp = selectionBackground; selectionBackground = s; if (((tmp == null && s != null) || (s == null && tmp != null) || (tmp != null && s != null && !tmp.equals(s)))) firePropertyChange(SELECTION_BACKGROUND_CHANGED_PROPERTY, tmp, s); repaint(); } /** * <p>Set the value of the {@link #selectionForeground} property.</p> * * <p>Fire a PropertyChangeEvent with name {@link * SELECTION_FOREGROUND_CHANGED_PROPERTY} to registered listeners, if * selectionForeground changed.</p> * * @param s The new value of the selectionForeground property */ public void setSelectionForeground(Color s) { Color tmp = selectionForeground; selectionForeground = s; if (((tmp == null && s != null) || (s == null && tmp != null) || (tmp != null && s != null && !tmp.equals(s)))) firePropertyChange(SELECTION_FOREGROUND_CHANGED_PROPERTY, tmp, s); repaint(); } /** * Set the value of the {@link #showGrid} property. * * @param s The new value of the showGrid property */ public void setShowGrid(boolean s) { setShowVerticalLines(s); setShowHorizontalLines(s); } /** * Set the value of the {@link #showHorizontalLines} property. * * @param s The new value of the showHorizontalLines property */ public void setShowHorizontalLines(boolean s) { showHorizontalLines = s; repaint(); } /** * Set the value of the {@link #showVerticalLines} property. * * @param s The new value of the showVerticalLines property */ public void setShowVerticalLines(boolean s) { showVerticalLines = s; repaint(); } /** * Set the value of the {@link #tableHeader} property. * * @param t The new value of the tableHeader property */ public void setTableHeader(JTableHeader t) { if (tableHeader != null) tableHeader.setTable(null); tableHeader = t; if (tableHeader != null) tableHeader.setTable(this); revalidate(); repaint(); } protected void configureEnclosingScrollPane() { JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this); if (jsp != null && tableHeader != null) { jsp.setColumnHeaderView(tableHeader); } } protected void unconfigureEnclosingScrollPane() { JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this); if (jsp != null) { jsp.setColumnHeaderView(null); } } public void addNotify() { super.addNotify(); configureEnclosingScrollPane(); } public void removeNotify() { super.addNotify(); unconfigureEnclosingScrollPane(); } /** * Sun javadocs describe an unusual implementation of * <code>doLayout</code> which involves some private interfaces. We try * to implement the same algorithm as is documented, but using the * columnModel directly. We still use a private helper method, but it has * a simpler signature. */ private void distributeSpill(TableColumn[] cols, int spill) { int MIN = 0; int MAX = 0; int PREF = 0; int[] min = new int[cols.length]; int[] max = new int[cols.length]; int[] pref = new int[cols.length]; for (int i = 0; i < cols.length; ++i) { pref[i] = cols[i].getPreferredWidth(); min[i] = cols[i].getMinWidth(); max[i] = cols[i].getMaxWidth(); PREF += pref[i]; MIN += min[i]; MAX += max[i]; } for (int i = 0; i < cols.length; ++i) { int adj = 0; if (spill > 0) adj = (spill * (pref[i] - min[i])) / (PREF - MIN); else adj = (spill * (max[i] - pref[i])) / (MAX - PREF); cols[i].setWidth(pref[i] + adj); } } public void doLayout() { TableColumn resizingColumn = null; int ncols = getColumnCount(); if (ncols < 1) return; int[] pref = new int[ncols]; int prefSum = 0; int rCol = -1; if (tableHeader != null) resizingColumn = tableHeader.getResizingColumn(); for (int i = 0; i < ncols; ++i) { TableColumn col = columnModel.getColumn(i); int p = col.getWidth(); pref[i] = p; prefSum += p; if (resizingColumn == col) rCol = i; } int spill = prefSum - getWidth(); if (resizingColumn != null) { TableColumn col; TableColumn [] cols; switch (getAutoResizeMode()) { case AUTO_RESIZE_LAST_COLUMN: col = columnModel.getColumn(ncols-1); col.setWidth(col.getPreferredWidth() + spill); break; case AUTO_RESIZE_NEXT_COLUMN: col = columnModel.getColumn(ncols-1); col.setWidth(col.getPreferredWidth() + spill); break; case AUTO_RESIZE_ALL_COLUMNS: cols = new TableColumn[ncols]; for (int i = 0; i < ncols; ++i) cols[i] = columnModel.getColumn(i); distributeSpill(cols, spill); break; case AUTO_RESIZE_SUBSEQUENT_COLUMNS: cols = new TableColumn[ncols]; for (int i = rCol; i < ncols; ++i) cols[i] = columnModel.getColumn(i); distributeSpill(cols, spill); break; case AUTO_RESIZE_OFF: default: } } else { TableColumn [] cols = new TableColumn[ncols]; for (int i = 0; i < ncols; ++i) cols[i] = columnModel.getColumn(i); distributeSpill(cols, spill); } } /** * @deprecated Replaced by <code>doLayout()</code> */ public void sizeColumnsToFit(boolean lastColumnOnly) { doLayout(); } /** * Obsolete since JDK 1.4. Please use <code>doLayout()</code>. */ public void sizeColumnsToFit(int resizingColumn) { doLayout(); } public String getUIClassID() { return "TableUI"; } /** * This method returns the table's UI delegate. * * @return The table's UI delegate. */ public TableUI getUI() { return (TableUI) ui; } /** * This method sets the table's UI delegate. * * @param ui The table's UI delegate. */ public void setUI(TableUI ui) { super.setUI(ui); } public void updateUI() { setUI((TableUI) UIManager.getUI(this)); revalidate(); repaint(); } public Class getColumnClass(int column) { return dataModel.getColumnClass(column); } public String getColumnName(int column) { return dataModel.getColumnName(column); } public int getEditingColumn() { return editingColumn; } public void setEditingColumn(int column) { editingColumn = column; } public int getEditingRow() { return editingRow; } public void setEditingRow(int column) { editingRow = column; } public Component getEditorComponent() { return editorComp; } public boolean isEditing() { return editorComp != null; } public void setDefaultEditor(Class columnClass, TableCellEditor editor) { if (editor != null) defaultEditorsByColumnClass.put(columnClass, editor); else defaultEditorsByColumnClass.remove(columnClass); } public void addColumnSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getColumnCount()-1) || index1 < 0 || index1 > (getColumnCount()-1))) throw new IllegalArgumentException("Column index out of range."); getColumnModel().getSelectionModel().addSelectionInterval(index0, index1); } public void addRowSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getRowCount()-1) || index1 < 0 || index1 > (getRowCount()-1))) throw new IllegalArgumentException("Row index out of range."); getSelectionModel().addSelectionInterval(index0, index1); } public void setColumnSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getColumnCount()-1) || index1 < 0 || index1 > (getColumnCount()-1))) throw new IllegalArgumentException("Column index out of range."); getColumnModel().getSelectionModel().setSelectionInterval(index0, index1); } public void setRowSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getRowCount()-1) || index1 < 0 || index1 > (getRowCount()-1))) throw new IllegalArgumentException("Row index out of range."); getSelectionModel().setSelectionInterval(index0, index1); } public void removeColumnSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getColumnCount()-1) || index1 < 0 || index1 > (getColumnCount()-1))) throw new IllegalArgumentException("Column index out of range."); getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1); } public void removeRowSelectionInterval(int index0, int index1) { if ((index0 < 0 || index0 > (getRowCount()-1) || index1 < 0 || index1 > (getRowCount()-1))) throw new IllegalArgumentException("Row index out of range."); getSelectionModel().removeSelectionInterval(index0, index1); } public boolean isColumnSelected(int column) { return getColumnModel().getSelectionModel().isSelectedIndex(column); } public boolean isRowSelected(int row) { return getSelectionModel().isSelectedIndex(row); } public boolean isCellSelected(int row, int column) { return isRowSelected(row) && isColumnSelected(column); } public void selectAll() { setColumnSelectionInterval(0, getColumnCount() - 1); setRowSelectionInterval(0, getRowCount() - 1); } public Object getValueAt(int row, int column) { return dataModel.getValueAt(row, convertColumnIndexToModel(column)); } public void setValueAt(Object value, int row, int column) { dataModel.setValueAt(value, row, convertColumnIndexToModel(column)); } public TableColumn getColumn(Object identifier) { return columnModel.getColumn(columnModel.getColumnIndex(identifier)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -