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

Returning row position in a JTable  RSS feed

 
Frank Manno
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all!
New to these forums, and am glad that I found them... There aren't many Client-Side java forums around.
I'm working with a JTable at the moment... I've decided to go with the DefaultTableModel as my Table Model, as I didn't feel it was necessary to implement my own.
The question I have now is if I want to remove an entire row from the table, what method would I use to return the row number? I've looked at the JTable and DefaultTableModel APIs, and have come up short.
Also, how do I disable the cells from being editable?
Can anyone lend a hand?
TIA!
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frank,
I don't understand your first question

if I want to remove an entire row from the table, what method would I use to return the row number?

Do you want to return the number of the row that you just deleted? But in order to delete the row in the first place, you have to supply that row number -- so why would you want it returned?
As for your second question

how do I disable the cells from being editable?

The only way I know how to do this is to override the "isCellEditable()" method (in "javax.swing.table.DefaultTableModel") -- so you would have to create a subclass of "DefaultTableModel".
Hope this helps.
Good Luck,
Avi.
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Avi, I think that he wants to get the row selected so that he can delete it.
If I remember well, you should write a class that implements ListSelectionListener. The method public void valueChanged(ListSelectionevent lse) from the interface ListSelectionListener will be notified every times that a row is selected. So in this method you can get the row number just like this:

Hope it was helpfull
[ April 07, 2003: Message edited by: Younes Essouabni ]
 
Frank Manno
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Avi Abrami:
Hi Frank,
I don't understand your first question

The only way I know how to do this is to override the "isCellEditable()" method (in "javax.swing.table.DefaultTableModel") -- so you would have to create a subclass of "DefaultTableModel".
Hope this helps.
Good Luck,
Avi.

Hi Avi,
Thanks for getting back to me... The first question was addressed Younes...
As for the isCellEditable... Must I create a subclass of DefaultTableModel, or can I simply override the method name in my class?

Thanks...
 
Frank Manno
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Younes Essouabni:
Hi Avi, I think that he wants to get the row selected so that he can delete it.
If I remember well, you should write a class that implements ListSelectionListener. The method public void valueChanged(ListSelectionevent lse) from the interface ListSelectionListener will be notified every times that a row is selected. So in this method you can get the row number just like this:

Hope it was helpfull
[ April 07, 2003: Message edited by: Younes Essouabni ]


Hi Younes,
That's exactly what I want to do... I added the valueChanged method in my class (after implementing ListSelectionListener).
The problem I'm having is where do I actually code the event. How does the listener know that the event has taken place? I might be going about this all wrong... So any help is appreciated!
Thanks...
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frank,
With regard to your follow-up question

Must I create a subclass of DefaultTableModel, or can I simply override the method name in my class?

As far as I know, the only way to override a method in a class, is via a subclass of that class. So yes, I guess you have to subclass "DefaultTableModel" in order to override the "isCellEditable()" method.
Hope this helps.
Good Luck,
Avi.
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Manno:
Hi Younes,
That's exactly what I want to do... I added the valueChanged method in my class (after implementing ListSelectionListener).
The problem I'm having is where do I actually code the event. How does the listener know that the event has taken place? I might be going about this all wrong... So any help is appreciated!
Thanks...

So if your class implements itself the ListSelectionListener you may do:

I think this should work
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!