我有一个包含一些数据的表.当我在textField中搜索文本时,表格的外观发生变化,并且textFields消失了.我不知道为什么,我不知道我是否做对了
这是我的整个代码.
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.BadLocationException;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.RowFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class test3 extends JFrame {
private JPanel contentPane;
private JTable table;
private TableModel tableModel;
private JTextField textField;
private String textForSearch;
private TableRowSorter sorter;
/**
* Launch the application.
*/
public static void main(String[] args) {
/**
* Create the frame.
*/
public test3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 346);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(63, 52, 305, 191);
contentPane.add(scrollPane);
String columns [] = {
"First Name", "Last Name", "Middle Name"
};
String data[][] = new String [3][3];
data [0][0] = "denise";
data [0][1] = "alyson";
data [0][2] = "berania";
data [1][0] = "denden";
data [1][1] = "pelesco";
data [1][2] = "pogi";
data [2][0] = "ryan";
data [2][1] = "ewan";
data [2][2] = "santos";
tableModel = new DefaultTableModel(data, columns);
table = new JTable(tableModel);
scrollPane.setViewportView(table);
sorter = new TableRowSorter(tableModel);
table.setRowSorter(sorter);
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textForSearch = textField.getText();
if(textForSearch.length()==0){
sorter.setRowFilter(null);
}else{
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + textForSearch));
}
for(int i =0;i
table.getColumnModel().getColumn(i).setCellRenderer(getRenderer());
}
}
});
textField.setBounds(262, 21, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
private TableCellRenderer getRenderer() {
return new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
if(arg1 != null){
textField.setText(arg1.toString());
String string = arg1.toString();
if(string.contains(textForSearch)){
int indexOf = string.indexOf(textForSearch);
try {
textField.getHighlighter().addHighlight(indexOf,indexOf+textForSearch.length(),new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.RED));
} catch (BadLocationException e) {
e.printStackTrace();
}
}
} else {
textField.setText("");
textField.getHighlighter().removeAllHighlights();
}
return textField;
}
};
}
}