📄 jtable.java
字号:
protected void createDefaultEditors() { //FIXME: Create the editor object. } protected void createDefaultRenderers() { //FIXME: Create the renderer object. } /** * @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code> */ public static JScrollPane createScrollPaneForTable(JTable table) { return new JScrollPane(table); } protected TableColumnModel createDefaultColumnModel() { return new DefaultTableColumnModel(); } protected TableModel createDefaultDataModel() { return new DefaultTableModel(); } protected ListSelectionModel createDefaultSelectionModel() { return new DefaultListSelectionModel(); } protected JTableHeader createDefaultTableHeader() { return new JTableHeader(columnModel); } private void createColumnsFromModel() { if (dataModel == null) return; TableColumnModel cm = createDefaultColumnModel(); for (int i = 0; i < dataModel.getColumnCount(); ++i) { cm.addColumn(new TableColumn(i)); } this.setColumnModel(cm); } // listener support public void columnAdded (TableColumnModelEvent event) { revalidate(); repaint(); } public void columnMarginChanged (ChangeEvent event) { revalidate(); repaint(); } public void columnMoved (TableColumnModelEvent event) { revalidate(); repaint(); } public void columnRemoved (TableColumnModelEvent event) { revalidate(); repaint(); } public void columnSelectionChanged (ListSelectionEvent event) { repaint(); } public void editingCanceled (ChangeEvent event) { repaint(); } public void editingStopped (ChangeEvent event) { repaint(); } public void tableChanged (TableModelEvent event) { repaint(); } public void valueChanged (ListSelectionEvent event) { repaint(); } /** * Returns index of the column that contains specified point * or -1 if this table doesn't contain this point. * * @param point point to identify the column * @return index of the column that contains specified point or * -1 if this table doesn't contain this point. */ public int columnAtPoint(Point point) { int x0 = getLocation().x; int ncols = getColumnCount(); Dimension gap = getIntercellSpacing(); TableColumnModel cols = getColumnModel(); int x = point.x; for (int i = 0; i < ncols; ++i) { int width = cols.getColumn(i).getWidth() + (gap == null ? 0 : gap.width); if (0 <= x && x < width) return i; x -= width; } return -1; } /** * Returns index of the row that contains specified point or * -1 if this table doesn't contain this point. * * @param point point to identify the row * @return index of the row that contains specified point or * -1 if this table doesn't contain this point. */ public int rowAtPoint(Point point) { int y0 = getLocation().y; int nrows = getRowCount(); Dimension gap = getIntercellSpacing(); int height = getRowHeight() + (gap == null ? 0 : gap.height); int y = point.y; for (int i = 0; i < nrows; ++i) { if (0 <= y && y < height) return i; y -= height; } return -1; } /** * Calculate the visible rectangle for a particular row and column. The * row and column are specified in visual terms; the column may not match * the {@link #dataModel} column. * * @param row the visible row to get the cell rectangle of * * @param column the visible column to get the cell rectangle of, which may * differ from the {@link #dataModel} column * * @param includeSpacing whether or not to include the cell margins in the * resulting cell. If <code>false</code>, the result will only contain the * inner area of the target cell, not including its margins. * * @return a rectangle enclosing the specified cell */ public Rectangle getCellRect(int row, int column, boolean includeSpacing) { int height = getHeight(); int width = columnModel.getColumn(column).getWidth(); int x_gap = columnModel.getColumnMargin(); int y_gap = rowMargin; column = Math.max(0, Math.min(column, getColumnCount() - 1)); row = Math.max(0, Math.min(row, getRowCount() - 1)); int x = 0; int y = (height + y_gap) * row; for (int i = 0; i < column; ++i) { x += columnModel.getColumn(i).getWidth(); x += x_gap; } 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(); } /** * Get the value of the {@link #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() { if (! rowSelectionAllowed) return null; 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 {@link #scrollableTracksViewportHeight} property. * * @return The constant value <code>false</code> */ public boolean getScrollableTracksViewportHeight() { return false; } /** * Get the value of the {@link #scrollableTracksViewportWidth} property. * * @return <code>true</code> unless the {@link autoResizeMode} prperty 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 rowHeight; else { int sum = 0; for (int i = 0; i < getColumnCount(); ++i) sum += columnModel.getColumn(0).getWidth(); return getColumnCount() == 0 ? 10 : sum / getColumnCount(); } } 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 if (vc > getColumnCount()) return -1; 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 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 {@link #columnCount} 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 {@link #rowCount} property by * delegation to the @{link #dataModel} field. * * @return The current value of the rowCount property */ public int getRowCount() { return dataModel.getRowCount(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -