你是说线程优先级么,可以设置的一般级别为5,设高点就可以了
创新互联专注于港闸网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供港闸营销型网站建设,港闸网站制作、港闸网页设计、港闸网站官网定制、小程序设计服务,打造港闸网络公司原创品牌,更为您提供港闸网站排名全网营销落地服务。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
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);
searchPanel.add(searchField);
searchButton = new JButton("搜索");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.getSelectionModel().clearSelection();
doSearch();
}
});
searchPanel.add(searchButton);
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);
}
private void doSearch() {
tableModel.doSearch(searchField.getText());
}
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 ListString data = Arrays.asList("王二", "王二小", "张三", "张三丰", "李四", "李四光");
private ListInteger searchHitRows = new ArrayListInteger();
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public String getColumnName(int column) {
return "姓名";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex data.size() ? data.get(rowIndex) : null;
}
public void doSearch(String key) {
searchHitRows.clear();
if (key == null || "".equals(key))
return;
for (int i = 0; i data.size(); i++) {
String value = data.get(i);
if (value.indexOf(key) 0)
continue;
int j = 0;
for (j = i - 1; j = 0; j--) {
String valueJ = data.get(j);
if (valueJ.indexOf(key) = 0)
break;
}
String temp = data.get(j + 1);
data.set(j + 1, value);
data.set(i, temp);
searchHitRows.add(Integer.valueOf(j + 1));
}
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRows.size();
}
}
不是很明白你的要求。具体的操作是怎样的的呢?
假设数据库中有个学生表:
学号 姓名 年龄
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 ListStudent 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 ListInteger searchHitRows = new ArrayListInteger();
@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(ListStudent data) {
if (data == null)
throw new IllegalArgumentException("参数data不能为null。");
this.data = data;
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRows.size();
}
}
从数据库查询出记录后,转成Student对象列表调用TestTableModel的setData设置表格的数据。
查数据库的时候按时间倒序取出来就好了啊?
select * from tab where **=** order by publishTime desc;