🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8861

Re: JTable

#8861
From: sven goetgeluck
Date: Thu, 06 Jul 2000 00:00
241 lines
7556 bytes
did all the code like you suggested :

public class MyTableModel extends AbstractTableModel {
        final String[] columnNames = {"Object name",
                                      "Total time",
                                      "Green time",
                                      "Yellow time",
                                      "Orange time",
                                      "Red time"};

        public static final int NAME_COLUMN    = 0;
        public static final int TOTAL_COLUMN    = 1;
        public static final int GREEN_COLUMN   = 2;
        public static final int YELLOW_COLUMN  = 3;
        public static final int ORANGE_COLUMN  = 4;
        public static final int RED_COLUMN     = 5;

        private Vector objects = new Vector();
        public void addObject(workstationObjectClass object)
        {
         if (objects != null)
          {
           objects.addElement(object);
           int index = objects.size()-1;
           if (index>0)
             fireTableRowsInserted(index,index);
//           fireTableRowsInserted( int firstRow, int lastRow )
          }
        }

        public int getColumnCount()
        {
            return 6;
        }

        public int getRowCount()
        {
         return objects.size();
        }

        public String getColumnName(int col)
        {
            if (columnNames[col] != null)
              return columnNames[col];
            else return "";
        }
         public boolean isCellEditable(int rowIndex, int columnIndex)
        {
        return columnIndex != 0;//title is not editable
        }
        public Class getColumnClass(int columnIndex)
        {
         switch(columnIndex)
         {
          case NAME_COLUMN   : return String.class;
          case TOTAL_COLUMN  : return double.class;
          case GREEN_COLUMN  : return double.class;
          case YELLOW_COLUMN : return double.class;
          case ORANGE_COLUMN : return double.class;
          case RED_COLUMN    : return double.class;
          default: return null;
         }
        }


        public Object getValueAt(int rowIndex, int columnIndex)
        {
         if (objects != null)
         {
          workstationObjectClass object = (workstationObjectClass)
objects.elementAt(rowIndex);
          switch (columnIndex)
          {
           case NAME_COLUMN   : return object.getName();
           case TOTAL_COLUMN  : return object.getTotal();
           case GREEN_COLUMN  : return object.getGreen();
           case YELLOW_COLUMN : return object.getYellow();
           case ORANGE_COLUMN : return object.getOrange();
           case RED_COLUMN    : return object.getRed();
           default : return null;

          }
         }
         return null;
        }

    public void setValueAt(Object value, int rowIndex, int columnIndex)
    {
      workstationObjectClass object = (workstationObjectClass)
objects.elementAt(rowIndex);
      try
      {
/*         switch (columnIndex)
         {
           case NAME_COLUMN   : object.getName();
           case TOTAL_COLUMN  : object.getTotal();
           case GREEN_COLUMN  : object.getGreen();
           case YELLOW_COLUMN : object.getYellow();
           case ORANGE_COLUMN : object.getOrange();
           case RED_COLUMN    : object.getRed();
         }
*/
       //object.setGegevens(100,50,20,15);
// fireTableCellUpdated(rowIndex, columnIndex);
      }
      catch (ClassCastException cce)
      {
       System.out.println(cce);
      }
    }

  }

/////////:object needed to display

public class workstationObjectClass
{
  private String Name;
  private double Green;
  private double Yellow;
  private double Orange;
  private double Red;
  private double Total;

  public workstationObjectClass(String Name,double Green,double Yellow,
double Orange,double Red)
  {
   this.Name   = Name;
   this.Green  = Green;
   this.Orange = Orange;
   this.Yellow = Yellow;
   this.Red    = Red;
   this.Total = Green+Red+Orange+Yellow;
  }
  public String getName()
  {
   return Name;
  }
  public Double getGreen()
  {
   return new Double(Green);
  }
  public Double getRed()
  {
   return new Double(Red);
  }
  public Double getTotal()
  {
   return new Double(Total);
  }
  public Double getOrange()
  {
   return new Double(Orange);
  }
  public Double getYellow()
  {
   return new Double(Yellow);
  }

  public void setGegevens(double green , double yellow, double orange,double
red)
  {
   Green = green ;
   Orange = orange;
   Red = red;
   Yellow = yellow;
   Total = red+green+orange+yellow;
  }


  public String toString()
  {
   return getName();
  }

}


/////////creation of the table
 Dimension dim = this.getSize();
    tableModel = new MyTableModel();
    tableModel.addObject(new workstationObjectClass("sven",100,20,50,12));
    /*tableModel.addObject(new
workstationObjectClass("sven2",120,120,150,2));
    tableModel.addObject(new workstationObjectClass("sven",100,20,50,12));*/
    objectsTable = new JTable(tableModel);
    objectsTable.setToolTipText("teste");
    scrollPane = new JScrollPane(objectsTable);
    objectsTable.setPreferredScrollableViewportSize(dim);
   this.add(scrollPane,BorderLayout.CENTER);


but when it display it crashes

it has en nullpointerexeption in
JTable.preparerenderer

what could it be Paul??
any suggestions??
greetings Sven

"John P. McGrath" wrote:

> On Wed, 05 Jul 2000 15:52:32 +0200, sven goetgeluck wrote:
>
> > must create at runtime a table with diffents count of rows,
> > is this possible?
> > all examples found have fixed rowlength , how can i insert
> > a row ..
> > I use a jtable with a model
>     :
> >        final Object[][] data = null;
>
> Since you provide the TableModel, you need to manage the data structures
> that hold the data.  A Java array of arrays is not the best structure to
> use for this.  Instead, try using an ArrayList of ArrayLists, or a Vector
> of Vectors if you are stuck on JDK 1.1, or if your table model needs to
> be synchronized.
>
> >   public void addRow(Object[] rowdata)
> >   {
> >  fireTableCellUpdated(row, col);
> >
> >   }
>
> After adding the row (not before), you will need to fire an event to
> notify listeners that the row has been added.  But fireTableCellUpdated()
> notifies listeners that the value of one cell has changed.  When you add
> a row you should use:
>
>     fireTableRowsInserted( int firstRow, int lastRow )
>
> Take a closer look at AbstractTableModel.  There are 7 different fireXXXX
> methods there for notifying listeners about different changes.  If you
> use the wrong one, your code may not behave properly or it may run a lot
> slower than need be.
>
> --
> John P. McGrath
> <JPMcGrath@fast.net>
> <jpm@nscs.fast.net>
Technical Details
Message-ID: <39646048.F73D8200@officeline.be>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <39633DA0.A4D28790@officeline.be> <MPG.13cde3eed0933ce4989a13@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,a00bcc41b12b3cb4
X-Google-Attributes: gid111d45,public
From: sven goetgeluck <sven@officeline.be>
Subject: Re: JTable
Date: 2000/07/06
Message-ID: <39646048.F73D8200@officeline.be>
X-Deja-AN: 643006930
Content-Transfer-Encoding: 7bit
References: <39633DA0.A4D28790@officeline.be> <MPG.13cde3eed0933ce4989a13@forums.borland.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: 6 Jul 2000 03:22:45 -0800, 127.0.0.1
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing