setAllawaysOnTop(true);
创新互联公司主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设、程序开发、网站优化、微网站、微信平台小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都做网站、成都网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
大概是这个·你自己打的时候看一下提示
不是很明白你的要求。具体的操作是怎样的的呢?
假设数据库中有个学生表:
学号 姓名 年龄
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设置表格的数据。
网页?!你打算在网页中使用JFileChooser?!我告诉你,绝对的错误!!
如果你是这样弄,那个JFileChooser就不是从网页中弹出来的,而是在服务器中出来的!!客户端那边是完全看不到的,你的B/S程序就这样废了。
如果说要用弹出一个选择文件的框,可以再网页中使用input中的type属性设置为File,对文件的处理,在提交到servlet中处理,form中还要弄成是enctype="multipart/form-data"。。。
不知道你能看懂多少,不懂再问
查数据库的时候按时间倒序取出来就好了啊?
select * from tab where **=** order by publishTime desc;