Is it possible to have an empty integer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    Is it possible to have an empty integer?

    Sorry for the lolcat title, but seriously I don't know what the best approach would be.

    I'm working with a table model. In the data I have a String, 2 Integers, and 1 Boolean column.

    The Boolean and one of Integer columns CAN have empty values.

    When I'm populating the data I'm setting the default values of the Boolean to false and the integer to null like so:

    Code:
    result[ri][0] = sdtSrvc.getServiceId();
    result[ri][1] = "   "+sdtSrvc.getCallTag();
    result[ri][2] = (mapSrvc != null) ? mapSrvc.getCableID() : null; //<--------- Empty Integer Needed Here instead of null
    result[ri][3] = (mapSrvc != null) ? mapSrvc.getEnabledFlag() : false;
    result[ri][4] = sdtSrvc.getTransportStreamId();
    Symptom of problem:
    The records that DO have a value, JTable treats them like a string and aligns them to the left. Yuck!

    I should mention I have overridden the AbstracTableMod el's getColumnClass( ) like this:

    Code:
        @Override
        public Class getColumnClass(int c)
        {
            Object val = getValueAt(0, c);
    
            if(val == null) {
                Integer scrap = 0;
                return scrap.getClass();
            } else
            {
                return getValueAt(0, c).getClass();
            }
        }
    This method is called by JTable's Cell Render..er. As you can see I'm creating an int and returning it.

    it works, but again Yuck! and what if there's an empty record in the String column. The cell render will treat it as an int.

    If I could somehow declare my default value as an empty int that would be great and I can change the above method to just the return line:

    return getValueAt(0, c).getClass();

    and be done with it!

    My other option is to evaluate c (the column index) and find which column it is and hard code the default type of that column, but again what if I reorder my columns or drop columns in the future? I'd rather not have this mess to maintain.

    In short, can I have my cake and 'not' eat it too?

    : )

    Thanks for any feedback guys!


    Dan
    Last edited by NeoPa; Nov 19 '10, 10:43 PM. Reason: That title is not acceptable! It's bad enough clearing up after the n00bs Dan.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Integer can not be empty Dan. But you can display them as Strings. So if null is the value of in just display "". That is nothing to display.

    Probably i am confused about your question :(

    Regards
    Dheeraj Joshi
    Last edited by Dheeraj Joshi; Nov 22 '10, 05:24 AM. Reason: Typo corrected

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Thanks Dheeraj, That doesn't get the rid of the symptom. Which is: It makes the table think that column is a string instead of an integer, hence rending the numbers further down the list as a string!

      If I hard code logic in the getColumnClass( ) method, then I'd hard code the column index. Something I don't want to do because it requires maintenance.

      How would you go about handling this situation? At the moment I'm defaulting every empty column to an integer.

      Thanks again,

      Dan

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Why can't you render all the integers as String and display it in the table?

        Yes, Overriding may not be a good option.

        I would default all empty columns as a String.

        Regards
        Dheeraj Joshi

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          What i am saying even if the values is int or null render it as String. (Unless your app is not very bulky).

          It solves the rendering problems.

          Regards
          Dheeraj Joshi

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            So I can do that, but it's not just the empty values that are rendered as string, but the integers in that row will now be displayed as integer (i.e. they are left aligned instead of right aligned, you can enter non-number characters instead of just digits, etc)

            Thanks,

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              I thought it is just displaying(read-only). But anyway if user can enter the values for the table then displaying them as String is not a good idea, since it involves a lot of validations when user enters some characters or symbols.

              Regards
              Dheeraj Joshi

              Comment

              Working...