Results 1 to 3 of 3
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default Highlighting Multiple Selected Rows

    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();
                }
            });
        }
    }
    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..
    Last edited by bigj; 02-07-2010 at 08:09 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    table.setRowSelectionInterval(row, row);
    That resets the selection. I believe you need to use:

    Java Code:
    table.addRowSelectionInterval(row, row);

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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

  1. Eclipse Syntax Highlighting
    By garrettgjb in forum Eclipse
    Replies: 1
    Last Post: 08-24-2009, 03:10 PM
  2. Replies: 1
    Last Post: 04-23-2009, 07:55 PM
  3. How to get multiple rows from Stored Procedure
    By SRJ1957 in forum Advanced Java
    Replies: 0
    Last Post: 08-11-2008, 08:26 PM
  4. To highlight multiple selected values in listbox
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-06-2008, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •