Results 1 to 3 of 3
- 02-07-2010, 07:23 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
Highlighting Multiple Selected Rows
as you can see in the program I wrote a simple block that displays a console ouput of the selected rows, Im trying to highlight a multiple selected row in a search by a JTextField, for example I entered 20, it will highlight all the rows that has that value, but I can only Highlight the last one in the search selection, I dont know how to implement .getSelectedRows() in a right way.. please I need help with this while im trying to resolve it on my own..Java Code:public class InventoryTEST4 extends JFrame { private JTextField searchField; private JTable table; private JPanel panel; private JScrollPane scroll; public InventoryTEST4() { 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); table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); scroll = new JScrollPane(table); scroll.setBounds(0, 100, 900, 500); searchField.setBounds(10, 50, 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)); for (int q = 0; q <= table.getSelectedRows().length - 1; q++) { System.out.println(table.getSelectedRows()[q]); } // this will automatically set the focus of the searched/selected row/value table.setRowSelectionInterval(row, row); for (int q = 0; q <= table.getColumnCount() - 1; q++) { table.getColumnModel().getColumn(q).setCellRenderer(new HighlightRenderer()); } } } } } }); panel.add(searchField); panel.add(scroll); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Inventory Window"); setSize(900, 900); 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 InventoryTEST4(); } }); } }Last edited by bigj; 02-07-2010 at 08:09 AM.
- 02-07-2010, 04:22 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,144
- Rep Power
- 5
That resets the selection. I believe you need to use:Java Code:table.setRowSelectionInterval(row, row);
Java Code:table.addRowSelectionInterval(row, row);
- 02-07-2010, 05:31 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
thank you sir..
Solved:
Java Code:public class InventoryTEST4 extends JFrame { private JTextField searchField; private JTable table; private JPanel panel; private JScrollPane scroll; public InventoryTEST4() { 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.setRowSelectionAllowed(true); table.addMouseListener(new MyMouseListener()); scroll = new JScrollPane(table); scroll.setBounds(0, 100, 900, 500); searchField.setBounds(10, 50, 150, 20); searchField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // set the column selection to false to gain multiple row selection table.setColumnSelectionAllowed(false); table.clearSelection(); 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.addRowSelectionInterval(row, row); for (int i = 0; i <= table.getColumnCount() - 1; i++) { table.getColumnModel().getColumn(i).setCellRenderer(new SingleAndMultipleSearchRenderer()); } } } } } }); panel.add(searchField); panel.add(scroll); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Inventory Window"); setSize(900, 900); setLocationRelativeTo(null); setVisible(true); } private class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { // set the column selection to true to gain single cell selection table.setColumnSelectionAllowed(true); for (int i = 0; i <= table.getColumnCount() - 1; i++) { table.getColumnModel().getColumn(i).setCellRenderer(new CellSelectionRenderer()); } } } // class for rendering a single cell selection private class CellSelectionRenderer 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(isSelected == true) { // 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; } } // class for single Row or multiple Row Selection renderer private class SingleAndMultipleSearchRenderer 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(isSelected == true) { // 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 InventoryTEST4(); } }); } }
Similar Threads
-
Eclipse Syntax Highlighting
By garrettgjb in forum EclipseReplies: 1Last Post: 08-24-2009, 03:10 PM -
inserting multiple rows from one table into another
By xcallmejudasx in forum JDBCReplies: 1Last Post: 04-23-2009, 07:55 PM -
How to get multiple rows from Stored Procedure
By SRJ1957 in forum Advanced JavaReplies: 0Last Post: 08-11-2008, 08:26 PM -
To highlight multiple selected values in listbox
By swapnanair in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-06-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks