jtable's getvalueat...

Discussion in 'Java' started by tiewknvc9, Jan 3, 2006.

  1. tiewknvc9

    tiewknvc9 Guest

    Hi!

    I have a jtable object, and I am trying to test the object to see if it
    has been left blank (i.e. "" or null).

    So I tried

    getValueAt(iRow, iCol) == "" and getValueAt(iRow, iCol) == null

    and neither seem to work, but both do compile correctly.

    So my question is.... How do I test a jtable's cell for a blank entry?

    Many Thanks.
     
    tiewknvc9, Jan 3, 2006
    #1
    1. Advertising

  2. tiewknvc9

    zero Guest

    "tiewknvc9" <> wrote in news:1136310568.610455.47510
    @g49g2000cwa.googlegroups.com:

    > Hi!
    >
    > I have a jtable object, and I am trying to test the object to see if it
    > has been left blank (i.e. "" or null).
    >
    > So I tried
    >
    > getValueAt(iRow, iCol) == "" and getValueAt(iRow, iCol) == null
    >
    > and neither seem to work, but both do compile correctly.
    >
    > So my question is.... How do I test a jtable's cell for a blank entry?
    >
    > Many Thanks.
    >
    >


    I haven't tested, but I think this would work - if you fill the JTable
    with Strings. You may need a specific approach based on what objects are
    in the JTable.

    If you fill the table with for example HashMaps (I can't immediately
    think of a reason why you would want to do that, but it's quite
    possible), you could use something like:

    Object o = getValueAt(iRow, iCol);
    if(o != null)
    if(o instanceof HashMap)
    return ((HashMap)).isEmpty();
    return false;


    A more or less generic (not to be confused with Java's generics) way
    could be:

    getValueAt(iRow, iCol).toString().equals("")

    Always make sure you test for null first of course, to avoid
    NullPointerExceptions.

    --
    Beware the False Authority Syndrome
     
    zero, Jan 3, 2006
    #2
    1. Advertising

  3. tiewknvc9

    Vova Reznik Guest

    tiewknvc9 wrote:
    > Hi!
    >
    > I have a jtable object, and I am trying to test the object to see if it
    > has been left blank (i.e. "" or null).
    >
    > So I tried
    >
    > getValueAt(iRow, iCol) == "" and getValueAt(iRow, iCol) == null
    >
    > and neither seem to work, but both do compile correctly.
    >
    > So my question is.... How do I test a jtable's cell for a blank entry?
    >
    > Many Thanks.
    >


    Object value = getValueAt(row, col);
    if(value == null || ((value instanceof String)&&
    ((String)value).length == 0)){
    // null or if String then it is empty
    }

    Result of using an operator [==] and a method [equals] may not be always
    the same.
     
    Vova Reznik, Jan 3, 2006
    #3
  4. tiewknvc9

    tiewknvc9 Guest

    I followed Vova's advice, it worked like a charm.

    Thanks
     
    tiewknvc9, Jan 3, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,555
    manusa
    Apr 12, 2007
  2. Tintin92
    Replies:
    1
    Views:
    1,813
    Andrew Thompson
    Feb 14, 2007
  3. Allen
    Replies:
    6
    Views:
    3,830
    Allen
    May 7, 2007
  4. vlenin66
    Replies:
    1
    Views:
    775
    Janusch
    May 27, 2008
  5. Haircuts Are Important
    Replies:
    3
    Views:
    417
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page