⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtable.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    public void removeScrollListener(ScrollListener sl_) {
	if (_scrollListeners == null)
	    return;
	_scrollListeners.remove(sl_);
    }

    /** Process scroll events generated by this JTable.
     */
    public void processScrollEvent(ScrollEvent e_) {
	if (_scrollListeners != null) {
	    for (Enumeration e = _scrollListeners.elements(); 
		    e.hasMoreElements(); ) {

		ScrollListener sl = (ScrollListener) e.nextElement();
		sl.scroll(e_);
	    }
	}
    }

    public Dimension getSize() {
	return new Dimension(this.getWidth(), this.getHeight());
    }

    public Dimension minimumSize() {
	return this.getSize();
    }

    public int getWidth() {

	int columns = _model.getColumnCount();
	int width = 1;
	for (int i=0; i<columns; i++) {
	    width += getColumnWidth(i) + 1;
	}

	return width;
    }

    public int getHeight() {
	int rows = _model.getRowCount();
	return rows + 2;
    }

    public void setPreferredScrollableViewportSize(Dimension size_) {
	_viewportSize = size_;
	_viewportSizeSet = true;
    }

    public Dimension getPreferredScrollableViewportSize() {
	if (_viewportSizeSet)
	    return new Dimension(_viewportSize);
	else
	    return minimumSize();
    }

    /** Sets the table's row selection model and registers for notifications
     * from the new selection model.
     */
    public void setSelectionModel(ListSelectionModel model_) {
	_rowSelectionModel = model_;
	_rowSelectionModel.addListSelectionListener(this);
    }

    /** Returns the table's row selection model.
     */
    public ListSelectionModel getSelectionModel() {
	return _rowSelectionModel;
    }

    /** Sets the table's selection mode to allow selection of either single
     * rows and/or columns, or multiple rows and/or columns.
     * @param mode_ the selection mode. Allowable values are
     * ListSelectionModel.SINGLE_SELECTION and
     * ListSelectionModel.MULTIPLE_INTERVAL_SELECTION.
     */
    public void setSelectionMode(int mode_) {
	_rowSelectionModel.setSelectionMode(mode_);
	_columnSelectionModel.setSelectionMode(mode_);
    }

    /** Returns the table's row/column selection mode.
     */
    public int getSelectionMode() {
	return _rowSelectionModel.getSelectionMode();
    }

    /** Set whether selection of columns is allowed.
     */
    public void setColumnSelectionAllowed(boolean allowed_) {
	_columnSelectionAllowed = allowed_;
    }

    /** Returns true if columns can be selected; otherwise false.
     */
    public boolean getColumnSelectionAllowed() {
	return _columnSelectionAllowed;
    }

    /** Set whether selection of rows is allowed.
     */
    public void setRowSelectionAllowed(boolean allowed_) {
	_rowSelectionAllowed = allowed_;
    }

    /** Returns true if rows can be selected; otherwise false.
     */
    public boolean getRowSelectionAllowed() {
	return _rowSelectionAllowed;
    }

    /** Adds the columns from <code>index0_</code> to <code>index1_</code>,
     * inclusive, to the current selection.
     */
    public void addColumnSelectionInterval(int index0_, int index1_) {
	_columnSelectionModel.addSelectionInterval(index0_, index1_);
    }

    /** Adds the rows from <code>index0_</code> to <code>index1_</code>,
     * inclusive, to the current selection.
     */
    public void addRowSelectionInterval(int index0_, int index1_) {
	_rowSelectionModel.addSelectionInterval(index0_, index1_);
    }

    /** Selects the columns from <code>index0_</code> to <code>index1_</code>,
     * inclusive.
     */
    public void setColumnSelectionInterval(int index0_, int index1_) {
	_columnSelectionModel.setSelectionInterval(index0_, index1_);
    }

    /** Selects the rows from <code>index0_</code> to <code>index1_</code>,
     * inclusive.
     */
    public void setRowSelectionInterval(int index0_, int index1_) {
	_rowSelectionModel.setSelectionInterval(index0_, index1_);
    }

    /** Returns the index of the first selected row, or -1 if
     * no row is selected.
     */
    public int getSelectedRow() {
	return _rowSelectionModel.getMinSelectionIndex();
    }

    /** Returns the number of selected rows.
     */
    public int getSelectedRowCount() {
	int min = _rowSelectionModel.getMinSelectionIndex();
	if (min == -1)
	    return 0;

	int max = _rowSelectionModel.getMaxSelectionIndex();
	int j = 0;
	for (int i=min; i<=max; i++) {
	    if (_rowSelectionModel.isSelectedIndex(i))
		j++;
	}
	return j;
    }

    /** Returns an array of the indices of all selected rows.
     */
    public int[] getSelectedRows() {
	int rowCount = getSelectedRowCount();
	if (rowCount == 0)
	    return new int[0];

	int[] array = new int[rowCount];
	int min = _rowSelectionModel.getMinSelectionIndex();
	int max = _rowSelectionModel.getMaxSelectionIndex();
	int j = 0;
	for (int i=min; i<=max; i++) {
	    if (_rowSelectionModel.isSelectedIndex(i))
		array[j++] = i;
	}
	return array;
    }

    /** Returns the index of the first selected column, or -1 if
     * no column is selected.
     */
    public int getSelectedColumn() {
	return _columnSelectionModel.getMinSelectionIndex();
    }

    /** Returns the number of selected columns.
     */
    public int getSelectedColumnCount() {
	int min = _columnSelectionModel.getMinSelectionIndex();
	if (min == -1)
	    return 0;

	int max = _columnSelectionModel.getMaxSelectionIndex();
	int j = 0;
	for (int i=min; i<=max; i++) {
	    if (_columnSelectionModel.isSelectedIndex(i))
		j++;
	}
	return j;
    }

    /** Returns an array of the indices of all selected columns.
     */
    public int[] getSelectedColumns() {
	int columnCount = getSelectedColumnCount();
	if (columnCount == 0)
	    return new int[0];

	int[] array = new int[columnCount];
	int min = _columnSelectionModel.getMinSelectionIndex();
	int max = _columnSelectionModel.getMaxSelectionIndex();
	int j = 0;
	for (int i=min; i<=max; i++) {
	    if (_columnSelectionModel.isSelectedIndex(i))
		array[j++] = i;
	}
	return array;
    }

    /** Returns true if the row with the specified index is selected.
     */
    public boolean isRowSelected(int row_) {
	return _rowSelectionModel.isSelectedIndex(row_);
    }

    /** Returns true if the column with the specified index is selected.
     */
    public boolean isColumnSelected(int column_) {
	return _columnSelectionModel.isSelectedIndex(column_);
    }

    /** This method is invoked when the row selection changes. 
     */
    public void valueChanged(ListSelectionEvent e_) {
	repaint();
    }

    public void debug(int level_) {
	for (int i=0; i<level_; i++)
	    System.err.print("    ");
	System.err.println("JTable origin=" + _origin + 
	    " size=" + getSize());
    }

    private int getColumnWidth(int column_) {
	/* Calculate the column width for the specified column.
	 */
	int columnwidth = _model.getColumnName(column_).length() + 2;

	for (int j=0; j<_model.getRowCount(); j++) {
	    Object value = getValueAt(j, column_);
	    if (value != null) {
		int width = value.toString().length();
		if (width > columnwidth)
		    columnwidth = width;
	    }
	}
	return columnwidth;
    }

    private void selectCurrentColumn() {
	if (_columnSelectionModel.isSelectedIndex(_currentColumn)) {
	    _columnSelectionModel.removeSelectionInterval(
		    _currentColumn, _currentColumn);
	}
	else {
	    int selectionMode = _rowSelectionModel.getSelectionMode();

	    // the column is not currently selected; select it.
	    if (selectionMode == ListSelectionModel.SINGLE_SELECTION) {
		_columnSelectionModel.setSelectionInterval(
			_currentColumn, _currentColumn);
	    }
	    else {
		_columnSelectionModel.addSelectionInterval(
			_currentColumn, _currentColumn);
	    }
	}
    }

    private void selectCurrentRow() {
	if (_rowSelectionModel.isSelectedIndex(_currentRow)) {
	    _rowSelectionModel.removeSelectionInterval(
		    _currentRow, _currentRow);
	}
	else {
	    int selectionMode = _rowSelectionModel.getSelectionMode();

	    // the row is not currently selected; select it.
	    if (selectionMode == ListSelectionModel.SINGLE_SELECTION) {
		_rowSelectionModel.setSelectionInterval(
			_currentRow, _currentRow);
	    }
	    else {
		_rowSelectionModel.addSelectionInterval(
			_currentRow, _currentRow);
	    }
	}
    }

    //--------------------------------------------------------------------
    // INSTANCE VARIABLES

    private TableModel _model = null;

    private Dimension _viewportSize;
    private boolean _viewportSizeSet = false;

    /** This instance variable determines the row that will
     * be highlighted when the table has input focus.
     */
    private int _currentRow = 0;

    private int _currentColumn = 0;

    private boolean _columnSelectionAllowed = true;

    private boolean _rowSelectionAllowed = true;

    /** A list of ScrollListeners registered for this JTable.
     */
    private Vector _scrollListeners = null;

    protected ListSelectionModel _rowSelectionModel = 
	    new DefaultListSelectionModel();

    protected ListSelectionModel _columnSelectionModel = 
	    new DefaultListSelectionModel();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -