Results 1 to 2 of 2

Thread: Invalid Index

  1. #1
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default Invalid Index

    I dont know whats going on when Im deleting a row in this table and displaying the value in the first column of the deleted row, im trying to display the names of the deleted rows, but why am i getting an Invalid index?
    Java Code:
    public class DeletingRows extends JFrame {
    
        private JTextField searchField;
        private JTable table;
        private JPanel panel;
        private JScrollPane scroll;
        private JButton deleteRowButton;
        private DefaultTableModel model;
    
        public DeletingRows() {
    
            panel = new JPanel();
    
            searchField = new JTextField();
            deleteRowButton = new JButton("Delete Row");
    
            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"}};
    
            model = new DefaultTableModel(data, columnNames);
    
            table = new JTable(model);
            table.setRowSelectionAllowed(true);
    
            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) {
    
                    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);
                            }
                        }
                    }
                }
            });
    
            deleteRowButton.setBounds(170, 50, 150, 20);
            deleteRowButton.addActionListener(new DeleteActionListener());
    
            panel.add(searchField);
            panel.add(deleteRowButton);
            panel.add(scroll);
    
            getContentPane().add(panel);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(907, 700);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        private class DeleteActionListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
    
                if (deleteRowButton.equals(e.getSource())) {
    
                    int numRows = table.getSelectedRows().length;
                    int[] listOfDeletedRows = new int[numRows];
    
                   
                    for (int i = numRows - 1; i >= 0; i--) {
    
                        model.removeRow(table.getSelectedRows()[i]); 
    
                    for (int x = 0; x <= listOfDeletedRows.length - 1; x++) {
                         
                        System.out.println(listOfDeletedRows[x]);
    
                        String firstColumnValue = table.getValueAt(listOfDeletedRows[x] - 1, 0).toString();
    
                        System.out.println(firstColumnValue);
                    }
                }
            }
        }
    
        public static void main(String[] args) {
    
            new DeletingRows();
        }
    }
    If remove the "-1" it displays the name of the second row. but if i add "-1" it throws me an invalid index. im getting confused here, need help guys:confused:
    Java Code:
    table.getValueAt(listOfDeletedRows[x] - 1, 0).toString();
    Last edited by Allgorythm; 02-16-2010 at 01:40 PM.

  2. #2
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default

    solved. I didnt notice that when you delete the index before getting its value will be a big mistake. how can i retrieve the value on the deleted index.? what i did is getting the value first before deleting it :o

Similar Threads

  1. invalid path
    By arshesander in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:36 AM
  2. HttpClient invalid headers
    By Nicole in forum Advanced Java
    Replies: 6
    Last Post: 03-31-2009, 01:36 AM
  3. Invalid Thread Access?
    By xcallmejudasx in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2008, 11:08 PM
  4. Invalid Argument error in IE7
    By Kaviurs in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-01-2008, 10:12 AM
  5. Replies: 1
    Last Post: 08-06-2007, 10:24 PM

Posting Permissions

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