java中JTable 通过检索搜索出某行的其中一个数据 将该行置顶并改变颜色 连数据库多列数据咋办?
1个回答
展开全部
不是很明白你的要求。具体的操作是怎样的的呢?
假设数据库中有个学生表:
学号 姓名 年龄
001 张三 20
002 李四 21
假设按姓名搜索,输入搜索条件“张”:
情况1、根据搜索条件直接搜索数据库记录(通常都是这样搜索),那从数据库搜索出的记录就只有姓名包含“张”的记录,然后JTable中也就只展示这些记录,就无所谓置顶不置顶啦。
情况2、只是根据搜索条件在当前JTable展示的数据中搜索,代码如下:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
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();
searchPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
searchPanel.setLayout(new GridLayout(2, 3, 2, 3));
searchPanel.add(new JLabel("学号"));
numField = new JTextField(20);
searchPanel.add(numField);
searchByNumButton = new JButton("按学号搜索");
searchByNumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.getSelectionModel().clearSelection();
tableModel.doSearchByNum(numField.getText());
}
});
searchPanel.add(searchByNumButton);
searchPanel.add(new JLabel("姓名"));
nameField = new JTextField(20);
searchPanel.add(nameField);
searchByNameButton = new JButton("按姓名搜索");
searchByNameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.getSelectionModel().clearSelection();
tableModel.doSearchByName(nameField.getText());
}
});
searchPanel.add(searchByNameButton);
getContentPane().add(searchPanel, BorderLayout.NORTH);
tableModel = new TestTableModel();
tableModel.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (tableModel.getHitRowCount() > 0) {
table.setRowSelectionInterval(0, tableModel.getHitRowCount() - 1);
}
}
});
table = new JTable(tableModel);
JScrollPane jsp = new JScrollPane(table);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void main(String[] args) {
JTableTest self = new JTableTest();
self.setVisible(true);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class TestTableModel extends AbstractTableModel {
private List<Student> data = Arrays.asList(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 data.size();
}
@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 = data.get(rowIndex);
if (columnIndex == 0)
return student.getNum();
else if (columnIndex == 1)
return student.getName();
return student.getAge();
}
/** 按姓名搜索 */
public void doSearchByName(String key) {
searchHitRows.clear();
if (key == null || "".equals(key))
return;
for (int i = 0; i < data.size(); i++) {
Student studentI = data.get(i);
String value = studentI.getName();
if (value.indexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
Student studentJ = data.get(j);
String valueJ = studentJ.getName();
if (valueJ.indexOf(key) >= 0)
break;
}
Student temp = data.get(j + 1);
data.set(j + 1, studentI);
data.set(i, temp);
searchHitRows.add(Integer.valueOf(j + 1));
}
fireTableDataChanged();
}
/** 按学号搜索 */
public void doSearchByNum(String key) {
searchHitRows.clear();
if (key == null || "".equals(key))
return;
for (int i = 0; i < data.size(); i++) {
Student studentI = data.get(i);
String value = studentI.getNum();
if (value.indexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
Student studentJ = data.get(j);
String valueJ = studentJ.getNum();
if (valueJ.indexOf(key) >= 0)
break;
}
Student temp = data.get(j + 1);
data.set(j + 1, studentI);
data.set(i, temp);
searchHitRows.add(Integer.valueOf(j + 1));
}
fireTableDataChanged();
}
public void setData(List<Student> data) {
if (data == null)
throw new IllegalArgumentException("参数data不能为null。");
this.data = data;
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRows.size();
}
}
从数据库查询出记录后,转成Student对象列表调用TestTableModel的setData设置表格的数据。
追问
我查的出来的是map.get("列名"),咋个放进去?
追答
把数据库查出的数据转化成TestTableModel中data需要的数据格式,然后调用TestTableModel.setData方法。
你说的map.get("列名"),是一个map中存放一条数据库记录的值,key是列名,value是列的取值,是这样吗?
如果是这样,那假设有studentMap1,studentMap2,那:
List<Student> students = new ArrayList<Student>();
students.add(new Student(studentMap1.get("学号列名"), studentMap1.get("姓名列名"), studentMap1.get("年龄列名")));
students.add(new Student(studentMap2.get("学号列名"), studentMap2.get("姓名列名"), studentMap2.get("年龄列名")));
tableModel.seData(students);
就把map中的数据都放到JTable中去了。
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询