• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getValueAt in JTable

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I have a JTable that I have the ability to scroll up and down through the rows using the up and down arrow keys. I also have the information from the rows going into some textFields. My problem is, that I am doing a getValueAt on a keyPressed event and when the info is displayed into the text fields, it is always one row off. I want the row that is highlighted to display in the fields. Not the row that the keyPressed event was on. Does this make sense?
Thanks for any help!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of KeyPress use the ListSelectionListener like in the following code.
CODE] JTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
tableRowHasChanged();
}
}); [/CODE]
where tableRowHasChanged is a method to display your JTextFields.
Mark
[ May 20, 2002: Message edited by: Mark Spritzler ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Mark. Using ListSelectionListener worked. However, it created another problem for me, which is one I don't understand. I'm a newbie at using ListSelectionListener so I was wondering if you could help me figure out where the problem lies.
In my application, there is a JComboBox that the user picks a customer. Each time there is an actionEvent on the customer JComboBox, a method is run that populates a table below with the corresponding information for that customer.
Since I am now using ListSelectionListener, I can click on a row on a table, or scroll up and down through it using arrow keys and the info is put into my text fields just wonderfully. However, when I change the customer in the JComboBox, I keep getting an ArrayIndexOutOfBoundsException -1 < 0.
This only happens if I have selected a row in the table. If I don't do anything in the table, I can choose different customers left and right without any grief.
Could you please help me out? I guess I don't understand what it's trying to tell me. If you need me to post code, let me know.
Thanks a million!!
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well basically you have a row selected, so the row number is used, when you select from the JComboBox, the results that come from that don't have the same amount of records, and the past selected is higher than the max number of records of the new customer.
Let me look for some code to fix that for you.
Actually I don't have any code like that.
I think somewhere in your code you are using the selected RowNumber, and then when the JCombobox changes the TableModel, it also tries to run code with that RuwNumber you had previously, which is higher than the actual amount of rows in the JTable now.
Does that make sense?
Mark
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
I don't know if this helps you any, but here is some of my code.

Thanks again for your help.
I really appreciate it.
[ May 21, 2002: Message edited by: Jennifer Sohl ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok try this.
You have the following code in the getSelectedValue


The first line says
i = table.getSelectedRow();
If you are refreshing the JTable with new data from the selection made in the JCombobox, then the i might end up equaling -1 which would then make the code after that line fail with Out of Subscript range errors.
So add an if (i > -1) or should it be i > 0. one of these two, that way it won't try to run the code that assigns the String variables.
Like the line: nameField = (String)rm.getValueAt(i,2);
if i = -1 then you will get an error here, but if it is in an if statement and i = -1 then the code will never run.
Mark
[ May 21, 2002: Message edited by: Mark Spritzler ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
Guess What?? It worked! Don't I feel stupid! That was so simple, I can't believe I missed it.
Once again, thanks so much for your help.
It is VERY MUCH appreciated!!
Have a great day!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic