?? jtable.java
字號:
/** * Create the default table column model that is used if the user-defined * column model is not provided. The default method creates * {@link DefaultTableColumnModel}. * * @return the created table column model. */ protected TableColumnModel createDefaultColumnModel() { return new DefaultTableColumnModel(); } /** * Create the default table data model that is used if the user-defined * data model is not provided. The default method creates * {@link DefaultTableModel}. * * @return the created table data model. */ protected TableModel createDefaultDataModel() { return new DefaultTableModel(); } /** * Create the default table selection model that is used if the user-defined * selection model is not provided. The default method creates * {@link DefaultListSelectionModel}. * * @return the created table data model. */ protected ListSelectionModel createDefaultSelectionModel() { return new DefaultListSelectionModel(); } /** * Create the default table header, if the user - defined table header is not * provided. * * @return the default table header. */ protected JTableHeader createDefaultTableHeader() { return new JTableHeader(columnModel); } /** * Invoked when the column is added. Revalidates and repains the table. */ public void columnAdded (TableColumnModelEvent event) { revalidate(); repaint(); } /** * Invoked when the column margin is changed. * Revalidates and repains the table. */ public void columnMarginChanged (ChangeEvent event) { revalidate(); repaint(); } /** * Invoked when the column is moved. Revalidates and repains the table. */ public void columnMoved (TableColumnModelEvent event) { revalidate(); repaint(); } /** * Invoked when the column is removed. Revalidates and repains the table. */ public void columnRemoved (TableColumnModelEvent event) { revalidate(); repaint(); } /** * Invoked when the the column selection changes. */ public void columnSelectionChanged (ListSelectionEvent event) { repaint(); } /** * Invoked when the editing is cancelled. */ public void editingCanceled (ChangeEvent event) { if (editorComp!=null) { remove(editorComp); repaint(editorComp.getBounds()); editorComp = null; } } /** * Finish the current editing session and update the table with the * new value by calling {@link #setValueAt}. * * @param event the change event */ public void editingStopped (ChangeEvent event) { if (editorComp!=null) { remove(editorComp); setValueAt(cellEditor.getCellEditorValue(), editingRow, editingColumn); repaint(editorComp.getBounds()); editorComp = null; } requestFocusInWindow(); } /** * Invoked when the table changes. * <code>null</code> means everything changed. */ public void tableChanged (TableModelEvent event) { // update the column model from the table model if the structure has // changed and the flag autoCreateColumnsFromModel is set if ((event == null || (event.getFirstRow() == TableModelEvent.HEADER_ROW)) && autoCreateColumnsFromModel) createDefaultColumnsFromModel(); // If the structure changes, we need to revalidate, since that might // affect the size parameters of the JTable. Otherwise we only need // to perform a repaint to update the view. if (event == null || event.getType() == TableModelEvent.INSERT) revalidate(); if (event == null || event.getType() == TableModelEvent.DELETE) { if (dataModel.getRowCount() == 0) clearSelection(); revalidate(); } repaint(); } /** * Invoked when another table row is selected. */ 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 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) { if (point != null) { int nrows = getRowCount(); int height = getRowHeight() + getRowMargin(); int y = point.y; int r = y / height; if (r < 0 || r >= nrows) return -1; else return r; } else 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) { // moveToCellBeingEdited expects the cached value and clones it. // If the caching would be removed later, uplate moveToCellBeingEdited // as well. int height = getRowHeight(row); 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(); if (includeSpacing) rectCache.setBounds(x, y, width, height +y_gap); else rectCache.setBounds(x, y, width - x_gap, height); return rectCache; } 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; } /** * Return the preferred scrolling amount (in pixels) for the given scrolling * direction and orientation. This method handles a partially exposed row by * returning the distance required to completely expose the item. When * scrolling the top item is completely exposed. * * @param visibleRect the currently visible part of the component. * @param orientation the scrolling orientation * @param direction the scrolling direction (negative - up, positive -down). * The values greater than one means that more mouse wheel or similar * events were generated, and hence it is better to scroll the longer * distance. * @author Audrius Meskauskas (audriusa@bioinformatics.org) */ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { int h = (rowHeight + rowMargin); int delta = h * direction; // Round so that the top would start from the row boundary if (orientation == SwingConstants.VERTICAL) { // Completely expose the top row int near = ((visibleRect.y + delta + h / 2) / h) * h; int diff = visibleRect.y + delta - near; delta -= diff; } return delta; // TODO when scrollng horizontally, scroll into the column boundary. } /** * Get the cell editor, suitable for editing the given cell. The default * method requests the editor from the column model. If the column model does * not provide the editor, the call is forwarded to the * {@link #getDefaultEditor(Class)} with the parameter, obtained from * {@link TableModel#getColumnClass(int)}. * * @param row the cell row * @param column the cell column * @return the editor to edit that cell */ public TableCellEditor getCellEditor(int row, int column) { TableCellEditor editor = columnModel.getColumn(column).getCellEditor(); if (editor == null) { int mcolumn = convertColumnIndexToModel(column); editor = getDefaultEditor(dataModel.getColumnClass(mcolumn)); } return editor; } /** * Get the default editor for editing values of the given type * (String, Boolean and so on). * * @param columnClass the class of the value that will be edited. * * @return the editor, suitable for editing this data type */ public TableCellEditor getDefaultEditor(Class columnClass) { if (defaultEditorsByColumnClass.containsKey(columnClass)) return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); else { JTextField t = new TableTextField(); TableCellEditor r = new DefaultCellEditor(t); defaultEditorsByColumnClass.put(columnClass, r); return r; } } /** * Get the cell renderer for rendering the given cell. * * @param row the cell row * @param column the cell column * @return the cell renderer to render that cell. */ public TableCellRenderer getCellRenderer(int row, int column) { TableCellRenderer renderer = columnModel.getColumn(column).getCellRenderer(); if (renderer == null) { int mcolumn = convertColumnIndexToModel(column); renderer = getDefaultRenderer(dataModel.getColumnClass(mcolumn)); } return renderer; } /** * Set default renderer for rendering the given data type. * * @param columnClass the data type (String, Boolean and so on) that must be * rendered. * @param rend the renderer that will rend this data type */ public void setDefaultRenderer(Class columnClass, TableCellRenderer rend) { defaultRenderersByColumnClass.put(columnClass, rend); } /** * Get the default renderer for rendering the given data type. * * @param columnClass the data that must be rendered * * @return the appropriate defauld renderer for rendering that data type. */ 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; } } /** * Convert the table m
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -