下面的代码用于过滤JTable,直到我使用英文字母。它也不区分大小写。我的目标是过滤空白区域和外来字符。我需要以某种方式替换针和大海捞针中的字符,例如č,ľ,ť,ž,ý,á替换为c,l,t,z,y,a。 有没有人有我的要求的经验或工作代码?提前谢谢。
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class Home extends javax.swing.JFrame {
DefaultTableModel model;
TableRowSorter sorter;
public Home() {
initComponents();
model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
String data[] = {"šing","čamg","búng","wámg","fáng","raňk","moňk","púťk","šank","dung","puck","rig","an da da","ku nd ada","c ic inada"};
for(int i=0;i<data.length;i++) {
model.addRow(new Object[] {
data[i]
});
}
sorter = new TableRowSorter<TableModel>(model);
jTable1.setRowSorter(sorter);
}
private void initComponents() {///}
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
String text = jTextField1.getText();
if(text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
public static void main(String args[]) {///}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
答案 0 :(得分:3)
正如@mKorbel所指出的那样:
KeyListener
(不良方法),而是使用DocumentListener
听取文本字段的文本更改。话虽如此,这就是我要做的事情:
Normalizer
,如已建议的那样,将行过滤器附加到表的行分类器,该行分类器同时考虑原始条目文本值及其规范化字符串表示。这里有MCVE可以玩。
import java.awt.BorderLayout;
import java.text.Normalizer;
import javax.swing.BorderFactory;
import javax.swing.DefaultRowSorter;
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.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
* @author dic19
*/
public class Demo {
private JTable table;
private JTextField textField;
private void createAndShowGui() {
textField = new JTextField(20);
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(final DocumentEvent e) {
Demo.this.filterTable(textField.getText());
}
@Override
public void insertUpdate(final DocumentEvent e) {
Demo.this.filterTable(textField.getText());
}
@Override
public void changedUpdate(final DocumentEvent e) {
Demo.this.filterTable(textField.getText());
}
});
String[] header = new String[] {"Foreign text", "English text"};
DefaultTableModel model = new DefaultTableModel(header, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
};
model.addRow(new Object[]{"šing", "wamg"});
model.addRow(new Object[]{"čamg", "bung"});
model.addRow(new Object[]{"búng", "sing"});
model.addRow(new Object[]{"wámg", "camg"});
table = new JTable(model);
table.setAutoCreateRowSorter(true);
JPanel filterPanel = new JPanel();
filterPanel.add(new JLabel("Filter text:"));
filterPanel.add(textField);
JPanel content = new JPanel(new BorderLayout(8, 8));
content.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
content.add(filterPanel, BorderLayout.PAGE_START);
content.add(new JScrollPane(table), BorderLayout.CENTER);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(content);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void filterTable(final String query) {
RowFilter<TableModel, Integer> filter = null;
if (query.length() > 0) {
filter = new RowFilter<TableModel, Integer>() {
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
// Normalize the query string.
String normalizedQuery = Normalizer.normalize(query, Normalizer.Form.NFD);
normalizedQuery = normalizedQuery.replaceAll("[^\\x00-\\x7F]", "");
for (int i = 0; i < entry.getValueCount(); i++) {
// Get both the string value and its normalized string
String stringValue = entry.getStringValue(i);
String normalizedStringValue = Normalizer.normalize(stringValue, Normalizer.Form.NFD);
normalizedStringValue = normalizedStringValue.replaceAll("[^\\x00-\\x7F]", "");
// Two cases need to be evaluated here:
// 1. The normalized string value contains the normalized query string.
// 2. The string value contains the query string.
if (normalizedStringValue.contains(normalizedQuery) || stringValue.contains(query)) {
return true;
}
}
return false;
}
};
}
((DefaultRowSorter)table.getRowSorter()).setRowFilter(filter);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGui();
}
});
}
}
答案 1 :(得分:2)
您可以使用java.text.Normalizer
例如:
String plain = "č,ľ,ť,ž,ý,á";
plain = Normalizer.normalize(plain, Normalizer.Form.NFD);
String clean = plain.replaceAll("[^\\x00-\\x7F]", "");
System.out.println(clean);
// Will output c,l,t,z,y,a
第一步取代角色:
U+010D LATIN SMALL LETTER C WITH CARON
通过&#34;分解形式&#34;:
U+0063 LATIN SMALL LETTER C
U+030C COMBINING CARON
第二步删除所有非ascii字符。
答案 2 :(得分:2)