我尝试使用 java swing 做一个搜索系统:
public void search(ActionEvent e){
String textEntered=getJTextField_searchField().getText();
if(textEntered.equals("")){
JOptionPane.showMessageDialog(null,"Please enter a word");
}
else{
kioskViewevents viewEvents=new kioskViewevents(textEntered);
if(viewEvents.searchContent() == true){
getJScrollPane().setVisible(false);
getJScrollPane_search();
}
else{
JOptionPane.showMessageDialog(null,"No result found");
}
}
}
此方法将在单击搜索按钮后执行。它将隐藏前一个表,然后替换为另一个表。然后我在表格中填充搜索结果。
public void SetUpJTable_search(){
DefaultTableModel tableModel = (DefaultTableModel) jTableSearch.getModel();
String[] data = new String[5];
db.setUp("IT Innovation Project");
String sql = "Select event_ID,event_Price,event_Title,event_Time,event_Date FROM crossEvent WHERE event_Title='"+ textEntered +"'";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
data[0] = resultSet.getString("event_ID");
data[1] = resultSet.getString("event_Price");
data[2] = resultSet.getString("event_Title");
data[3] = resultSet.getString("event_Time");
data[4] = resultSet.getString("event_Date");
tableModel.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
我设置了我的搜索表:
private JTable getJTable_search() {
String header[] = { "Event ID", "Price", "Title","Time","Date"};
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
tableModel.setColumnIdentifiers(header);
jTableSearch.setAutoCreateColumnsFromModel(false);
jTableSearch.getColumnModel().getColumn(0).setMinWidth(100);
jTableSearch.getColumnModel().getColumn(0).setMaxWidth(100);
jTableSearch.getColumnModel().getColumn(1).setMinWidth(200);
jTableSearch.getColumnModel().getColumn(1).setMaxWidth(300);
jTableSearch.getColumnModel().getColumn(2).setMinWidth(300);
jTableSearch.getColumnModel().getColumn(2).setMaxWidth(400);
jTableSearch.getColumnModel().getColumn(3).setMinWidth(100);
jTableSearch.getColumnModel().getColumn(3).setMaxWidth(100);
jTableSearch.getColumnModel().getColumn(4).setMinWidth(100);
jTableSearch.getColumnModel().getColumn(4).setMaxWidth(100);
jTableSearch.getTableHeader().setFont(new Font("Dialog", Font.PLAIN, 20));
jTableSearch.getTableHeader().setForeground(Color.white);
jTableSearch.getTableHeader().setBackground(new Color(102, 102, 102));
jTableSearch.setRowHeight(50);
jTableSearch.setBounds(new Rectangle(97, 157, 800, 450));
jTableSearch.setFont(new Font("Dialog", Font.PLAIN, 18));
return jTableSearch;
}
我的搜索表滚动窗格:
private JScrollPane getJScrollPane_search(){
if (jScrollPane_search == null) {
jScrollPane_search = new JScrollPane();
jScrollPane_search.setBounds(new Rectangle(75, 180, 800, 450));
jScrollPane_search.setViewportView(getJTable_search());
}
SetUpJTable_search();
return jScrollPane_search;
}
这整个垃圾代码是在执行搜索之前用数据库中存在的数据填充表:
private JTable getJTable() {
String header[] = { "Event ID", "Price", "Title","Time","Date"};
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
tableModel.setColumnIdentifiers(header);
// Hide Topic ID
jTable.setAutoCreateColumnsFromModel(false);
jTable.getColumnModel().getColumn(0).setMinWidth(100);
jTable.getColumnModel().getColumn(0).setMaxWidth(100);
jTable.getColumnModel().getColumn(1).setMinWidth(200);
jTable.getColumnModel().getColumn(1).setMaxWidth(300);
jTable.getColumnModel().getColumn(2).setMinWidth(300);
jTable.getColumnModel().getColumn(2).setMaxWidth(400);
jTable.getColumnModel().getColumn(3).setMinWidth(100);
jTable.getColumnModel().getColumn(3).setMaxWidth(100);
jTable.getColumnModel().getColumn(4).setMinWidth(100);
jTable.getColumnModel().getColumn(4).setMaxWidth(100);
jTable.getTableHeader().setFont(new Font("Dialog", Font.PLAIN, 20));
jTable.getTableHeader().setForeground(Color.white);
jTable.getTableHeader().setBackground(new Color(102, 102, 102));
jTable.setRowHeight(50);
jTable.setBounds(new Rectangle(97, 157, 800, 450));
jTable.setFont(new Font("Dialog", Font.PLAIN, 18));
return jTable;
}
public void SetUpJTable() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
String[] data = new String[5];
db.setUp("IT Innovation Project");
String sql = "Select event_ID,event_Price,event_Title,event_Time,event_Date FROM crossEvent";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
tableModel.getDataVector().removeAllElements();
tableModel.fireTableDataChanged();
try {
while (resultSet.next()) {
data[0] = resultSet.getString("event_ID");
data[1] = resultSet.getString("event_Price");
data[2] = resultSet.getString("event_Title");
data[3] = resultSet.getString("event_Time");
data[4] = resultSet.getString("event_Date");
tableModel.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
但是,我收到一条错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at kioskViewevents.ui.ViewEventsUI.getJTable_search(ViewEventsUI.java:158)
at kioskViewevents.ui.ViewEventsUI.getJScrollPane_search(ViewEventsUI.java:201)
at kioskViewevents.ui.ViewEventsUI.search(ViewEventsUI.java:330)
at kioskViewevents.ui.ViewEventsUI$4.actionPerformed(ViewEventsUI.java:315)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
任何指南?提前致谢。