Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Table Selection Lost  RSS feed

 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an interface as shown below. There is a top level frame with a main panel. The main panel has a toolbar, and another panel with a split pane. The left pane contains a scrollpane with a JTable (diagram is showing only one column to keep this simple). The right pane contains tabs with various panels. These tabs are the details for the entries in the table on the left. The Table on the left has a renderer that sets forground and background colors. The render sets a default and changes for isSelected and hasFocus. When I select within this table, the colors are set as expected. My probelm is when I have focus on the panels in the tab pane. The table isSelected rendering is lost. The user then has no association between the table listing and the details in the tabs. How can I keep the selection showing in the table on the left? Thanks in advance.

[ September 23, 2003: Message edited by: Tim Troy ]
[ September 23, 2003: Message edited by: Tim Troy ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the code to your renderer I can only guess. I'd say that when your implementation of TableCellRenderer creates the component, it checks if the cell has focus, and if it does not have focus it returns a default background and foreground. Change it to ignore focus and just do logic on if the cell is selected or not.
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, it "feels" like the cell rendering is not the problem. It seems related to the fact that the table no longer has focus. If you can see anything in the render below, please feel free to point out my error. The colors are ugly just to see the results. There are 3 columns being displayed. The first column is an identifier and the 2nd & 3rd are icons.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I whipped up the following class to test your renderer and it appears to work fine. I had to comment out lines 91-172 of AccIDRenderer because I don't have HistoryFormAccIDListTable, so I can't say for sure. Try it out:

If this class exhibits the behavior you expect (JTable selections stay selected) the next step I'd take would be to put debug statements in the ListSelectionModel the JTable uses to try to figure out who is mucking up the selection:
Change the JTable's selection model:

to this one:
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
Thanks for your efforts. Your code does work as my application should. I then tried your AnotherListSelectionModel class. I can see the stacktrace when selecting in the table. I did not know about the Thread.dumpStack() method (good stuff). When I selected one of my fields in the panel, the setSelectionInterval method does not get called, so no stacktrace. I then extended your idea to include the removeSelectionInterval method. This gave me the clue I needed. My table was being updated each time a row was selected. In this case this is good. The problem was my laziness when I coded my setValueAt method in the table model. I was calling fireTableChanged(new TableModelEvent(this)); instead of fireTableChanged(new TableModelEvent(this, row, col)); The first method call resulted in the table clearing it's selection. Using the more precise version was the way to go. Again, thanks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!