import javaawtBorderLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTable;
import javaxswingJTextField;
import javaxswingeventTableModelEvent;
import javaxswingeventTableModelListener;
public class JTableTest extends JFrame {
private JPanel searchPanel;
private JTextField searchField;
private JButton searchButton;
private JTable table;
private TestTableModel tableModel;
public JTableTest() {
super("Simple JTable Test");
setSize(400, 300);
searchPanel = new JPanel();
searchField = new JTextField(20);
searchPaneladd(searchField);
searchButton = new JButton("搜索");
searchButtonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tablegetSelectionModel()clearSelection();
doSearch();
}
});
searchPaneladd(searchButton);
getContentPane()add(searchPanel, BorderLayoutNORTH);
tableModel = new TestTableModel();
tableModeladdTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (tableModelgetHitRowCount() > 0) {
tablesetRowSelectionInterval(0, tableModelgetHitRowCount() - 1);
}
}
});
table = new JTable(tableModel);
JScrollPane jsp = new JScrollPane(table);
getContentPane()add(jsp, BorderLayoutCENTER);
}
private void doSearch() {
tableModeldoSearch(searchFieldgetText());
}
public static void main(String[] args) {
JTableTest self = new JTableTest();
selfsetVisible(true);
}
}import javautilArrayList;
import javautilArrays;
import javautilList;
import javaxswingtableAbstractTableModel;
public class TestTableModel extends AbstractTableModel {
private List<String> data = ArraysasList("王二", "王二小", "张三", "张三丰", "李四", "李四光");
private List<Integer> searchHitRows = new ArrayList<Integer>();
@Override
public int getRowCount() {
return datasize();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public String getColumnName(int column) {
return "姓名";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex < datasize() dataget(rowIndex) : null;
}
public void doSearch(String key) {
searchHitRowsclear();
if (key == null || ""equals(key))
return;
for (int i = 0; i < datasize(); i++) {
String value = dataget(i);
if (valueindexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
String valueJ = dataget(j);
if (valueJindexOf(key) >= 0)
break;
}
String temp = dataget(j + 1);
dataset(j + 1, value);
dataset(i, temp);
searchHitRowsadd(IntegervalueOf(j + 1));
}
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRowssize();
}
}
代码如下:
/获取鼠标点击的行号
@param y 点击位置的纵坐标值
@return 行号
/
private int getClickedRow(int y) {
// JTable的行总数
int rowCount = tablegetRowCount();
// JTable行的累计纵坐标
int rowY = 0;
if (y < 0) {
return -1;
}
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
rowY = rowY + (int) tablegetCellRect(rowIndex, 0, true)getHeight();
if (y < rowY) {
return rowIndex;
}
}
return -1;
}
t是JTable的实例
光标送去2行、2列
tgetCellEditor(2, 2)getTableCellEditorComponent(t, tgetValueAt(2, 2), true, 2, 2)requestFocus();
不是很明白你的要求。具体的 *** 作是怎样的的呢?
假设数据库中有个学生表:
学号 姓名 年龄
001 张三 20
002 李四 21
假设按姓名搜索,输入搜索条件“张”:
情况1、根据搜索条件直接搜索数据库记录(通常都是这样搜索),那从数据库搜索出的记录就只有姓名包含“张”的记录,然后JTable中也就只展示这些记录,就无所谓置顶不置顶啦。
情况2、只是根据搜索条件在当前JTable展示的数据中搜索,代码如下:
import javaawtBorderLayout;import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTable;
import javaxswingJTextField;
import javaxswingborderEmptyBorder;
import javaxswingeventTableModelEvent;
import javaxswingeventTableModelListener;
public class JTableTest extends JFrame {
private JPanel searchPanel;
private JTextField numField;
private JButton searchByNumButton;
private JTextField nameField;
private JButton searchByNameButton;
private JTable table;
private TestTableModel tableModel;
public JTableTest() {
super("Simple JTable Test");
setSize(400, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
searchPanel = new JPanel();
searchPanelsetBorder(new EmptyBorder(3, 3, 3, 3));
searchPanelsetLayout(new GridLayout(2, 3, 2, 3));
searchPaneladd(new JLabel("学号"));
numField = new JTextField(20);
searchPaneladd(numField);
searchByNumButton = new JButton("按学号搜索");
searchByNumButtonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tablegetSelectionModel()clearSelection();
tableModeldoSearchByNum(numFieldgetText());
}
});
searchPaneladd(searchByNumButton);
searchPaneladd(new JLabel("姓名"));
nameField = new JTextField(20);
searchPaneladd(nameField);
searchByNameButton = new JButton("按姓名搜索");
searchByNameButtonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tablegetSelectionModel()clearSelection();
tableModeldoSearchByName(nameFieldgetText());
}
});
searchPaneladd(searchByNameButton);
getContentPane()add(searchPanel, BorderLayoutNORTH);
tableModel = new TestTableModel();
tableModeladdTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (tableModelgetHitRowCount() > 0) {
tablesetRowSelectionInterval(0, tableModelgetHitRowCount() - 1);
}
}
});
table = new JTable(tableModel);
JScrollPane jsp = new JScrollPane(table);
getContentPane()add(jsp, BorderLayoutCENTER);
}
public static void main(String[] args) {
JTableTest self = new JTableTest();
selfsetVisible(true);
}
}import javautilArrayList;
import javautilArrays;
import javautilList;
import javaxswingtableAbstractTableModel;
public class TestTableModel extends AbstractTableModel {
private List<Student> data = ArraysasList(new Student("001", "王二", 20), new Student("002",
"王二小", 19), new Student("003", "张三", 20), new Student("004", "张三丰", 21), new Student("005",
"李四", 21), new Student("006", "李四光", 19));
private List<Integer> searchHitRows = new ArrayList<Integer>();
@Override
public int getRowCount() {
return datasize();
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public String getColumnName(int column) {
if (column == 0)
return "学号";
else if (column == 1)
return "姓名";
return "年龄";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = dataget(rowIndex);
if (columnIndex == 0)
return studentgetNum();
else if (columnIndex == 1)
return studentgetName();
return studentgetAge();
}
/ 按姓名搜索 /
public void doSearchByName(String key) {
searchHitRowsclear();
if (key == null || ""equals(key))
return;
for (int i = 0; i < datasize(); i++) {
Student studentI = dataget(i);
String value = studentIgetName();
if (valueindexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
Student studentJ = dataget(j);
String valueJ = studentJgetName();
if (valueJindexOf(key) >= 0)
break;
}
Student temp = dataget(j + 1);
dataset(j + 1, studentI);
dataset(i, temp);
searchHitRowsadd(IntegervalueOf(j + 1));
}
fireTableDataChanged();
}
/ 按学号搜索 /
public void doSearchByNum(String key) {
searchHitRowsclear();
if (key == null || ""equals(key))
return;
for (int i = 0; i < datasize(); i++) {
Student studentI = dataget(i);
String value = studentIgetNum();
if (valueindexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
Student studentJ = dataget(j);
String valueJ = studentJgetNum();
if (valueJindexOf(key) >= 0)
break;
}
Student temp = dataget(j + 1);
dataset(j + 1, studentI);
dataset(i, temp);
searchHitRowsadd(IntegervalueOf(j + 1));
}
fireTableDataChanged();
}
public void setData(List<Student> data) {
if (data == null)
throw new IllegalArgumentException("参数data不能为null。");
thisdata = data;
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRowssize();
}
}
从数据库查询出记录后,转成Student对象列表调用TestTableModel的setData设置表格的数据。
以上就是关于java中JTable 通过检索搜索出某行的其中一个数据 将该行置顶并改变颜色全部的内容,包括:java中JTable 通过检索搜索出某行的其中一个数据 将该行置顶并改变颜色、java 中 JTable里怎么得知哪一行被选中、java jtable根据行数吧光标移动到指定的行数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)