Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enabling Row Selection in a JTable  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JFrame which contains a scrollable JTable. I need to make the rows of the table selectable. However, all the examples for doing this, extend JPanel. As a relative noob I do not know how to integrate a JPanel into my code or if there is a way to implement this by extending JFrame instead. Can someone point me in the right direction? TIA.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what JPanel versus JFrame has to do with enabling row selection in a JTable. Seems to me you would do the same thing to the JTable in both cases. Did you have some particular problem with that?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't either. I have a working scrollable jtable and I now want to make rows selectable. I can only find examples that use jpanel to accomplish it so I am clueless how to apply that to my code since I don't use a jpanel.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't make any sense. You wouldn't use a JPanel to make rows selectable in a JTable. You would call methods of the JTable, or one of its related objects, to make rows selectable. The fact that the example happens to use a JTable to hold the table is completely irrelevant. So look through the example code for the bit you need. It isn't going to have anything to do with a JPanel.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is my thought as well. However, since I can't seem to find one that seems to make sense to me, perhaps you would be kind enough to post a link of an example that does.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, how would I be able to tell whether an example is going to make sense to you?

But wait a minute. Selection by row is the default for JTable selection models, so why do you think you need to do anything at all?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh.... I guess because when I click on a row nothing happens. I would at least expect it to be highlighted or something.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? Have a look at the Oracle tutorial. Launch the Web Start version of the example, in my browser the selected row does get highlighted.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have but while I have not yet written an event handler, I am surmising to at least get the highlighting working, all I need is this:

The demo does work for me too but I am obviously not understanding something fundamental.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:The demo does work for me too but I am obviously not understanding something fundamental.

Start with the demo code.

Change one thing at a time

When the behavior changes to something you didn't want, read all API for the last change you made, carefully.

That'll help you understand.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a poor tutorial for a beginner. I do not need all the extra code that lets one change the type of selection. I cannot decipher which code belongs to the type of selection and which code is associated with the selection itself. As far as I can tell the only thing I should need is what I posted but it simply does not work unless there must be a handler defined. I took it from your (lack of) reply that to get the rows to highlight, rather than the cells (which is what I get), the handler is not necessary.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:That is a poor tutorial for a beginner.


Well, that's because beginners shouldn't really be writing Swing programs. I don't mean that to be harsh, it's just that Swing programming requires a decent knowledge of object-oriented design. This is a technique which isn't properly taught in most Java courses, and it isn't easy to learn it on your own.

The tutorial is a basic overview of the JTable features. So if you can't understand the tutorial, that should be sending you a message, namely that you don't yet have the Java skills to work with JTables. Again, I don't mean to be insulting, that's just how I see the situation at the moment.

However let's assume you're going to forge ahead regardless. Lack of skills can be remedied, that's why we are posting to this thread. That's why the Ranch is here for that matter. So start by writing a program which simply creates a JTable, adds a row to it, and displays it in a GUI. Nothing more than that.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't take offense and appreciate your reply. However, I have been doing OOP for quite some time (PHP and C++) and in the area of understanding Java's GUI capability (Swing) they are not much help. I don't think it is a lack of OOP experience that is giving me trouble but rather the elements of Swing and their interactions that are the problem. I have tried a basic table and it works. However, as I have said all along, when I try the same thing in my app it simply does not work. I can't simplify the JFrame in my app in a way to add things one at a time to see what breaks it. I've tried going the other way by removing things until it works but if I remove any more nothing will be displayed so there would be no way to tell if it works or not. I have to believe there is something I'm doing that is seemingly unrelated that is causing this problem. Shooting in the dark, perhaps because I am using my own TableModel or TableCellRenderer there is a required override of which I am unaware that bypasses the row selection handler when not present.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:... perhaps because I am using my own TableModel or TableCellRenderer there is a required override of which I am unaware that bypasses the row selection handler when not present.


