Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Please involves listmodels ((getselectedindex) errors)

    I know this is my first post, but I really need some help.

    I have made a small application which is based on the tabpanel.

    I have 5 tabs (customer, friends, contacts) and two other tabs that just have hardcoded information

    Each tab has a JList and 2 buttton (add , delete)

    The add works without any problems on add three Jlist opening a new a frame to add informaton which hads to the Jlist and a text file.

    Oh yeah each Jlist is being populated by 3 different text files 1 per list.

    I have Methods which add list value to Jlist and text file.

    the delete on the other hand does not working correctly. It will the firsttime, but when I try to delete a second model item from either the same or one of the other list it gives me a "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1" error.

    if I go to the lines of code that look to be causing the issue it is my listCust.getSelectedIndex() line.

    Is there something else I need to add to stop this from happening and if so what can I add.

    I have included my button event I am using. All three button events look the same except except the names for listmodel , get model etc.. change from listcust... to listFriends etc..

    Code:
    private void bntDelCustActionPerformed(java.awt.event.ActionEvent evt) {                                           
            Methods m = new Methods();
            String custWhat = "Customers";
    
            // import model to defaultlistmodel
            DefaultListModel listModelCust = (DefaultListModel)listCust.getModel();
            
            // get index  for method to delete item from textfile
            int LineNumCust = listCust.getSelectedIndex();
    
            //remove selected (i know repeated , but different reasons)
            listModelCust.remove(listCust.getSelectedIndex());
    
            // call method to delete line
            boolean stop = m.DeleteFromFlatFileDB(LineNumCust, custWhat);
    
            if(stop == false)
            {
                // message saying add
                JOptionPane.showMessageDialog(Tab5Cust, "Record Deleted", "Added", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                // message saying error
                JOptionPane.showMessageDialog(Tab5Cust, "Record Not Deleted", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    The m.DeleteFromFlatFileDB only deletes a line from the text files and returns a true or false based on success

    Anyways I have been going crazy trying to figure this out. I am new to java, but have been using c# and Visual Studio for a couple years.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    JLists are always a pain >.<
    Here's a quick test I wrote up that you can compare against. Things to note are the explicit definition of a DefaultListModel instead of allowing it to be built from vector (DefaultListModel is a subinterface of ListModel, though to me they sound like the opposite, so getModel on JList can only be cast to DefaultListModel if its been explicitly declared as such). See if this can help pinpoint the issue:
    PHP Code:
    public class ListTest
    {

        private static 
    JList jl;
        private static 
    DefaultListModel lm;
        
        public static 
    void main(String... argv)
        {
            
    JFrame jf = new JFrame("Test List");
            
            
    lm = new DefaultListModel();
            for (
    int i 010; ++i)
            {
                
    lm.addElement("Test_" i);
            }
                    
            
    jl = new JList(lm);
            
            
    jf.getContentPane().add(jlBorderLayout.CENTER);
            
    JButton del = new JButton("Delete.");
            
    jf.getContentPane().add(delBorderLayout.SOUTH);
                             
            
    del.addActionListener(new ActionListener()
            {
                @
    Override
                
    public void actionPerformed(ActionEvent e)
                {
                    
    int[] idx jl.getSelectedIndices();
                    
    // We'll pull from an extraction of the model instead of from member
                    
    DefaultListModel lm = (DefaultListModel)jl.getModel();
                    
                    for (
    int i idx.length 1>= 0; --i)
                    {
                        
    lm.removeElementAt(idx[i]);
                    }
                }
                
            }
            );
            
            
    jf.pack();
            
    jf.setVisible(true);
            
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

    If that doesn't help, try walking what you have through a debugger to see what the values in listCust and listModelCust include. You may want to consider writing a nested class too so you can just reuse the button listeners and pass in the JList and name you need instead of rewriting the listener.
    Last edited by Fou-Lu; 02-18-2011 at 04:44 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey thank you for the reply.

    I tried changing from selecting from extraction instead of a member and I am getting the same error.

    I have tried stepping through and it always goes to the .getSelectedIndex() parts.

    I am really stumped on this one I am not sure why deleting from a different list would affect the index of another list when deleting.

    Is there something I need to reset ?

    Update:

    I know now that it doesn't have anything to do with adding or removing the items from the files as I commented out that part and still was given an error so it is all jlist ...oh yeah remind me not to use them again ? lol
    Last edited by Evertrue; 02-19-2011 at 02:01 AM. Reason: Updating...

  • #4
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok i just spend a long stepping though the whole application ...

    anyway when I delete one items from one list it works fine. I was wrong when I said it error on getselectedindex. It is error when it gets the model again pulling a max of -1 for some reason the second time around.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I saw unusual behaviour when I was testing out; thats how I determined that the ListModel default wasn't the DefaultListModel. One of my tests were tossing IndexOutOfBounds exceptions myself.
    -1 should only occur in the event of nothing being selected. Mine has the advantage since I select an array of all selected instead of just a single selection. When I iterate it (notice as well that its backwards, I also found that iterating from front to back result in a synchronized shift, so if I selected 1 and 6, I'd lose 1 and 7 from the list).

    Can you post all the code that your using? Also, this behaviour exists when you delete the second time from any list right, including the original one?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Evertrue (02-19-2011)

  • #6
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yes you are correct. It does it on any list including the original the second time around.

    My code is back to the way of the above post. I can put it back to trying the way you have it.

    hmm...

    Here is some more of the code hopefully this helps.

    Bnt Event for Customers
    Code:
    private void bntDelCustActionPerformed(java.awt.event.ActionEvent evt) {                                           
            Methods m = new Methods();
            String custWhat = "Customers";
    
            // import model to defaultlistmodel
            DefaultListModel listModelCust = (DefaultListModel)listCust.getModel();
            
            // get index  for method to delete item from textfile
            int LineNumCust = listCust.getSelectedIndex();
    
            //remove selected (i know repeated , but different reasons)
            listModelCust.remove(listCust.getSelectedIndex());
    
            // call method to delete line
            boolean stop = m.DeleteFromFlatFileDB(LineNumCust, custWhat);
    
            if(stop == false)
            {
                // message saying add
                JOptionPane.showMessageDialog(Tab5Cust, "Record Deleted", "Added", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                // message saying error
                JOptionPane.showMessageDialog(Tab5Cust, "Record Not Deleted", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }

    bnt Event for Friends
    Code:
    private void bntDelFriendActionPerformed(java.awt.event.ActionEvent evt) {                                           
            Methods m = new Methods();
            String custWhat = "Friends";
    
            // import model to defaultlistmodel
            DefaultListModel listModelFriend = (DefaultListModel)listFriend.getModel();
            
            // get index  for method to delete item from textfile
            int LineNumFriend = listFriend.getSelectedIndex();
    
            //remove selected (i know repeated , but different reasons)
            listModelFriend.remove(listFriend.getSelectedIndex());
    
            // call method to delete line
            boolean stop = m.DeleteFromFlatFileDB(LineNumFriend, FriendWhat);
    
            if(stop == false)
            {
                // message saying add
                JOptionPane.showMessageDialog(Tab4Friend, "Record Deleted", "Added", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                // message saying error
                JOptionPane.showMessageDialog(Tab4Friend, "Record Not Deleted", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }

    MAIN
    Code:
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        new Pools_and_Spas().setVisible(true);
                        Methods m = new Methods();
    
                        // load data into list
                        DefaultListModel ModelCust = m.SetCustList();
                        listCust.setModel(ModelCust);
                        
                        DefaultListModel ModelFriend = m.SetFriendList();
                        listFriend.setModel(ModelFriend);
                                                               
                    } catch (IOException ex) // catch IO exception if any
                    {
                       JOptionPane.showMessageDialog(Tab5Cust, ex);
                       System.out.println("Exception occurred!");
                       System.out.println(ex.getMessage());
    
                    }
                    catch (Exception e)  // catch exceptions if any
                    {
                       JOptionPane.showMessageDialog(Tab5Cust, e);
                       System.out.println("Exception occurred!");
                       System.out.println(e.getMessage());
    
                    }
    
                }
            });      
    
        }
    I am not sure what else code to to give... the list are populating from separate textfiles that are setup like:

    FirstName|LastName|Address1|Address2|City|State|Zip|Phone|Email

    I pull the information and separate the information with loops and token and display only the first and last name in the list. I have abutton that shows the rest of the information is an overridden dialog.

  • #7
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok I an update.

    Before I when I said it had nothing to do with the file because I commented it out..

    Anyway I only commented out the file code in one method.

    Today I commented out all file code in both methods and it works fine so I guess it has a problem where it deletes the information from the file...

    Looking at it now ... I think I ma getting close to getting to the bottom of what is going on

  • #8
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I couldn't figure it out so I did this another way.

    Before I was trying to delete an item from the jlist and in the same button event calling method to delete from the text file also.

    Well I started thinking of other ways I could do this because this way was not working for me as the indexes kept messing up after the second delete.

    So anyway what I have done is loaded the list elements from the text files in MAIN so they populate the list.

    While the application is running I can add customers, friends etc.. or delete them without touching the text files at storing or deleting everything from the list models. (listCust, listFriends etc..)

    then I made a window closing event which would transfer the data in the listmodels to .tmp files, delete the original files and then rename the tmp files to the orignal files name.

    Problem solved .

    Thanks for trying help to help...

    Rep added


  •  

    Posting Permissions

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