• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

JTable get data from colums when row is selected

 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone point me to a good tutorial or example that will help me understand how to get the data from a JTable when the row has focus or is selected.

In reading it seems that I will need some type of listener but most of what I read is if the data in the table is changed. My table can not be changed but when it gets
focus i need to get the data.

Thanks
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum.

Are you familiar with the Java™ Tutorials?
 
Roger Wells
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am aware of them but for listeners they seems to be based on the data being changed not just a focus event.

Unless I am missing something which is a good chance.

 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected. You can add a ListSelectionListener to that via the addL..S..L..() method and override the methods which tell you when the row selection is changing, just like any other listener.
 
Roger Wells
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected. You can add a ListSelectionListener to that via the addL..S..L..() method and override the methods which tell you when the row selection is changing, just like any other listener.



I guess I need more help I have read through suggestions but no success. Hers is where I am. I am trying to listen for a row to get focus in the tblTemplate. You can see some of the items I have tried but I can't get it to work.

 
Rancher
Posts: 3184
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on How to Write a ListSelectionListener for working examples.

 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Rob just said: pretty much any time you have a question about Swing, there's an Oracle tutorial which goes into that thing.
 
Roger Wells
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Read the section from the Swing tutorial on How to Write a ListSelectionListener for working examples.



Rob I appreciate you help. Sorry but I am still having problems. I have read the TableListSelectionDemo.java several times.

So here is where I am starting one line of code at a time.
I get the following error:

<identifier> expected
incompatible types: ListSelectionModel cannot be converted to listSelectionModel
variable tblTemplate might not have been initialized


 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roger Wells wrote:incompatible types: ListSelectionModel cannot be converted to listSelectionModel



I don't understand what you're trying to do there. Presumably you've declared that variable "listSelectionModel" elsewhere in your code and it isn't of type "ListSelectionModel". It's extremely unhelpful for you to not post that declaration.

So let's go back a step. Show us the code you wrote which gets the ListSelectionModel from your JTable.

Edit: Sorry, looks like that's supposed to be the code I just asked for. So why doesn't it declare that variable right there?



Another edit: And why is inside an anonymous inner class extending JTable?
 
Rob Camick
Rancher
Posts: 3184
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So here is where I am starting one line of code at a time.



I have no idea why you are attempting to add that line of code there. Where in the tutorial do they extend the JTable to add that line of code?

You are going about things backwards.

Download the working demo code from the tutorial and test the demo code.

Then use the demo code as your starting point and change one line of code at a time to implement your requirement.

That is learn by example and customize the example.
 
Roger Wells
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob

After much hair pulling I finally got this to work. I found another tutorial that helped me understand.

I have a question still. This table has 2 rows and several columns. When I click on a column I would like for it to
select all of the rows in that column so that I can pull data from all of the rows. Currently this only pulls data from the first column.

Can you offer some suggestion on how to fix this?

Here is my current code:

 
Rob Camick
Rancher
Posts: 3184
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I found another tutorial that helped me understand.


Not sure what you didn't understand about the working code presented in the tutorial.

It showed you how to identify the selected row when a row is clicked. Then you just use getValueAt(...) to get the data.

If there something you don't understand, then you ask a follow up question citing the text from the tutorial or the code from the demo that you don't understand.

I would like for it to select all of the rows in that column



Not really sure what you are asking. Your original question was about selecting a row. Now it seems to have changed to be about selecting a column.

Not sure why you have two for loops. If you click on a column then you should get an event.

Then you just loop through all the rows getting the value for the row and selected column. There is no need to know the selected rows, since you said you want the data from all the rows.

Also, if you simply assign the value for each row to a String, you will replace the data from the first row, with the data from the second row.

Post a proper SSCCE demonstrating the problem.

If you want both values then you would need to use a StringBuilder and append the data for each row. Or use an ArrayList to get the value for each row.
 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roger Wells wrote:When I click on a column I would like for it to select all of the rows in that column so that I can pull data from all of the rows.



I don't understand the point of that. Once you have the posted code running because a selection event occurred, you have access to the whole table and you can pull data from whatever rows you want. You don't have to restrict yourself to selected rows only.
 
Roger Wells
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul and Rob

I'm sorry about the confusion with rows and columns. I also get confused when starting with 0 instead of 1.

I stay confused so I am sure I will be back with more confusing questions. Hope you are patient with me!

Here is the code that worked for me.

 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's better. There's a gotcha with list selection listeners, namely that people don't realize at first that they report on deselection events as well as selection events. So for example if you had selected row 1 and then you selected row 2, the listener is going to get a notification that row 1 was deselected and then another notification that row 2 was selected. In your case you won't notice because the action taken in your listener code is idempotent (except for the debugging output).
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic