Quick Search für JTable: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springenZeile 6: | Zeile 6: | ||
table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); | table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); | ||
<code=java> | <code=java> | ||
− | public static | + | public static void setQuickSearch(final JTable table) { |
+ | // table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); | ||
final JTextField searchField = new JTextField(); | final JTextField searchField = new JTextField(); | ||
class Search { | class Search { | ||
+ | |||
void search() { | void search() { | ||
table.clearSelection(); | table.clearSelection(); | ||
String text = searchField.getText(); | String text = searchField.getText(); | ||
if (text.length() == 0) { | if (text.length() == 0) { | ||
− | return ; | + | return; |
} | } | ||
for (int row = 0; row < table.getRowCount(); row++) { | for (int row = 0; row < table.getRowCount(); row++) { | ||
Zeile 26: | Zeile 28: | ||
} | } | ||
table.addKeyListener(new KeyAdapter() { | table.addKeyListener(new KeyAdapter() { | ||
+ | |||
@Override | @Override | ||
public void keyPressed(final KeyEvent evt) { | public void keyPressed(final KeyEvent evt) { | ||
char ch = evt.getKeyChar(); | char ch = evt.getKeyChar(); | ||
− | if(!Character.isLetterOrDigit(ch)){ | + | if (!Character.isLetterOrDigit(ch)) { |
− | return ; | + | return; |
} | } | ||
int selectedRow = table.getSelectedRow(); | int selectedRow = table.getSelectedRow(); | ||
int selectedColumn = table.getSelectedColumn(); | int selectedColumn = table.getSelectedColumn(); | ||
− | if (selectedRow >= 0 && selectedColumn >= 0 && | + | Object clientProperty = table.getClientProperty("JTable.autoStartsEdit"); |
− | table.isCellEditable(table.getSelectedRow(), table.getSelectedColumn())){ | + | if ((clientProperty == null || clientProperty != null && (Boolean)clientProperty) && |
− | return ; | + | selectedRow >= 0 && selectedColumn >= 0 && |
+ | table.isCellEditable(table.getSelectedRow(), table.getSelectedColumn())) { | ||
+ | return; | ||
} | } | ||
searchField.setText(String.valueOf(ch)); | searchField.setText(String.valueOf(ch)); | ||
Zeile 50: | Zeile 55: | ||
d.setVisible(true); | d.setVisible(true); | ||
searchField.getDocument().addDocumentListener(new DocumentListener() { | searchField.getDocument().addDocumentListener(new DocumentListener() { | ||
+ | |||
public void insertUpdate(final DocumentEvent e) { | public void insertUpdate(final DocumentEvent e) { | ||
s.search(); | s.search(); | ||
} | } | ||
+ | |||
public void removeUpdate(final DocumentEvent e) { | public void removeUpdate(final DocumentEvent e) { | ||
s.search(); | s.search(); | ||
} | } | ||
+ | |||
public void changedUpdate(final DocumentEvent e) { | public void changedUpdate(final DocumentEvent e) { | ||
s.search(); | s.search(); | ||
Zeile 61: | Zeile 69: | ||
}); | }); | ||
searchField.addFocusListener(new FocusListener() { | searchField.addFocusListener(new FocusListener() { | ||
+ | |||
@Override | @Override | ||
public void focusGained(final FocusEvent e) { | public void focusGained(final FocusEvent e) { | ||
} | } | ||
+ | |||
@Override | @Override | ||
public void focusLost(final FocusEvent e) { | public void focusLost(final FocusEvent e) { | ||
Zeile 70: | Zeile 80: | ||
}); | }); | ||
Action exit = new AbstractAction() { | Action exit = new AbstractAction() { | ||
+ | |||
@Override | @Override | ||
public void actionPerformed(final ActionEvent e) { | public void actionPerformed(final ActionEvent e) { |
Version vom 1. Dezember 2009, 22:57 Uhr
Die folgende Methode installiert eine "Quick Search" Funktion für eine Tabelle (JTable). Das Beispiel implementiert die Suche auf der ersten Spalte. Es wird angenommen, dass sie einfachen Text enthält und nicht editierbar ist. Die Suche sollte automatisch starten, wenn der Fokus auf einer nicht editierbaren Zelle ist und man den Anfang des gesuchten Textes auf der Tastatur erfasst.
ANMERKUNG: Mit der folgenden Property kann man den Quick Search auch für editierbare Zellen implementieren:
table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); <code=java> public static void setQuickSearch(final JTable table) { // table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
final JTextField searchField = new JTextField(); class Search {
void search() { table.clearSelection(); String text = searchField.getText(); if (text.length() == 0) { return; } for (int row = 0; row < table.getRowCount(); row++) { Object val = table.getValueAt(row, 0); String value = val != null ? val.toString() : ""; if (value.toLowerCase().startsWith(text.toLowerCase())) { table.changeSelection(row, 0, false, false); break; } } } } table.addKeyListener(new KeyAdapter() {
@Override public void keyPressed(final KeyEvent evt) { char ch = evt.getKeyChar(); if (!Character.isLetterOrDigit(ch)) { return; } int selectedRow = table.getSelectedRow(); int selectedColumn = table.getSelectedColumn(); Object clientProperty = table.getClientProperty("JTable.autoStartsEdit"); if ((clientProperty == null || clientProperty != null && (Boolean)clientProperty) && selectedRow >= 0 && selectedColumn >= 0 && table.isCellEditable(table.getSelectedRow(), table.getSelectedColumn())) { return; } searchField.setText(String.valueOf(ch)); final Search s = new Search(); s.search(); final JDialog d = new JDialog(); d.setUndecorated(true); d.setSize(150, 20); d.setLocation(table.getTableHeader().getLocationOnScreen()); final JLabel lb = new JLabel("Quick Search: "); d.add(lb, BorderLayout.LINE_START); d.add(searchField); d.setVisible(true); searchField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(final DocumentEvent e) { s.search(); }
public void removeUpdate(final DocumentEvent e) { s.search(); }
public void changedUpdate(final DocumentEvent e) { s.search(); } }); searchField.addFocusListener(new FocusListener() {
@Override public void focusGained(final FocusEvent e) { }
@Override public void focusLost(final FocusEvent e) { d.dispose(); } }); Action exit = new AbstractAction() {
@Override public void actionPerformed(final ActionEvent e) { d.dispose(); } }; searchField.setAction(exit); searchField.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit"); searchField.getActionMap().put("exit", exit); } });
} </code=java>