Results 1 to 11 of 11
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Unhappy ArrayoutOfBounds on JTable

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class Trypack {
        public Trypack(){
         
            
           final DefaultTableModel Mdl1 = new DefaultTableModel();
                table = new JTable(Mdl1)
                    {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
           Mdl1.addColumn("Student no.");
           Mdl1.addColumn("Name");
           Mdl1.addColumn("Course");
           Mdl1.addColumn("Date of Birth");
           Mdl1.addColumn("Math Gtade");
           Mdl1.addColumn("English Gtade");
           Mdl1.addColumn("Science Gtade");
           Mdl1.addColumn("Makabayan Grade");
            
            scrollPane = new JScrollPane(table);
            final JFrame frame1 = new JFrame();
            final JFrame frame2 = new JFrame();
            final JFrame frame3 = new JFrame();
            final JFrame frame4 = new JFrame();
            final JFrame frame5 = new JFrame();
            final JFrame frame6 = new JFrame();
            final  JPanel Pnl1 = new JPanel();
            final  JPanel Pnl2 = new JPanel();
            final  JPanel Pnl3 = new JPanel();
            final  JPanel Pnl4 = new JPanel();
            final  JPanel Pnl5 = new JPanel();
            final  JPanel Pnl6 = new JPanel();
            final JMenuItem Item1 = new JMenuItem("Logout");
             JMenuBar menuBar = new JMenuBar();
              JMenu menu1 = new JMenu("File");
              JMenu menu2 = new JMenu("Help");
              final JComboBox cmbx = new JComboBox(new String[]{"-select-","BSIT","BSCS","COMVUL","BSAA"});
              
              btnLogin = new JButton("Login");
              btnClear = new JButton("Clear");
              btnViewStud = new JButton("View Students");
              btnAddStud = new JButton("Add Students");
              btnSave = new JButton("Save"); 
              btnClear1 = new JButton("Clear");
              btnSave1 = new JButton("Save");
              btnClear2 = new JButton("Clear");
              btnBack = new JButton("Back");
              btnViewGrad = new JButton("View Grades");
              btnAddNewStud = new JButton("Add new Student");
              btnClose = new JButton("Close");
              
              lblUser = new JLabel("Username: ");
              lblPass = new JLabel("Password: ");
              lblStudNum = new JLabel("Student #: ");
              lblLastName = new JLabel("Lastname: ");
              lblFirstName = new JLabel("FirstName: ");
              lblCourse = new JLabel("Course: ");
              lblMidName = new JLabel("Middle Name: ");
              lblMath = new JLabel("Math Grade: ");
              lblScience = new JLabel("Science Grade: ");
              lblEnglish = new JLabel("English Grade: ");
              lblMakabayan = new JLabel("Makabayan Grade: ");
              lblBirth = new JLabel("Date of Birth: ");
              
              txtLogin = new JTextField();        
              pass2 = new JPasswordField();
              pass2.setColumns(10);
              txtStudNum = new JTextField();
              txtLast = new JTextField();
              txtFirst = new JTextField();
              txtMid = new JTextField();
              txtMath = new JTextField();
              txtScience = new JTextField();
              txtEnglish = new JTextField();
              txtMakabayan = new JTextField();
              txtBirth = new JTextField();
              
              menuBar.add(menu1);
              menuBar.add(menu2);
              menu1.add(Item1); 
              
            frame1.add(lblUser);
            frame1.add(txtLogin);
            frame1.add(lblPass);
            frame1.add(pass2);
            frame1.add(btnLogin);
            frame1.add(btnClear);
             
            Pnl1.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl1.setLayout(new BorderLayout());
            Pnl1.setVisible(true);
            
            Pnl2.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl2.setLayout(new GridLayout());
            Pnl2.setVisible(true);
          
            Pnl3.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl3.setLayout(new GridLayout(1,3));
            Pnl3.setVisible(true);
            
           
            Pnl4.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl4.setLayout(new GridLayout(3,1));
            Pnl4.setVisible(true);
            
            Pnl5.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl5.setLayout(new GridLayout(4,1));
            Pnl5.setVisible(true);
            
            Pnl6.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl6.setLayout(new GridLayout(1,2));
            Pnl6.setVisible(true);
            
            frame2.setVisible(false);
            frame2.setSize(350,150);
            frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame2.setLayout(new GridLayout(3,1));
            frame2.setLocationRelativeTo(null);
                      
                    Pnl1.add(scrollPane,BorderLayout.CENTER);
                    Pnl2.add(table);
                    Pnl3.add(btnViewGrad);
                    Pnl3.add(btnAddNewStud);
                    Pnl3.add(btnBack);
                
            frame3.setVisible(false);
            frame3.setSize(950, 350);
            frame3.setTitle("Manage Your Students");
            frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame3.setLayout(new GridLayout(2,1));
            frame3.setLocationRelativeTo(null);
            frame3.getContentPane().add(new JScrollPane(table));
            frame3.getContentPane().add(Pnl3);
            
            frame4.setVisible(false);
            frame4.setSize(350,300);
            frame4.setTitle("Adding Students");
            frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame4.setLayout(new GridLayout(7,2));
            frame4.setLocationRelativeTo(null);     
            
            frame5.setVisible(false);
            frame5.setSize(320,320);
            frame5.setTitle("Viewing Grades");
            frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame5.setLayout(new GridLayout(8,1));
            frame5.setLocationRelativeTo(null);
            
            
            frame6.setVisible(false);
            frame6.setSize(300,400);
            frame6.setTitle("Adding Grades");
            frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame6.setLayout(new GridLayout(3,2));
            frame6.setLocationRelativeTo(null);
            frame6.getContentPane().add(Pnl4);
            frame6.getContentPane().add(Pnl5);
            frame6.getContentPane().add(Pnl6);
            
            frame1.setVisible(true);
            frame1.setSize(350,150);
            frame1.setLayout(new GridLayout(3,2));
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setLocationRelativeTo(null);
            frame1.setTitle("Login Your Account");
                   
            frame2.add(menuBar);
            frame2.add(btnViewStud);
            frame2.add(btnAddStud);
            
            frame4.add(lblStudNum);
            frame4.add(txtStudNum);
            frame4.add(lblLastName);
            frame4.add(txtLast);
            frame4.add(lblFirstName);
            frame4.add(txtFirst);
            frame4.add(lblMidName);
            frame4.add(txtMid);
            frame4.add(lblBirth);
            frame4.add(txtBirth);
            frame4.add(lblCourse);
            frame4.add(cmbx);
            frame4.add(btnSave);
            frame4.add(btnClear1);
            
            Pnl5.add(lblMath);
            Pnl5.add(txtMath);
            Pnl5.add(lblScience);
            Pnl5.add(txtScience);
            Pnl5.add(lblEnglish);
            Pnl5.add(txtEnglish);
            Pnl5.add(lblMakabayan);
            Pnl5.add(txtMakabayan);
            Pnl6.add(btnSave1);
            Pnl6.add(btnClear2);
            
           Item1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act1) {
                    int lo = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Logout?", "Logout", JOptionPane.YES_NO_OPTION);
                    if(lo == JOptionPane.YES_OPTION)
                    {
                        txtLogin.setText(null);
                        pass2.setText(null);
                        frame1.setVisible(true);
                    }
                }
            });
            
            btnLogin.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if(txtLogin.getText().isEmpty() || pass2.getText().isEmpty())
                         {
                        JOptionPane.showMessageDialog(null, "Please complete all required fields.", "Input Required", 
                                + JOptionPane.ERROR_MESSAGE);
                        txtLogin.setText(null);
                        pass2.setText(null);
                         }
                    else
                    {
                        frame2.setVisible(true);
                        frame2.setTitle("Login as: "+ txtLogin.getText());
                        frame1.setVisible(false);
                    }
                }
            });
            
            btnClear.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtLogin.setText(null);
                    pass2.setText(null);
                }
            });
          btnViewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act2) {
                        frame2.setVisible(false);
                         frame3.setVisible(true);
                        btnViewGrad.setEnabled(false);
                          
                }
            });
            
            btnAddStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act3) {
                    cmbx.setSelectedIndex(0);
                        frame4.setVisible(true);
                        frame2.setVisible(false);
                    
                }
            });
            
            btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     if(cmbx.getSelectedIndex()==0 || txtFirst.getText().isEmpty() || txtLast.getText().isEmpty() 
                         || txtMid.getText().isEmpty()  || txtBirth.getText().isEmpty() || txtStudNum.getText().isEmpty())
                         {
                        JOptionPane.showMessageDialog(null, "Please complete all required fields before saving.", "Input Required", 
                                + JOptionPane.ERROR_MESSAGE);
                         }
                     else
                     {
                         
                       btnViewGrad.setEnabled(true);
                        frame4.setVisible(false);
                        frame6.setVisible(true);
                        
                    lblStudNum1 = new JLabel("Student #:        \t" +txtStudNum.getText());
                    lblName = new JLabel("Name:        \t"+txtLast.getText()+ " " + txtFirst.getText()+ ", " + txtMid.getText());
                    lblCourse1 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(lblName);
                   Pnl4.add(lblCourse1);
                   Pnl4.add(lblStudNum1);
                   Pnl4.revalidate();
                   Pnl4.repaint();
                   
                     }
                }
            });
            
            btnClear1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    clearFields();
                }
            });
            
            btnSave1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     frame6.setVisible(false);
                     frame5.setVisible(true);
                     if(txtMath.getText().isEmpty() || txtScience.getText().isEmpty() 
                         || txtEnglish.getText().isEmpty()  || txtMakabayan.getText().isEmpty())
                         {
                        JOptionPane.showMessageDialog(null, "Please complete all required fields before saving.", "Input Required", 
                                + JOptionPane.ERROR_MESSAGE);
                         }
                     else
                     {
                         StudentRecord sRecord = new StudentRecord();
                        sRecord.setCourse(cmbx.getSelectedItem().toString());
                        sRecord.setDateOfBirth(txtBirth.getText());
                        sRecord.setName(txtLast.getText(), txtFirst.getText(), txtMid.getText());
                        sRecord.setStudentNo(txtStudNum.getText());
                         String[] showyboi = {sRecord.getStudentNo(),sRecord.getName(),sRecord.getCourse(),sRecord.getDateOfBirth(),
                            txtMath.getText(),txtScience.getText(),txtEnglish.getText(),txtMakabayan.getText()};
                        Mdl1.addRow(showyboi);
                     lblStudNum2 = new JLabel("Student #:       \t"+showyboi[0]);
                     lblName1 = new JLabel("Name:        \t"+showyboi[1]);
                     lblCourse2 = new JLabel("Course:        \t"+showyboi[2]);
                     lblMath2 = new JLabel("Math:        \t"+showyboi[4]);
                     lblScience2 = new JLabel("Science:        \t"+showyboi[5]);
                     lblEnglish2 = new JLabel("English:         \t"+showyboi[6]);
                     lblMakabayan2 = new JLabel("Makabayan:      \t"+showyboi[7]);
                        
                     }
                     frame5.add(lblStudNum2);
                     frame5.add(lblName1);
                     frame5.add(lblCourse2);
                     frame5.add(lblMath2);
                     frame5.add(lblScience2);
                     frame5.add(lblEnglish2);
                     frame5.add(lblMakabayan2);   
                     frame5.add(btnClose);
                     frame5.revalidate();
                     frame5.repaint();
                }
            });
            
            
            
            btnClear2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtMath.setText(null);
                    txtScience.setText(null);
                    txtEnglish.setText(null);
                    txtMakabayan.setText(null);
                }
            });
            
            btnBack.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame3.setVisible(false);
                    frame2.setVisible(true);
                }
            });
            
            btnViewGrad.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(false);
                    table.getCellSelectionEnabled();
                    table.getRowSelectionAllowed();
                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    int row = table.getSelectedRow();
                  String str = (String) table.getModel().getValueAt(row, 0);
                  JOptionPane.showMessageDialog(null,str);
                   
                    
                }
            });
            
            btnAddNewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                   cmbx.setSelectedIndex(0);
                   frame3.setVisible(false);
                   frame4.setVisible(true);
                   Pnl4.removeAll();
                   frame5.getContentPane().removeAll();
                   Btn11clearFields();
                   
                }
            });
            
            btnClose.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(false);
                    frame3.setVisible(true);                
                }
            }); 
        }
        
         private void clearFields()
        {
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtBirth.setText(null);
        }
         
         private void Btn11clearFields(){
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtMath.setText(null);
            txtScience.setText(null);
            txtEnglish.setText(null);
            txtMakabayan.setText(null);
         }
        
        public static void main(String[]args){
        new Trypack();
            }
        private JButton btnLogin,btnClear,btnViewStud,btnAddStud,btnSave,btnClear1,btnSave1,btnClear2,btnBack,btnViewGrad,btnAddNewStud,btnClose;
        private JLabel lblUser,lblPass,lblStudNum,lblLastName,lblFirstName,lblCourse,lblMidName,lblMath,lblScience,lblEnglish,lblMakabayan,lblBirth,lblName;
        private JLabel lblStudNum1,lblStudNum2,lblCourse1,lblCourse2,lblMath2,lblScience2,lblEnglish2,lblMakabayan2,lblName1;
        private JPasswordField pass2;
        private JTextField txtLogin,txtStudNum,txtLast,txtFirst,txtMid,txtMath,txtScience,txtEnglish,txtMakabayan,txtBirth;
        private JTable table;
        private JScrollPane scrollPane;
        }
    My code on the The class StudentRecord
    Java Code:
    public class StudentRecord
    {
        private String StudentNo;
        private String LastName;
        private String FirstName;
        private String MiddleName;
        private String DateOfBirth;
        private String Course;
        
        public void setStudentNo(String StudentNo)
        {
            this.StudentNo = StudentNo;
        }
        
        public void setLastName(String LastName)
        {
            this.LastName = LastName;
        }
        
        public void setFirstName(String FirstName)
        {
            this.FirstName = FirstName;
        }
        
        public void setMiddleName(String MiddleName)
        {
            this.MiddleName = MiddleName;
        }
        
        public void setName(String LastName, String FirstName, String MiddleName)
        {
            setLastName(LastName);
            setMiddleName(MiddleName);
            setFirstName(FirstName);
        }
        
        public void setDateOfBirth(String DateOfBirth)
        {
            this.DateOfBirth = DateOfBirth;
        }
        
        public void setCourse(String Course)
        {
            this.Course = Course;
        }
        
        public String getStudentNo()
        {
            return StudentNo;
        }
        
        public String getLastName()
        {
            return LastName;
        }
        
        public String getFirstName()
        {
            return FirstName;
        }
        
        public String getMiddleName()
        {
            return MiddleName;
        }
        
        public String getName()
        {
            return getLastName()+", "+getFirstName()+" "+getMiddleName();
        }
        
        public String getDateOfBirth()
        {
            return DateOfBirth;
        }
        
        public String getCourse()
        {
            return Course;
        }
        
        @Override
        public String toString()
        {
            String sRecord = 
                    "[Record]"
                    + "\nStudent No.:   " + getStudentNo()
                    + "\nFull Name:     " + getName()
                    + "\nDate of Birth: " + getDateOfBirth()
                    + "\nCourse:        " + getCourse();
            return sRecord;
        }
    }
    Im getting this error everytime i click the ViewGrad Button
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    	at java.util.Vector.elementData(Vector.java:730)
    	at java.util.Vector.elementAt(Vector.java:473)
    	at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
    the output that i wanted is to select a row and when i clicked the viewGrad Button it will display the value of the selectedrow
    i use this code to get the value of the row
    Java Code:
    int row = table.getSelectedRow();
                  String str = (String) table.getModel().getValueAt(row, 0);
                  JOptionPane.showMessageDialog(null,str);
    But im getting the error above.

    Please help me on how to display the value of the selectedrow....

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: ArrayoutOfBounds on JTable

    You don't need 6 JFrames and 500 lines of code to demonstrate a problem with a JTable. Heck, a Swing application normally wouldn't use more than one JFrame; data entry forms and alerts would be implemented as modal JDialogs.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. To the point, and without the clutter of irrelevant stuff.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: ArrayoutOfBounds on JTable

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: ArrayoutOfBounds on JTable

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class Crap{
        public Crap(){
             JFrame frame1 = new JFrame();
           final DefaultTableModel Mdl1 = new DefaultTableModel();
                table = new JTable(Mdl1)
                    {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
           Mdl1.addColumn("Student no.");
           scrollPane = new JScrollPane(table);
           btnView = new JButton("View");
           frame1.setSize(200,150);
           frame1.setLocationRelativeTo(null);
           frame1.setVisible(true);
           frame1.setLayout(new GridLayout(2,1));
           txtIn = new JTextField();
           frame1.add(btnView);
           frame1.add(txtIn);
           
           btnView.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                   table.getCellSelectionEnabled();
                    table.getRowSelectionAllowed();
                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    int i;
                    int row = table.getSelectedRow();
                  Mdl1.addRow(new String[]{txtIn.getText()});
                  i = Integer.parseInt(table.getValueAt(row,0).toString());
                 // String str = (String) table.getModel().getValueAt(row, 0).toString();
                  JOptionPane.showMessageDialog(null,i);
                }
            });
           
        }
        public static void main(String[]args){
                new Crap();
            }
       private JTable table;
       private JScrollPane scrollPane;
       private JButton btnView;
       private JTextField txtIn;
    }
    and this is the error
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    	at java.util.Vector.elementData(Vector.java:730)
    	at java.util.Vector.elementAt(Vector.java:473)
    	at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
    	at javax.swing.JTable.getValueAt(JTable.java:2720)
    	at Core.Crap$2.actionPerformed(Crap.java:40)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    the output that i wanted is to get the txtIn input and display it but i get the error above everytime i click the btnView.
    i can't edit my main post because the edit button is disappearing..

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayoutOfBounds on JTable

    Use the "poor man's debugger" to check the state of your program before the error. Since the error occurs here:

    i = Integer.parseInt(table.getValueAt(row, 0).toString());

    Put a println statement before this line to see what's going on:


    Java Code:
             public void actionPerformed(ActionEvent e) {
                table.getCellSelectionEnabled();
                table.getRowSelectionAllowed();
                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                int i;
                int row = table.getSelectedRow();
                System.out.println("selected row is: " + row); // ***** add this *****
                Mdl1.addRow(new String[] { txtIn.getText() });
                i = Integer.parseInt(table.getValueAt(row, 0).toString());
                JOptionPane.showMessageDialog(null, i);
             }
    And what do you see?

  6. #6
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: ArrayoutOfBounds on JTable

    it says the selected row is -1 but why it is -1??

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayoutOfBounds on JTable

    Quote Originally Posted by raffs03 View Post
    it says the selected row is -1 but why it is -1??
    The JTable API will tell you what -1 means.

    But also consider:
    Where in the code above is a row of the JTable selected? How is it possible for the user to do this if the JTable is not available for the user to interact with it?

  8. #8
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: ArrayoutOfBounds on JTable

    yeah sorry i forgot to show the table but here it is
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class Crap{
        public Crap(){
             JFrame frame1 = new JFrame();
            final JFrame frame2 = new JFrame();
           final DefaultTableModel Mdl1 = new DefaultTableModel();
                table = new JTable(Mdl1)
                    {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
           Mdl1.addColumn("Student no.");
           scrollPane = new JScrollPane(table);
           btnView = new JButton("View");
           btnAdd = new JButton("Add");
           frame1.setSize(200,150);
           frame1.setLocationRelativeTo(null);
           frame1.setVisible(true);
           frame1.setLayout(new GridLayout(2,1));
           frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame2.setVisible(false);
           frame2.setLayout(new GridLayout(2,1));
           frame2.setLocationRelativeTo(null);
           frame2.setSize(200,150);
           frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           txtIn = new JTextField();
           frame1.add(btnAdd);
           frame1.add(txtIn);
           frame2.getContentPane().add(new JScrollPane(table));
           frame2.add(btnView);
           
           btnAdd.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame2.setVisible(true);
                      Mdl1.addRow(new String[]{txtIn.getText()});
                  
                }
            });
           
           btnView.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    table.getCellSelectionEnabled();
                    table.getRowSelectionAllowed();
                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    int i;
                    int row = table.getSelectedRow();
                  System.out.println("selected row is: " + row);
                  i = Integer.parseInt(table.getValueAt(row,0).toString());
                 // String str = (String) table.getModel().getValueAt(row, 0).toString();
                  JOptionPane.showMessageDialog(null,i);
                }
            });
           
        }
        public static void main(String[]args){
                new Crap();
            }
       private JTable table;
       private JScrollPane scrollPane;
       private JButton btnAdd,btnView;
       private JTextField txtIn;
    }
    When i changed the selected row to the selectedrowCount the system println tells me that the row is 0 and it display the output, but when i put another input println says the same and the output is the same but i selected the 2nd row(by clicking it.) any suggestions?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayoutOfBounds on JTable

    It took a while for me to figure out what is wrong, but after playing quite a bit with your SSCCE (and thanks for posting that!) I finally now see it. Your problem is here:

    Java Code:
                public void actionPerformed(ActionEvent e) {
                    table.getCellSelectionEnabled(); // this is a getter method and does *nothing* useful here
                    table.getRowSelectionAllowed(); // this is a getter method and does *nothing* useful here
    
                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // ******** this is messing you royally! ******
    
                    int i;
                    int row = table.getSelectedRow();
                  System.out.println("selected row is: " + row);
                  i = Integer.parseInt(table.getValueAt(row,0).toString());
                 // String str = (String) table.getModel().getValueAt(row, 0).toString();
                  JOptionPane.showMessageDialog(null,i);
                }
            });
    The problem is that you're changing the selection mode *after* the selection has been made but before you extract the selection. This resets the selection to its non-selected state. The solution is to do all these setting when you create the JTable, not when you try to get its selection.

  10. #10
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: ArrayoutOfBounds on JTable

    hey Sorry For the late reply D but you saved my day!! im now done with my project and ready present it to my professor!! i'm really glad to this forum, long live!
    by the way problem solved.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 08:57 PM
  2. Replies: 8
    Last Post: 11-13-2011, 10:51 PM
  3. ArrayOutofBounds Exception
    By aianta in forum New To Java
    Replies: 5
    Last Post: 08-12-2011, 08:14 PM
  4. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 09:27 AM
  5. Replies: 2
    Last Post: 05-15-2008, 11:15 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
  •