jlist.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 654 行 · 第 1/2 页
JAVA
654 行
}
/** Returns the highest selected item index.
*/
public int getMaxSelectionIndex() {
return _selectionModel.getMaxSelectionIndex();
}
/**
* Sets the flag that determines whether this list allows multiple
* selections.
* @param mode_ the selection mode. Allowed values are:<p>
* <ul>
* <li> ListSelectionModel.SINGLE_SELECTION. Only one list index
* can be selected at a time.
* <li> ListSelectionModel.SINGLE_INTERVAL_SELECTION.
* <li> ListSelectionModel.MULTIPLE_INTERVAL_SELECTION. Any number
* of list items can be selected simultaneously.
* </ul>
*/
public void setSelectionMode(int mode_) {
_selectionModel.setSelectionMode(mode_);
}
/**
* Determines whether this list allows multiple selections.
*/
public int getSelectionMode() {
return _selectionModel.getSelectionMode();
}
/** Determines if the specified item in this scrolling list is selected.
*/
public boolean isIndexSelected(int index_) {
return _selectionModel.isSelectedIndex(index_);
}
/**
* Called by LayoutManager.
*/
public Dimension minimumSize() {
/* Calculate the minimum number of columns that will contain all the
* items in the list.
*/
_columns = 1;
for (int i=0; i< _listModel.getSize(); i++) {
String c = _listModel.getElementAt(i).toString();
if (c.length() > _columns)
_columns = c.length();
}
/* Take into account the border inherited from the JComponent
* superclass.
*/
Insets insets = super.getInsets();
return new Dimension(_columns + insets.left + insets.right,
_visibleRows + insets.top + insets.bottom);
}
public void requestFocus() {
/* Generate the FOCUS_GAINED event.
*/
super.requestFocus();
/* Get the absolute origin of this component
*/
Point origin = getLocationOnScreen();
Insets insets = super.getInsets();
origin.translate(insets.left, insets.top);
Toolkit.getDefaultToolkit().setCursor(
origin.addOffset(0, _currentRow));
}
/** Draws this component. Overrides draw() in Component.
*/
public void draw() {
/* This situation can occur if the ListModel has been changed.
*/
if (_currentRow >= _listModel.getSize()) {
if (_listModel.getSize() == 0)
_currentRow = 0;
else
_currentRow = _listModel.getSize() - 1;
}
/* Draw the border if it exists
*/
super.draw();
/* Get the absolute origin of this component.
*/
Point origin = getLocationOnScreen();
Insets insets = super.getInsets();
origin.translate(insets.left, insets.top);
Toolkit term = Toolkit.getDefaultToolkit();
int colorpair = getCursesColor();
int attribute;
StringBuffer blanks = new StringBuffer();
for (int j=0; j<_columns; j++)
blanks.append(' ');
for (int i = 0; i < _listModel.getSize(); i++) {
term.setCursor(origin.x, origin.y + i);
if (isIndexSelected(i))
attribute = Toolkit.A_REVERSE;
else
attribute = 0;
if (i == _currentRow)
attribute += Toolkit.A_BOLD;
String item = _listModel.getElementAt(i).toString();
StringBuffer buffer = new StringBuffer(item);
for (int k=item.length(); k<_columns; k++)
buffer.append(' ');
term.addString(buffer.toString(), attribute, colorpair);
} // end FOR loop
for (int i = _listModel.getSize(); i < _visibleRows; i++) {
term.setCursor(origin.x, origin.y + i);
term.addString(blanks.toString(), 0, colorpair);
}
}
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();
switch (key) {
case '\t':
getParent().nextFocus();
return;
case KeyEvent.VK_BACK_TAB:
getParent().previousFocus();
return;
case KeyEvent.VK_DOWN:
/* If we are already at the bottom of the list, ignore
* this keystroke.
*/
if (_currentRow >= _listModel.getSize() -1)
return;
evtqueue.postEvent(
new ScrollEvent(this, ScrollEvent.UP,
new Point(0, ++_currentRow)));
break;
case KeyEvent.VK_PAGE_DOWN:
_currentRow += _visibleRows;
if (_currentRow >= _listModel.getSize())
_currentRow = _listModel.getSize() -1;
evtqueue.postEvent(
new ScrollEvent(this, ScrollEvent.UP,
new Point(0, _currentRow)));
break;
case KeyEvent.VK_END:
_currentRow = _listModel.getSize() - 1;
evtqueue.postEvent(new ScrollEvent(
this, ScrollEvent.UP, new Point(0, _currentRow)));
break;
case KeyEvent.VK_UP:
/* If we are already at the top of the list, ignore
* this keystroke.
*/
if (_currentRow < 1)
return;
evtqueue.postEvent(new ScrollEvent(
this, ScrollEvent.DOWN, new Point(0, --_currentRow)));
break;
case KeyEvent.VK_PAGE_UP:
_currentRow -= _visibleRows;
if (_currentRow < 0)
_currentRow = 0;
evtqueue.postEvent(new ScrollEvent(this, ScrollEvent.DOWN,
new Point(0, _currentRow)));
break;
case KeyEvent.VK_ENTER:
_doSelect();
break;
case KeyEvent.VK_HOME:
_currentRow = 0;
break;
}
if ((getParent() instanceof JViewport) == false) {
draw();
requestFocus();
super.requestSync();
}
}
private void _doSelect() {
/* Pressing ENTER or double-clicking on a row selects/deselects
* the current row. If the list is empty, ignore the keystroke.
*/
if (_listModel.getSize() == 0)
return;
if (isIndexSelected(_currentRow))
removeSelectionInterval(_currentRow, _currentRow);
else {
int mode = getSelectionMode();
if (mode == ListSelectionModel.SINGLE_SELECTION)
setSelectedIndex(_currentRow);
else
addSelectionInterval(_currentRow, _currentRow);
}
repaint();
}
public void processMouseEvent(MouseEvent e_) {
super.processMouseEvent(e_);
if (e_.getButton() == MouseEvent.BUTTON1 &&
e_.getModifiers() == MouseEvent.MOUSE_CLICKED &&
this.isFocusTraversable()) {
if (e_.getClickCount() == 1) {
int y = e_.getY();
Point origin = getLocationOnScreen();
Container parent = getParent();
if (parent instanceof JViewport) {
_currentRow = y - origin.y;
repaint();
}
}
else
_doSelect();
}
}
/**
* Register a ScrollListener object for this JList.
*/
public void addScrollListener(ScrollListener sl_) {
if (_scrollListeners == null)
_scrollListeners = new Vector();
_scrollListeners.add(sl_);
}
/**
* Remove a ScrollListener object that is registered for this JList.
*/
public void removeScrollListener(ScrollListener sl_) {
if (_scrollListeners == null)
return;
_scrollListeners.remove(sl_);
}
/** Process scroll events generated by this JList.
*/
public void processScrollEvent(ScrollEvent e_) {
if (_scrollListeners != null) {
for (Enumeration e = _scrollListeners.elements();
e.hasMoreElements(); ) {
ScrollListener sl = (ScrollListener) e.nextElement();
sl.scroll(e_);
}
}
}
/** Outputs a textual description of this component to stderr.
*/
public void debug(int level_) {
for (int i=0; i<level_; i++)
System.err.print(" ");
System.err.println("JList origin=" + _origin +
" size=" + minimumSize());
}
//================================================================
// INSTANCE VARIABLES
private int _visibleRows = 5;
private int _columns = 10;
/** Offset (from start of list) of the item under the cursor (i.e. the
* item that will be selected/deselected if the user presses ENTER)
*/
protected int _currentRow = 0;
/** The ListSelectionModel used by this JList.
*/
protected ListSelectionModel _selectionModel =
new DefaultListSelectionModel();
/** The ListModel that holds the items that are displayed by
* this JList.
*/
protected ListModel _listModel;
/** A list of ScrollListeners registered for this JList.
*/
private Vector _scrollListeners = null;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?