Cómo implementar la búsqueda en un JTable [cerrado]
Frecuentes
Visto 20,524 veces
2
I want to have a feature with JTable wherein I'll provide a textfield to enter a value to be searched from the JTable and if this entered value matches with any of the cell values in the JTable then that particular cell should be highlighted & the cell font should be turned to BOLD. The values will be matched when the user presses Enter key after specifying the value in the text field.
¿Cómo puedo hacer esto?
2 Respuestas
16
This is a way to resolve the problem. Here is the code:
public class JTableSearchAndHighlight extends JFrame {
private JTextField searchField;
private JTable table;
private JPanel panel;
private JScrollPane scroll;
public JTableSearchAndHighlight() {
initializeInventory();
}
private void initializeInventory() {
panel = new JPanel();
searchField = new JTextField();
panel.setLayout(null);
final String[] columnNames = {"Name", "Surname", "Age"};
final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Max", "Dumbass", "10"}, {"Melanie", "Martin", "500"},
{"Jollibe", "Mcdonalds", "15"}};
table = new JTable(data, columnNames);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
scroll = new JScrollPane(table);
scroll.setBounds(0, 200, 900, 150);
searchField.setBounds(10, 100, 150, 20);
searchField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = searchField.getText();
for (int row = 0; row <= table.getRowCount() - 1; row++) {
for (int col = 0; col <= table.getColumnCount() - 1; col++) {
if (value.equals(table.getValueAt(row, col))) {
// this will automatically set the view of the scroll in the location of the value
table.scrollRectToVisible(table.getCellRect(row, 0, true));
// this will automatically set the focus of the searched/selected row/value
table.setRowSelectionInterval(row, row);
for (int i = 0; i <= table.getColumnCount() - 1; i++) {
table.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
}
}
}
}
}
});
panel.add(searchField);
panel.add(scroll);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Inventory Window");
setSize(900, 400);
setLocationRelativeTo(null);
setVisible(true);
}
private class HighlightRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
// everything as usual
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// added behavior
if(row == table.getSelectedRow()) {
// this will customize that kind of border that will be use to highlight a row
setBorder(BorderFactory.createMatteBorder(2, 1, 2, 1, Color.BLACK));
}
return this;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JTableSearchAndHighlight();
}
});
}
}
Respondido 14 Oct 13, 11:10
7
El JXTable
from the SwingX project has built-in support to search the table (look for the Searchable
interface). It also allows to quickly create a search field which uses this Searchable
interface: the JXSearchField
y/o JXSearchPanel
.
If I remember correctly this will cover most of your requirements. Probably only need to add some custom code for making the cell contents bold
Respondido 25 ago 12, 19:08
Not sure who gave the down-vote. Up-voted to nullify it. This is a valid answer - Aerodeslizador lleno de anguilas
@HovercraftFullOfEels I am also wondering what the reason is for the downvote. Always nice if the downvoter at least indicates in a comment why the down vote, allowing me to correct/improve my answer - petirrojo
+1 I think it's brilliant idea - Programador loco
How do I "link" the JXSearchField
con el JXTable
? - Rendichaya
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java swing search jtable or haz tu propia pregunta.
Great! So que has probado And what problems are you having? - Hovercraft Full Of Eels
I didn't try anything, just i want to know if there is a simple way to do it. - aoulhent
Yes, most of this is explained pretty well in the JTable tutorials. Have you gone through them yet? - Hovercraft Full Of Eels
You could search the model for the text of interest As for highlighting and bold, that's the job of the cell renderer. - Hovercraft Full Of Eels
sorry i didn't see the link above, thanks a lot. - aoulhent