• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Simple Selection

 
John Deal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having major problems with what should be a simple thing. I have a JTable and I want the user to be able to select any row (any field in the row, I just care about which row) and fire my selection listener. I use the JTable(Object [][], Object []) constructor and the default model (I will have to do a custom one but I am just working on getting basic selection working now). The table data shows up fine but if I pass the data table via a parameter to the class constructor creating the JTable only the first row is selectable. The other rows show up but are not selectable. If I use the same table and use it directly in the JTable constructor it works fine (but of course the table is static for it to work in this debug mode which is very un-cool for my program design). I have worked/thought about this for a few days. I can't find any posts that relate to this so I assume I must be doing something basicly wrong

Below is the problem code. The commented out data assignment points to the static table mentioned above. If I use the same static table as a parameter only the 1st row is selectable (which is actually the column headers). If I uncomment the assignment all rows are selectable:

JobTable(Object [][] data)
{
// data= debugData;// DEBUG!
// Insert column headers
for (int colIndex = 0; colIndex < NUM_COLUMNS; colIndex++)
{
data[0][colIndex]= COLUMN_NAMES[colIndex];
}

jobTable= new JTable(data, COLUMN_NAMES);
configureTable();
}

Thanks!
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
use your code as you wrote it in your post, but put this at first line in constructor


If the data contains reference to debugData, then your commented assignment should have no influence on what is going on there.
 
John Deal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John. I inserted the code and got the expected results. It does not matter if the data = debugData assignment is commented out or not the message
data == debugData
is displayed. I am at a total loss on this one and it has stopped me cold. I could get around it with major violations of OOP principles. Of course I am only using a static table in the process of debugging this. Here is the slightly revised code:


Again the issue is the assigned listener only gets triggered only on 1st row selection if debugData is passed as a parameter. If the parameter is ignored and use debugData directly all rows are selectable.

Thanks
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

I sent you private message, if you are interested in closer cooperation on your problem. I suggest you remove as much code as possible and then you upload it somewhere, so more people can try exactly the code you have.

By looking at only piece of your code I'm also suspicious, that problem might be somewhere else.
 
John Deal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I solved this issue. The problem was that I was always adding the JTable to the interface (JScrollPane -> JPanel). Somehow the data of the just added table was being displayed but the listener model was of the previous table. When the new data was clicked, the old data replaced it. In any event I removed the components then added the new versions back into their respective containers. Now it seems to work as expected .

Thanks!
 
Erkin Kanlioglu
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry to steal the thread to say something. If you do not want to lose any
point regarding GUI implementation, please consider writing your own TableModel instead of using default one. (Note : Experience is talking.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic