jlist.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 654 行 · 第 1/2 页

JAVA
654
字号
/* class JList
 *
 * Copyright (C) 2003  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
 */

/*
 * Modified Jul 15, 2003 by Tadpole Computer, Inc.
 * Modifications Copyright 2003 by Tadpole Computer, Inc.
 *
 * Modifications are hereby licensed to all parties at no charge under
 * the same terms as the original.
 *
 * Fixed case where visible row count was larger that the total number
 * of rows in the model.
 */

package charvax.swing;

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

import charva.awt.Container;
import charva.awt.Dimension;
import charva.awt.EventQueue;
import charva.awt.Insets;
import charva.awt.Point;
import charva.awt.Scrollable;
import charva.awt.Toolkit;
import charva.awt.event.KeyEvent;
import charva.awt.event.MouseEvent;
import charva.awt.event.ScrollEvent;
import charva.awt.event.ScrollListener;
import charvax.swing.event.ListSelectionListener;

/**
 * A component that allows the user to select one or more objects from a 
 * list.<p>
 *
 * The JList does not provide scrolling capability. The 
 * JList is normally inserted into a JScrollPane to provide scrolling.<p>
 *
 */
public class JList
    extends JComponent 
    implements Scrollable
{
    /** Constructs a JList with 5 rows, 10 columns wide.
     */
    public JList() {
	setModel(new DefaultListModel());
    }

    /**
     * Construct a JList that displays the elements in the specified 
     * non-null model.
     */
    public JList(ListModel model_) {
	setModel(model_);
    }

    /**
     * Construct a JList containing the items in the specified array.
     */
    public JList(Object[] items_)
    {
	setListData(items_);
    }

    /**
     * Construct a JList containing the items in the specified Vector.
     */
    public JList(Vector items_)
    {
	setListData(items_);
    }

    /** Constructs a ListModel from an array of Objects and then applies 
     * setModel to it.
     */
    public void setListData(Object[] listData_) {
	DefaultListModel model = new DefaultListModel();

	for (int i=0; i<listData_.length; i++) {
	    model.addElement(listData_[i]);
	}
	setModel(model);
    }

    /** Constructs a ListModel from a Vector and then applies setModel to it.
     */
    public void setListData(Vector listData_) {
	Enumeration e = listData_.elements();
	DefaultListModel model = new DefaultListModel();

	while (e.hasMoreElements()) {
	    Object item = e.nextElement();
	    model.addElement(item);
	}
	setModel(model);
    }

    /** Sets the model that represents the "contents" of the list, and
     * clears the selection.
     */
    public void setModel(ListModel model_) {
	_listModel = model_;
	_selectionModel.clearSelection();
    }

    /** Returns the data model that holds the list of items displayed by this
     * JList.
     */
    public ListModel getModel() {
	return _listModel;
    }

    /** Set the maximum number of rows that can be displayed at a time
     * by the JScrollPane which contains this JList.
     */
    public void setVisibleRowCount(int rows_) {
	_visibleRows = rows_;
    }

    public int getVisibleRowCount() {
	return _visibleRows;
    }

    /** Set the number of columns INSIDE the list.
     */
    public void setColumns(int cols_) {
	_columns = cols_;
    }

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

    public int getWidth() {
	Insets insets = super.getInsets();
	return _columns + insets.left + insets.right; 
    }

    public int getHeight() { 
	Insets insets = super.getInsets();
	return _listModel.getSize() + insets.top + insets.bottom;
    }

    /** Returns the size of the viewport needed to display visibleRows rows.
     */
    public Dimension getPreferredScrollableViewportSize() {
	return new Dimension(getWidth(), getVisibleRowCount());
    }

    /** Sets the selection model of the JList to an implementation
     * of the ListSelectionModel interface.
     */
    public void setSelectionModel(ListSelectionModel model_) {
	_selectionModel = model_;
    }

    /** Returns the list's implementation of ListSelectionModel.
     */
    public ListSelectionModel getSelectionModel() {
	return _selectionModel;
    }

    /**
     * Register an ListSelectionListener object for this component.
     * The listener is notified each time a change to the selection occurs.
     */
    public void addListSelectionListener(ListSelectionListener il_) {
	_selectionModel.addListSelectionListener(il_);
    }

    /** Remove the specified ListSelectionListener from the list of listeners
     * that will be notified when the selection changes.
     */
    public void removeListSelectionListener(ListSelectionListener listener_) {
	_selectionModel.removeListSelectionListener(listener_);
    }

    /**
     * Get the first selected index, or -1 if there is no selected index.
     */
    public int getSelectedIndex() {
	return _selectionModel.getMinSelectionIndex();
    }

    /** Returns an array of the selected indices. The indices are
     * sorted in increasing index order.
     */
    public int[] getSelectedIndices() {
	ArrayList objects = new ArrayList();
	if ( ! _selectionModel.isSelectionEmpty()) {
	    int first = _selectionModel.getMinSelectionIndex();
	    int last = _selectionModel.getMaxSelectionIndex();
	    for (int i=first; i<=last; i++) {
		if (_selectionModel.isSelectedIndex(i))
		    objects.add(new Integer(i));
	    }
	}

	int[] values = new int[objects.size()];
	for (int i=0; i<values.length; i++) {
	    values[i] = ((Integer) objects.get(i)).intValue();
	}
	return values;
    }

    /**
     * Get the first selected item on this list, or <code>null</code>
     * if the selection is empty.
     */
    public Object getSelectedValue() {

	/* Return null if there are no selected items.
	 */
	int index = _selectionModel.getMinSelectionIndex();
	if (index == -1)
	    return null;

	return _listModel.getElementAt(index);
    }

    /** Returns an array of the selected values. The objects are
     * sorted in increasing index order.
     */
    public Object[] getSelectedValues() {
	ArrayList objects = new ArrayList();
	if ( ! _selectionModel.isSelectionEmpty()) {
	    int first = _selectionModel.getMinSelectionIndex();
	    int last = _selectionModel.getMaxSelectionIndex();
	    for (int i=first; i<=last; i++) {
		if (_selectionModel.isSelectedIndex(i))
		    objects.add(_listModel.getElementAt(i));
	    }
	}
	return objects.toArray();
    }

    /**
     * Make the specified item visible (by scrolling the list up or down).
     * This method does not do anything unless the JList is in a JViewport.
     * Note that the list is not redrawn by this method; the redrawing is
     * done by the JScrollPane (that is registered as a ScrollListener).
     */
    public synchronized void ensureIndexIsVisible(int index_) {
	if ( ! (getParent() instanceof JViewport) )
	    return;

	if (index_ < 0)
	    index_ = 0;
	else if (index_ > _listModel.getSize() - 1) {
	    index_ = _listModel.getSize() - 1;
	}

	// It seems reasonable to assume that the "current row" should
	// be set to the index that is being made visible.
	_currentRow = index_;

	Toolkit term = Toolkit.getDefaultToolkit();
	EventQueue evtqueue = term.getSystemEventQueue();

	// First scroll the list DOWN so that index 0 is visible.
	evtqueue.postEvent(
	        new ScrollEvent(this, ScrollEvent.DOWN, 
		new Point(0, 0)));

	// Then (if necessary) scroll it UP so that the specified index
	// is not below the bottom of the viewport.
	evtqueue.postEvent(
	        new ScrollEvent(this, ScrollEvent.UP, 
		new Point(0, index_)));
    }

    /**
     * Select the item at the specified index. Note that this method
     * does not redraw the JList.
     */
    public void setSelectedIndex(int index_) {
	_selectionModel.setSelectionInterval(index_, index_);
    }

    /** 
     * Sets the selection to be the set union between the current
     * selection and the specified interval between index0_ and index1_
     * (inclusive).
     */
    public void addSelectionInterval(int index0_, int index1_) {
	_selectionModel.addSelectionInterval(index0_, index1_);
    }

    /** 
     * Sets the selection to be the set difference between the current
     * selection and the specified interval between index0_ and index1_
     * (inclusive).
     */
    public void removeSelectionInterval(int index0_, int index1_) {
	_selectionModel.removeSelectionInterval(index0_, index1_);
    }

    /** Clears the selection. After this <code>isSelectionEmpty()</code> 
     * will return true.
     */
    public void clearSelection() {
	_selectionModel.clearSelection();
    }

    /** Returns the lowest selected item index.
     */
    public int getMinSelectionIndex() {
	return _selectionModel.getMinSelectionIndex();

⌨️ 快捷键说明

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