You didn't realize that your TableCellRenderer is the piece of code which is responsible for highlighting cells which need to be highlighted? (It's responsible for everything about how the cell appears.) The method you've implemented there (whose name I forget right now) has a parameter which tells you whether the cell is selected. So use that parameter and set the cell's foreground and background colours appropriately.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I did not, I thought once the cells were displayed the renderer was done and selection was handled elsewhere. There is nothing in the tutorial that indicates that the selection highlight handling is part of the renderer. My renderer was written to change the background color so that the rows alternate backgrounds for readability. Thanks for putting me on the right track as I can see there is an isSelected argument in the super component.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, from your original post where you apparently couldn't tell how to use a JPanel, I would never have guessed that you had written your own cell renderer.

But you have misunderstood a fundamental design feature of Swing: the data (model) is always separated from the display (view). So when you select a row, that information (that the row is selected) is stored in the model. Specifically, in a class named something like SelectionModel.

But if you want to display selected rows differently, you're talking about the view, not the model. So it's the responsibility of something in the view to do that, not something in the model. Hence the TableCellRenderer.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JPanel issue came up because for whatever reason I kept coming up with bad examples and I doubted myself over the examples. Anyway, I have row selection working now except the behavior is wrong. Once again I am probably coming up with wrong examples but it seems that Windows selection behavior is supposed to be the default. I hope I don't have to handle this in a renderer too because it can easily get complicated. In any case, using shift+click and ctrl+click does not work. I can select single rows or drag the mouse to select a single interval of multiple rows and that's it. In spite of it being the default, using MULTIPLE.SELECTION_INTERVAL does not do it.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:I hope I don't have to handle this in a renderer too because it can easily get complicated.


No. Remember that the renderer is part of the view. Keeping track of what rows are selected is part of the model, in this case the SelectionModel of the JTable.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in reading this thread, I get the impression the main problem is the lack of highlighting of the selected row/s,
originally as a single row selection, but now mutiple row selections via control-click and/or shift-click.

if this is so, create a simple, empty JTable, 10 rows, 3 columns, and set this as the renderer.
this just alternates the row colors between red and yellow, but will highlight selections


 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but that is precisely what I have other than different colors. However, I am not getting the behavior expected. It acts as if the ctrl and shift keys being pressed are ignored. If I highlight a single row, then ctrl+click another row, the original becomes unhighlighted (i.e. isSelected becomes false for that row) and only the new row is highlighted. That is the same behavior as if the ctrl key were not pressed.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try it on a simple empty table?

it worked fine for me
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No but I don't see how that helps. Let's assume it works which I'm sure it does, how does that help be find what is wrong with my real JTable? While I have my own tableModel it only extend the DefaultTableModel to set up my columns. There is no SelectionModel so I thought it should have the default behavior. Perhaps like my renderer, that is not the case.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was always under the impression that single-selection was the default selection mode for a JTable. And that's what you are seeing, which confirms my impression. However let's not assume, let's find out:


 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can add a listener to find out exactly what's being selected and deselected:

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give that a try but I have used 'setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)' to no avail.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help. I found the problem, mostly by mistake. It seems my build path had the JRE library set to version 1.6. When I changed it to 1.7, everything began working.
 
Veena Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, from your original post where you apparently couldn't tell how to use a JPanel, I would never have guessed that you had written your own cell renderer.

But you have misunderstood a fundamental design feature of Swing: the data (model) is always separated from the display (view). So when you select a row, that information (that the row is selected) is stored in the model. Specifically, in a class named something like SelectionModel.

But if you want to display selected rows differently, you're talking about the view, not the model. So it's the responsibility of something in the view to do that, not something in the model. Hence the TableCellRenderer.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:Thanks for all the help. I found the problem, mostly by mistake. It seems my build path had the JRE library set to version 1.6. When I changed it to 1.7, everything began working.


D'oh!

Nobody would ever have thought to ask you anything like that. Good thing you stumbled across the real problem.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am laughing uncontrollably reading this thread. I'm really surprised to see that even JRE version can cause this issue.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!