【发布时间】:2014-09-27 18:43:11
【问题描述】:
我这里有一个不那么短的工作代码。我的搜索按钮需要帮助。如果我输入textfield 并单击搜索按钮,表数据应更改为textfield 中写入的内容。如果它没有搜索任何内容,那么该表应该是空白的。我不能在这里工作是将新数据显示到表格中。我应该删除第一个数据,然后删除repaint()?或者有没有更好的解决方案?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TableSearch {
JFrame Card = new JFrame();
static JTable table;
JTabbedPane card_tab = new JTabbedPane(); // FOR TAB
JPanel buttonpanel = new JPanel(); // FOR BUTTON BELOW
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
Border etch = BorderFactory.createEtchedBorder(Color.white, Color.gray);
Border margin = new EmptyBorder(10, 10, 10, 10);
JTextField text;
public TableSearch() throws SQLException, IOException {
Card.setVisible(true);
Card.setSize(821, 421);
Card.setTitle("File Maintenance");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - Card.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - Card.getHeight()) / 2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.setBackground(new Color(0, 128, 0));
//container.setBorder(margin);
JPanel labelpanel = new JPanel();
labelpanel.setLayout(flow);
labelpanel.setBackground(new Color(0, 128, 0));
JLabel label_1 = new JLabel("Description:");
label_1.setFont(new Font("Calibri", Font.BOLD, 20));
label_1.setForeground(Color.YELLOW);
labelpanel.add(label_1);
text = new JTextField();
text.setPreferredSize(new Dimension(200, 20));
labelpanel.add(text);
JButton btnsearch = new JButton("Search");
btnsearch.setPreferredSize(new Dimension(90, 20));
btnsearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String search = text.getText();
System.out.println(search);
//What should I put here to change the table data???
}
});
labelpanel.add(btnsearch);
JPanel jp1 = new JPanel();
jp1.setBackground(new Color(0, 128, 0));
//jp1.setBorder(new CompoundBorder(etch,margin));
jp1.setLayout(new BorderLayout());
table = new JTable(new TableModel());
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
JTableHeader header = table.getTableHeader();
header.setBackground(new Color(224, 223, 227));
header.setFont(new Font("Calibri", Font.BOLD, 20));
table.setRowHeight(25);
jp1.add(scrollPane);
Card.add(container);
container.add(labelpanel, BorderLayout.PAGE_START);
container.add(jp1, BorderLayout.CENTER);
}
public static class TableModel extends DefaultTableModel {
public TableModel() {
super(new Object[]{"Description", "Code", "Price"}, 0);
for (int index = 0; index < 4; index++) {
addRow(new Object[]{index, index, index});
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TableSearch();
} catch (SQLException ex) {
Logger.getLogger(TableSearch.class.getName())
.log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TableSearch.class.getName())
.log(Level.SEVERE, null, ex);
}
}
});
}
}
【问题讨论】:
-
@MadProgrammer 啊不。我已经可以在我的原始程序中搜索了。我没有将它包含在我的代码中的唯一原因是它来自数据库并且它位于哈希图中。但是我的主要问题是我不知道如何将搜索到的项目放在表格中。
-
假设您有数据,为什么不直接使用
addRow的addRow...它接受Object[]...TableModel形式的数据是一个非常糟糕的命名类顺便说一句,因为javax.swing.Table包中已经有一个TableModel;) -
(因为它来自数据库,并且它位于哈希图 vs 并单击搜索按钮,表数据应更改为)然后这个问题问错了,你想在数据库或 JTable 中搜索,顺便说一句,不要使用HasMap,使用基于util.List的数组,
-
@MadProgrammer 对不起班级名称。我会马上编辑它。 addRow 很好,但是当我的表模型在方法中时如何添加行?我的意思是程序将如何确定单击搜索按钮并更改数据?
标签: java swing jtable refresh rowfilter