📄 jtable.java
字号:
if (includeSpacing) return new Rectangle(x, y, width, height); else return new Rectangle(x, y, width - x_gap, height - y_gap); } public void clearSelection() { selectionModel.clearSelection(); getColumnModel().getSelectionModel().clearSelection(); } /** * Get the value of the selectedRow property by delegation to * the {@link ListSelectionModel#getMinSelectionIndex} method of the * {@link #selectionModel} field. * * @return The current value of the selectedRow property */ public int getSelectedRow () { return selectionModel.getMinSelectionIndex(); } /** * Get the value of the {@link #selectionModel} property. * * @return The current value of the property */ public ListSelectionModel getSelectionModel() { //Neither Sun nor IBM returns null if rowSelection not allowed return selectionModel; } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { if (orientation == SwingConstants.VERTICAL) return visibleRect.height * direction; else return visibleRect.width * direction; } /** * Get the value of the <code>scrollableTracksViewportHeight</code> property. * * @return The constant value <code>false</code> */ public boolean getScrollableTracksViewportHeight() { return false; } /** * Get the value of the <code>scrollableTracksViewportWidth</code> property. * * @return <code>true</code> unless the {@link #autoResizeMode} property is * <code>AUTO_RESIZE_OFF</code> */ public boolean getScrollableTracksViewportWidth() { if (autoResizeMode == AUTO_RESIZE_OFF) return false; else return true; } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { // FIXME: I don't exactly know what sun does here. in both cases they // pick values which do *not* simply expose the next cell in a given // scroll direction. if (orientation == SwingConstants.VERTICAL) return direction * rowHeight; else { int sum = 0; for (int i = 0; i < getColumnCount(); ++i) sum += columnModel.getColumn(0).getWidth(); int inc = getColumnCount() == 0 ? 10 : sum / getColumnCount(); return direction * inc; } } public TableCellEditor getCellEditor(int row, int column) { TableCellEditor editor = columnModel.getColumn(column).getCellEditor(); if (editor == null) editor = getDefaultEditor(dataModel.getColumnClass(column)); return editor; } public TableCellEditor getDefaultEditor(Class columnClass) { if (defaultEditorsByColumnClass.containsKey(columnClass)) return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); else { // FIXME: We have at least an editor for Object.class in our defaults. TableCellEditor r = new DefaultCellEditor(new JTextField()); defaultEditorsByColumnClass.put(columnClass, r); return r; } } public TableCellRenderer getCellRenderer(int row, int column) { TableCellRenderer renderer = columnModel.getColumn(column).getCellRenderer(); if (renderer == null) renderer = getDefaultRenderer(dataModel.getColumnClass(column)); return renderer; } public void setDefaultRenderer(Class columnClass, TableCellRenderer rend) { defaultRenderersByColumnClass.put(columnClass, rend); } public TableCellRenderer getDefaultRenderer(Class columnClass) { if (defaultRenderersByColumnClass.containsKey(columnClass)) return (TableCellRenderer) defaultRenderersByColumnClass.get(columnClass); else { TableCellRenderer r = new DefaultTableCellRenderer(); defaultRenderersByColumnClass.put(columnClass, r); return r; } } public int convertColumnIndexToModel(int vc) { if (vc < 0) return vc; else return columnModel.getColumn(vc).getModelIndex(); } public int convertColumnIndexToView(int mc) { if (mc < 0) return mc; int ncols = getColumnCount(); for (int vc = 0; vc < ncols; ++vc) { if (columnModel.getColumn(vc).getModelIndex() == mc) return vc; } return -1; } public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { boolean rsa = getRowSelectionAllowed(); boolean csa = getColumnSelectionAllowed(); boolean rs = rsa ? getSelectionModel().isSelectedIndex(row) : false; boolean cs = csa ? columnModel.getSelectionModel().isSelectedIndex(column) : false; boolean isSelected = ((rsa && csa && rs && cs) || (rsa && !csa && rs) || (!rsa && csa && cs)); return renderer.getTableCellRendererComponent(this, dataModel.getValueAt(row, convertColumnIndexToModel(column)), isSelected, false, // hasFocus row, column); } /** * Get the value of the {@link #autoCreateColumnsFromModel} property. * * @return The current value of the property */ public boolean getAutoCreateColumnsFromModel() { return autoCreateColumnsFromModel; } /** * Get the value of the {@link #autoResizeMode} property. * * @return The current value of the property */ public int getAutoResizeMode() { return autoResizeMode; } /** * Get the value of the {@link #rowHeight} property. * * @return The current value of the property */ public int getRowHeight() { return rowHeight; } /** * Get the height of the specified row. * * @param row the row whose height to return */ public int getRowHeight(int row) { // FIXME: return the height of the specified row // which may be different from the general rowHeight return rowHeight; } /** * Get the value of the {@link #rowMargin} property. * * @return The current value of the property */ public int getRowMargin() { return rowMargin; } /** * Get the value of the {@link #rowSelectionAllowed} property. * * @return The current value of the property */ public boolean getRowSelectionAllowed() { return rowSelectionAllowed; } /** * Get the value of the {@link #cellSelectionEnabled} property. * * @return The current value of the property */ public boolean getCellSelectionEnabled() { return getColumnSelectionAllowed() && getRowSelectionAllowed(); } /** * Get the value of the {@link #dataModel} property. * * @return The current value of the property */ public TableModel getModel() { return dataModel; } /** * Get the value of the <code>columnCount</code> property by * delegation to the @{link #columnModel} field. * * @return The current value of the columnCount property */ public int getColumnCount() { return columnModel.getColumnCount(); } /** * Get the value of the <code>rowCount</code> property by * delegation to the @{link #dataModel} field. * * @return The current value of the rowCount property */ public int getRowCount() { return dataModel.getRowCount(); } /** * Get the value of the {@link #columnModel} property. * * @return The current value of the property */ public TableColumnModel getColumnModel() { return columnModel; } /** * Get the value of the <code>selectedColumn</code> property by * delegation to the @{link #columnModel} field. * * @return The current value of the selectedColumn property */ public int getSelectedColumn() { return columnModel.getSelectionModel().getMinSelectionIndex(); } private static int countSelections(ListSelectionModel lsm) { int lo = lsm.getMinSelectionIndex(); int hi = lsm.getMaxSelectionIndex(); int sum = 0; if (lo != -1 && hi != -1) { switch (lsm.getSelectionMode()) { case ListSelectionModel.SINGLE_SELECTION: sum = 1; break; case ListSelectionModel.SINGLE_INTERVAL_SELECTION: sum = hi - lo + 1; break; case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: for (int i = lo; i <= hi; ++i) if (lsm.isSelectedIndex(i)) ++sum; break; } } return sum; } private static int[] getSelections(ListSelectionModel lsm) { int sz = countSelections(lsm); int [] ret = new int[sz]; int lo = lsm.getMinSelectionIndex(); int hi = lsm.getMaxSelectionIndex(); int j = 0; java.util.ArrayList ls = new java.util.ArrayList(); if (lo != -1 && hi != -1) { switch (lsm.getSelectionMode()) { case ListSelectionModel.SINGLE_SELECTION: ret[0] = lo; break; case ListSelectionModel.SINGLE_INTERVAL_SELECTION: for (int i = lo; i <= hi; ++i) ret[j++] = i; break; case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: for (int i = lo; i <= hi; ++i) if (lsm.isSelectedIndex(i)) ret[j++] = i; break; } } return ret; } /** * Get the value of the <code>selectedColumnCount</code> property by * delegation to the @{link #columnModel} field. * * @return The current value of the selectedColumnCount property */ public int getSelectedColumnCount() { return countSelections(columnModel.getSelectionModel()); } /** * Get the value of the <code>selectedColumns</code> property by * delegation to the @{link #columnModel} field. * * @return The current value of the selectedColumns property */ public int[] getSelectedColumns() { return getSelections(columnModel.getSelectionModel()); } /** * Get the value of the <code>columnSelectionAllowed</code> property. * * @return The current value of the columnSelectionAllowed property */ public boolean getColumnSelectionAllowed() { return getColumnModel().getColumnSelectionAllowed(); } /** * Get the value of the <code>selectedRowCount</code> property by * delegation to the @{link #selectionModel} field. * * @return The current value of the selectedRowCount property */ public int getSelectedRowCount() { return countSelections(selectionModel); } /** * Get the value of the <code>selectedRows</code> property by * delegation to the @{link #selectionModel} field. * * @return The current value of the selectedRows property */ public int[] getSelectedRows() { return getSelections(selectionModel); } /** * Get the value of the {@link #accessibleContext} property. * * @return The current value of the property */ public AccessibleContext getAccessibleContext() { return accessibleContext; } /** * Get the value of the {@link #cellEditor} property. * * @return The current value of the property */ public TableCellEditor getCellEditor() { return cellEditor; } /** * Get the value of the {@link #dragEnabled} property. * * @return The current value of the property */ public boolean getDragEnabled() { return dragEnabled; } /** * Get the value of the {@link #gridColor} property. * * @return The current value of the property */ public Color getGridColor() { return gridColor; } /** * Get the value of the <code>intercellSpacing</code> property. * * @return The current value of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -