📄 filtersorttable.java
字号:
/*
* FilterSortTable.java
*
* @author alan.dodson
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package au.id.dodson.FilterableSortableTable;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
/**
* FilterSortTable
*/
public class FilterSortTable extends JTable {
public TableExclusionFilterModel filterModel;
public int selectedRow = 0;
private Vector rowData;
private int sourceLocation = 0;
private Vector tableListeners = new Vector();
/**
*
* @param filter
*/
public void setTableFilter(String filter) {
filterModel.setFilter(filter, true);
}
/**
*
* @param tableModel
*/
public void setTableModel(TableModel tableModel) {
this.setVisible(true);
this.setAutoscrolls(true);
TableSorter tableSorter = new TableSorter(tableModel);
filterModel = new TableExclusionFilterModel(tableSorter, tableModel.getColumnCount(), true);
super.setModel(filterModel);
tableSorter.setTableHeader(getTableHeader());
//Set up tool tips for column headers.
getTableHeader().setToolTipText("Click to specify sorting; Control-Click to specify secondary sorting");
setRowSelectionAllowed(true);
setColumnSelectionAllowed(false);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
ListSelectionModel rowSM = getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting())
return;
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
} else {
selectedRow = lsm.getMinSelectionIndex();
setSelectedRow(selectedRow);
}
}
});
addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// System.out.println("double click");
setRowAccepted(selectedRow);
}
}
});
addKeyListener(
new KeyAdapter() {
// public void keyReleased(KeyEvent e) {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
setRowAccepted(selectedRow);
}
}
});
}
/**
*
* @return
*/
public int getSelectedRow() {
return selectedRow;
}
/**
*
* @param acceptedRow
*/
public void setRowAccepted(int acceptedRow) {
Iterator itr = tableListeners.iterator();
while (itr.hasNext()) {
TableListenerInterface listener = (TableListenerInterface)itr.next();
listener.setRowAccepted(getRow(acceptedRow));
}
}
/**
*
* @param l
*/
public void addTableListener(TableListenerInterface l) {
tableListeners.add(l);
}
/**
*
* @param l
*/
public void removeTableListener(TableListenerInterface l) {
tableListeners.remove(l);
}
/**
*
* @param selectedRow
*/
public void setSelectedRow(int selectedRow) {
this.selectedRow = selectedRow;
}
/**
*
* @param selectedRow
* @return
*/
public Vector getRow(int selectedRow) {
Vector rowValue = null;
if (selectedRow >= 0) {
rowValue = new Vector();
for (int i = 0; i < getColumnCount(); i++) {
rowValue.add(getValueAt(selectedRow, i));
}
}
return rowValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -