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

📄 jtable.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* class JTable
 *
 * Copyright (C) 2001  R M Pitman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package charvax.swing;

import java.util.Enumeration;
import java.util.Vector;

import charva.awt.Dimension;
import charva.awt.EventQueue;
import charva.awt.Point;
import charva.awt.Scrollable;
import charva.awt.Toolkit;
import charva.awt.event.KeyEvent;
import charva.awt.event.ScrollEvent;
import charva.awt.event.ScrollListener;
import charvax.swing.event.ListSelectionEvent;
import charvax.swing.event.ListSelectionListener;
import charvax.swing.event.TableModelEvent;
import charvax.swing.event.TableModelListener;
import charvax.swing.table.DefaultTableModel;
import charvax.swing.table.TableModel;

/**
 * JTable is a user-interface component that displays data in a two-
 * dimensional table format.<p>
 * The user-interface works as follows:<p>
 * The user can select a column by pressing the left or right arrow keys
 * to move to the desired column, and then pressing ENTER.<br>
 * He/she can select a row by pressing the up and down arrow keys to move to
 * the desired row, then pressing ENTER.<br>
 * Depending on the value of the selection mode, multiple rows and/or columns
 * may be selected. By default the selection mode is set to SINGLE_SELECTION
 * so that only a single row or column can be selected at a time. Selection
 * of rows and/or columns can be enabled/disabled by means of the
 * setRowSelectionAllowed() and setColumnSelectionAllowed() methods.
 */
