This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Strings In a JTable  RSS feed

 
Pete Calinski
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem finding string objects in a jTable.

I use setValueAt() to write string variables into a jTable.

I can then use getValueAt() to search to find any of those strings.

All works fine until I edit an entry in the table. Then I can never find that string again.

In fact, if I just click in the cell type a character, then delete that character, something changes. The contents of the cell look the same but the search for it doesn't find it.

For example if I have a string "xyz" that was written in a cell. If I set the search value, "a=xyz", and "b=getValueAt(some cell)".

Then at the compare:

if(a==b) I can hover over a and b and I see that;

a = (java.lang.String) xyz

and

b = (java.lang.String) xyz

[note I am using NetBeans to do this]

after the compare, the true path is taken .

If I then click in the cell then click out and try the same compare, everything is exactly the same including what gets reported when I hover. But the false path is taken at the "if".

I searched the stuff about renderers but I don't think that is my problem.

Any ideas?

Thanks in Advance.

Pete
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(a==b) I can hover over a and b and I see that;


Don't use "==" to compare objects.

Instead use the the equals(...) method.



This has nothing to do with a JTable. It is for any Object you use anywhere in Java.
 
Pete Calinski
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

It works now. For others I found:

""==" compares reference and not the content."

Pete
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!