1

I have a jTable that containing some accounts data. I want to disable a button when i click on jTable Row, if the cell text(Data) is "NO" of column number 4 (in code vise column 3). Below is my code, but it does not work.

int selectedRow = tblJournalBatchList.getSelectedRow();
if (tblJournalBatchList.getValueAt(selectedRow, 3) == "NO") {
    btnPost.setEnabled(false);
}

What's wrong with my code?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mahesh
  • 109
  • 1
  • 2
  • 12

1 Answers1

2

use .equals instead ==. you can't use == for this case as String is a object type.it only works for primitives like int,char....you are checking reference quality that's why it's not work. for more information see this How do I compare strings in Java?

int selectedRow = tblJournalBatchList.getSelectedRow();

if ((String)tblJournalBatchList.getValueAt(selectedRow, 3).equals("NO")) {

    btnPost.setEnabled(false);

}
Community
  • 1
  • 1
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 1
    Thanks Dude, It worked. But no need to cast to String value as it's already a string value. am I correct? – Mahesh Nov 07 '14 at 08:05
  • nice don't forget to read the referenced question – Madhawa Priyashantha Nov 07 '14 at 08:11
  • 1
    note required wrapping `if ((String)tblJournalBatchLis....` into `if (selectedRow > -1) {` to trest if any row is selected, btw remove all code and replace with description and link to tutorial about `ListSelectionListener` – mKorbel Nov 07 '14 at 08:13