Forum Discussion

raj5c1's avatar
raj5c1
Occasional Contributor
8 years ago

getting display format of the data field in java swing table

I am trying to get the value from Date field of one of the columns in Jtable object. When i get the value i am using toString on it (OleValue is not working). But toString converting the date to default format( Mon Nov 03 08:00:42 CDT 2016) instead of the displayed format(eg 11/03/2016). I tried to use the few native methods like cellRender object of that column but none of them  have any value. 

Here is how iam getting the value 

 grid.getValueAt(0, 1).toString

 

Is there any way that i can get displayed text form Date object.

  • Hi raj5c1,

     

    Try this function:

    // Returns the displayed value in the specified JTable cell
    // Parameters:
    // table - a JTable object
    // row, column - integer indexes of the row and column
    function getRenderedValue(table, row, column) { var value = table.getValueAt(row, column); var renderer = table.getCellRenderer(row, column).getTableCellRendererComponent(table, value, false, false, row, column); if (aqObject.IsSupported(renderer, "getText")) { value = renderer.getText(); } return value; }

    It is based on the code found on GitHub:
    Extract table cell value from cell renderer rather than model

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi raj5c1,

     

    Try this function:

    // Returns the displayed value in the specified JTable cell
    // Parameters:
    // table - a JTable object
    // row, column - integer indexes of the row and column
    function getRenderedValue(table, row, column) { var value = table.getValueAt(row, column); var renderer = table.getCellRenderer(row, column).getTableCellRendererComponent(table, value, false, false, row, column); if (aqObject.IsSupported(renderer, "getText")) { value = renderer.getText(); } return value; }

    It is based on the code found on GitHub:
    Extract table cell value from cell renderer rather than model

    • raj5c1's avatar
      raj5c1
      Occasional Contributor

      Helen,

       

      That's what i need. Thanks.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    OK... so, it's a Java Swing Table, correct?

    Have you simply tried

     

    grid.wValue(0,1)

    That should return simply the value in the designated cell.  Now, if that doesn't return a nice "date" format (although, wValue, as far as I know, is exactly the display value), perhaps it's a DateTime value that is being converted via code into your displayed value.  In which case, you can use aqConvert.DateTimeToFormatStr to define your own formatting of the string.

    • raj5c1's avatar
      raj5c1
      Occasional Contributor

      I tried wValue(0,4). that is also giving me the Date object. if i try to print the value without using tostring, it prints nothing. if i use the toString on it , agin it will convert the date to default format. I can use the aqConvert.DateTimeToFormatStr(DFormatStr), only if i know the format before hand. but that is going to be different from one application to another.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Have you tried toLocaleString instead of just toString?

        Generally speaking, this is a matter of specific formatting of the string representation of the Java data object referenced by the field in the grid. That's not a TestComplete issue but a Java/JavaScript coding issue.

         

        You could also just use aqConvert.DateTimeToStr as that will return the locale specific formatting.  If you want to construct something yourself, you could use the aqDateTime object and then construct your own string using the GetMonth, GetDayOfYear, and GetYear methods to build what you are looking for.  Something like

         

        var myCellValue = grid.wValue(0,4);
        var myString = aqDateTime.GetMonth(myCellValue) + '/' + aqDateTime.GetDayOfYear(myCellValue) + '/' + aqDateTime.GetYear(myCellValue);
        Log.Message(myString);

         

        Just some suggestions to try...