public class JTable
    extends JComponent
    implements TableModelListener, Scrollable, ListSelectionListener
{
    /** Default constructor
     */
    public JTable() {
	this(new DefaultTableModel(0, 0));
    }

    /** Constructs a table of numRows_ and numColumns_ of empty cells
     * using a DefaultTableModel.
     */
    public JTable(int numRows_, int numColumns_) {
	this(new DefaultTableModel(numRows_, numColumns_));
    }

    /**
     * Construct a JTable from the specified data and column names, using
     * a DefaultTableModel.
     */
    public JTable(Object[][] data_, Object[] columnNames_) {
	this(new DefaultTableModel(data_, columnNames_));
    }

    /** Construct a JTable with the specified data model.
     */
    public JTable(TableModel model_) {
	setModel(model_);
	_rowSelectionModel.addListSelectionListener(this);
    }

    /**
     * Sets the data model to the specified TableModel and registers with it
     * as a listener for events from the model.
     */
    public void setModel(TableModel model_) {
	_model = model_;
	_model.addTableModelListener(this);
    }

    public TableModel getModel() { return _model; }

    public void setValueAt(Object object_, int row_, int column_) {
	_model.setValueAt(object_, row_, column_);
    }

    public Object getValueAt(int row_, int column_) {
	return _model.getValueAt(row_, column_);
    }

    /** This method implements the TableModelListener interface;
     * it is invoked when this table's TableModel generates a 
     * TableModelEvent.
     */
    public void tableChanged(TableModelEvent evt_) {
	/* For now, we'll just post a PaintEvent onto the queue.
	 */
	repaint();
    }

    public void requestFocus() {
	/* Generate the FOCUS_GAINED event.
	 */
	super.requestFocus();

	/* Get the absolute origin of this component 
	 */
	Point origin = getLocationOnScreen();

	/* Calculate the x position of the cursor
	 */
	int x=1;
	for (int i=0; i<_currentColumn; i++) {
	    x += getColumnWidth(i) + 1;
	}

	/* Ensure that the new cursor position is not off the screen (which
	 * it can be if the JTable is in a JViewport).
	 */
	Point newCursor = origin.addOffset(x, _currentRow+1);
	if (newCursor.x < 0)
	    newCursor.x = 0;
	if (newCursor.y < 0)
	    newCursor.y = 0;
	Toolkit.getDefaultToolkit().setCursor(newCursor);
    }

    public void draw() {
	/* Get the absolute origin of this component.
	 */
	Point origin = getLocationOnScreen();

	Toolkit term = Toolkit.getDefaultToolkit();
	int rows = _model.getRowCount();
	int columns = _model.getColumnCount();
	int colorpair = getCursesColor();

	/* Start by blanking out the table area and drawing the box 
	 * around the table.
	 */
	term.blankBox(origin, getSize(), colorpair);
	term.drawBox(origin, getSize(), colorpair);

	/* Now fill in the table headings
	 */
	int x = 1;
	int attr = Toolkit.A_BOLD;
	for (int i=0; i<columns; i++) {
	    term.setCursor(origin.addOffset(x, 0));
	    term.addChar(' ', attr, colorpair);
	    term.addString(_model.getColumnName(i), attr, colorpair);
	    term.addChar(' ', attr, colorpair);
	    x += getColumnWidth(i) + 1;
	}

	/* Now draw the vertical lines that divide the columns.
	 */
	if (_model.getColumnCount() != 0) {
	    x = getColumnWidth(0) + 1;
	    for (int i=0; i<columns-1; i++) {
		term.setCursor(origin.addOffset(x, 0));
		term.addChar(Toolkit.ACS_TTEE, 0, colorpair);	    // top tee
		term.setCursor(origin.addOffset(x, 1));
		term.addVerticalLine(rows, 0, colorpair);
		term.setCursor(origin.addOffset(x, rows+1));
		term.addChar(Toolkit.ACS_BTEE, 0, colorpair);	    // bottom tee
		x += getColumnWidth(i+1) + 1;
	    }
	}

	/* Now draw the contents of the cells.
	 */
	x = 1;
	for (int column = 0; column<columns; column++) {
	    for (int row=0; row<rows; row++) {
		term.setCursor(origin.addOffset(x, row+1));
		Object value = _model.getValueAt(row, column);

		/* Show the currently SELECTED rows and columns in reverse video
		 */
		int attrib = 
		    (isRowSelected(row) || isColumnSelected(column))  ?
		    Toolkit.A_REVERSE : Toolkit.A_NORMAL;

		// Highlight the current row and column
		if (_currentRow == row || _currentColumn == column)
		    attrib += Toolkit.A_BOLD;

		if (value == null)
		    term.addString("", attrib, colorpair);
		else
		    term.addString(value.toString(), attrib, colorpair);
	    }
	    x += getColumnWidth(column) + 1;
	}
    }

    /**
     * Processes key events occurring on this object 
     */
    public void processKeyEvent(KeyEvent ke_) {
	/* First call all KeyListener objects that may have been registered
	 * for this component. 
	 */
	super.processKeyEvent(ke_);

	/* Check if any of the KeyListeners consumed the KeyEvent.
	 */
	if (ke_.isConsumed())
	    return;

	Toolkit term = Toolkit.getDefaultToolkit();
	EventQueue evtqueue = term.getSystemEventQueue();
	int key = ke_.getKeyCode();
	if (key == '\t') {
	    getParent().nextFocus();
	    return;
	}
	else if (key == KeyEvent.VK_BACK_TAB) {
	    getParent().previousFocus();
	    return;
	}
	else if (key == KeyEvent.VK_UP) {
	    if (_currentRow == 0)
		term.beep();
	    else {
		_currentRow--;
		int x=0;
		for (int i=0; i<_currentColumn; i++)
		    x += getColumnWidth(i) + 1;
		evtqueue.postEvent(
		    new ScrollEvent(this, ScrollEvent.DOWN, 
			new Point(x, _currentRow+1)));
	    }
	}
	else if (key == KeyEvent.VK_DOWN) {
	    if (_currentRow == _model.getRowCount() - 1)
		term.beep();
	    else {
		_currentRow++;
		int x=0;
		for (int i=0; i<_currentColumn; i++)
		    x += getColumnWidth(i) + 1;
		evtqueue.postEvent(
		    new ScrollEvent(this, ScrollEvent.UP, 
			new Point(x, _currentRow+2)));
	    }
	}
	else if (key == KeyEvent.VK_LEFT) {
	    if (_currentColumn == 0)
		term.beep();
	    else {
		_currentColumn--;
		int x=0;
		for (int i=0; i<_currentColumn; i++)
		    x += getColumnWidth(i) + 1;
		evtqueue.postEvent(
		    new ScrollEvent(this, ScrollEvent.RIGHT, 
			new Point(x, _currentRow)));
	    }
	}
	else if (key == KeyEvent.VK_RIGHT) {
	    if (_currentColumn == _model.getColumnCount() - 1)
		term.beep();
	    else {
		_currentColumn++;
		int x=0;
		for (int i=0; i<=_currentColumn; i++)
		    x += getColumnWidth(i) + 1;
		evtqueue.postEvent( 
		    new ScrollEvent(this, ScrollEvent.LEFT, 
			new Point(x, _currentRow)));
	    }
	}
	else if (key == KeyEvent.VK_HOME) {
	    int x=0;
	    for (int i=0; i<_currentColumn; i++)
		x += getColumnWidth(i) + 1;
	    evtqueue.postEvent( 
		    new ScrollEvent(this, ScrollEvent.RIGHT, 
			new Point(x, _currentRow)));
	}
	else if (key == KeyEvent.VK_END) {
	    int x=0;
	    for (int i=0; i<=_currentColumn; i++)
		x += getColumnWidth(i) + 1;
	    evtqueue.postEvent( 
		    new ScrollEvent(this, ScrollEvent.LEFT, 
			new Point(x, _currentRow)));
	}
	else if (key == KeyEvent.VK_ENTER) {
	    if (getColumnSelectionAllowed())
		selectCurrentColumn();

	    if (getRowSelectionAllowed())
		selectCurrentRow();

	    repaint();
	}

	if ((getParent() instanceof JViewport) == false) {
	    draw();
	    requestFocus();
	    super.requestSync();
	}
    }

    /**
     * Register a ScrollListener object for this table.
     */
    public void addScrollListener(ScrollListener sl_) {
	if (_scrollListeners == null)
	    _scrollListeners = new Vector();
	_scrollListeners.add(sl_);
    }

    /**
     * Remove a ScrollListener object that is registered for this table.

⌨️ 快捷键说明

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