Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6385326

Boxes in the JTable with values from database

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • x86
    • windows_98

      FULL PRODUCT VERSION :
      java version "1.6.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-rc-b70)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 98SE 4.10.2222 A

      A DESCRIPTION OF THE PROBLEM :
      I get strange boxes in addition to field values in the JTable which show results from database. I am updating its model each time. This is not so when I use jdk1.3 or 1.4. I have all the three machines on my system, but only JDK1.6 is installed. the other two are just copied to my computer.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Do I send you my access database and my ResultsModel class and other classes? This extends AbstractTableModel.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      clean data field values from database
      ACTUAL -
      data field values are contaminated with square boxes that appear at the end of value

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      Cannot send you the application but here is my model class:

      package jdir;

      import javax.swing.table.*;
      import javax.swing.*;
      import java.util.*;
      import java.awt.*;
      import java.sql.ResultSet;
      import java.sql.ResultSetMetaData;
      /**
       * <p>Title: JDirectory</p>
       * <p>Description: Java Solution For Directory</p>
       * <p>Copyright: Copyright (c) 2005</p>
       * <p>Company: CymaSoft Works</p>
       * @author Adil Jumshed
       * @version 1.0
       */

      public class ResultsModel extends AbstractTableModel{

        int columns=0;
        String [] columnNames=new String[0];
        String [] columnClass =new String [0];
        ArrayList dataRows;
        int records=0;
        String grandtotal="";

        public ResultsModel(){}

        public void setResultSet(ResultSet results){

          try{
            ResultSetMetaData metadata=results.getMetaData();
            columns=metadata.getColumnCount();
            columnNames=new String[columns];
            columnClass=new String[columns];

          // Get the columns names
            for(int i=0;i<columns;i++){
              columnNames[i]=metadata.getColumnLabel(i+1);
            }

           //Get all rows
            dataRows=new ArrayList();
            String [] rowData;
            double gtotal=0;

            String fieldval="";
            while(results.next()){

              rowData=new String[columns];
              records++;

              for (int i=0;i<columns;i++){
            //columnClass[i]=metadata.getColumnClassName(i+1);
                String temp=null;
                try{
                  temp=results.getString(i+1);
                  }catch(Exception e){}

                  if(temp!=null){
                    fieldval=temp;
                    temp=null;
                  }
                  else continue;

                  if( columnNames[i].equalsIgnoreCase("Dated")){
                    rowData[i]=FormatDate.getDateFromDatabase(fieldval);
                    continue;
                  }
                  rowData[i]=fieldval;

              }
              dataRows.add(rowData);

            }

            // if result gets one empty row only, check it
            if(records==1){
              boolean emptyrow=false;
              rowData=(String[])dataRows.get(0);

              for (int i=0;i<columns;i++){
                if(rowData[i]==""){
                  emptyrow=true;
                }
                else{
                  emptyrow=false;
                  break;
                }
              }
              if(emptyrow)records=0;
            }

            // if no result
            if(records==0){
              rowData=new String[columns];
              int pos=5;
              for (int i=0;i<columns;i++){
                if(i==pos)
                  rowData[i]="<html><body bgcolor=black text=red><b>NO DATA<b></body></html>";
                else
                  rowData[i]="-";
              }

              dataRows.add(rowData);
            }

                // My display form
      RecordsPanel.txtRecords.setText(""+records);
            records=0;

            fireTableChanged(null);

            grandtotal=null;
            rowData=null;

          }
          catch(Exception sqle){
            System.err.println("ResultsModel: "+sqle);
          }
          System.out.println("ResultsModel Free Memory="+(Runtime.getRuntime().freeMemory()/1024)+" bytes");
          System.out.println("ResultsModel Total Memory="+(Runtime.getRuntime().totalMemory()/1024)+" bytes");

          System.gc();
          System.out.println("ResultsModel Free Memory="+(Runtime.getRuntime().freeMemory()/1024)+" bytes");
          System.out.println("ResultsModel Total Memory="+(Runtime.getRuntime().totalMemory()/1024)+" bytes");

        }

        public String getColumnName(int column){
          return columnNames[column];
        }

        public int getRowCount(){

          if (dataRows==null)
            return 0;
          else
            return dataRows.size();
        }

        public Object getValueAt(int row,int column){
          return ((String [])( dataRows.get(row)) )[column];
        }

        public int getColumnCount(){
          return columns;
        }

      //package com.inprise.pm;

      void setColumnHeaderPreferredWidth(JTable table, String colName)
      {
      TableColumn col =null;
      try{
      col=table.getColumn(colName);
      }
      catch (Exception ex) {
      new DebugWriter().writeFile("ResultsModel: setColumnHeaderPreferredWidth TableColumn col:"+ex);

      }
      /*
      TableCellRenderer renderer =null;
      try{
      TableCellRenderer getDefaultRenderer();
      DefaultTableCellRenderer();
      renderer=col.getHeaderRenderer();
      }
      catch (Exception ex) {
      new DebugWriter().writeFile("ResultsModel:: setColumnHeaderPreferredWidth TableCellRenderer renderer:"+ex);

      }
      Component comp =null;
      // System.out.println( col.getHeaderValue());
      // System.out.println( renderer);
      try{
      comp =renderer.getTableCellRendererComponent( table, col.getHeaderValue(),false, false, 0, 0);
      }
      catch (Exception ex) {
      new DebugWriter().writeFile("ResultsModel: setColumnHeaderPreferredWidth Component comp:"+ex);

      }
      //System.out.println(comp.getPreferredSize().width);
      col.setPreferredWidth(comp.getPreferredSize().width + 10);
      col.setMinWidth(comp.getPreferredSize().width + 10);
      col.setMaxWidth(comp.getPreferredSize().width + 10);

      */
      col.setPreferredWidth(200);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :

      I don't know; maybe if I use String.trim() before displaying values in JTable, it could help.

            ndcosta Nelson Dcosta (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: