views:

21

answers:

2

I am trying that if a checkbox in column0 is selected a double value will be displayed in column2. Not sure where I am going wrong. Please review the code and advise.....thx....

package JavaTutes;

import java.awt.; import javax.swing.; import javax.swing.table.*;

import TablewCheckBoxComboBox.HeaderRenderer;

public class TableWithCheckBoxorBlank {

enum TriState
{
    VALUEA,
    VALUEB,
    NONE
}

private String[] columnNames = null;
private int noOfRows = 0;
private String[] hstIDs = null;
private Double[] hstTime = null;
private Double unitTime = 0.00;
private int frameHt = 0;
TCProcessData someData = new TCProcessData();

public void makeUI() 
{

    //Object[] columns = new Object[] {"Quick Est", "Detailed Est", "Ranked Time"};
    Object[] columns = new Object[]
            {
                "<html><center>" + "Quick Est" + "</center></html>",
                "<html><center>" + "Detailed Est" + "</center></html>",
                "<html><center>" + "Ranked Time" + "</center></html>",
            };

   noOfRows = someData.getNoOfRows();
   Object[][] data = new Object[noOfRows][3];

   for (int i = 0; i<noOfRows; i++)
   {
       data[i][0] = new Boolean(false);
       data[i][1] = new Boolean(false);
       data[i][2] = TriState.NONE;
   }

   final DefaultTableModel model = new DefaultTableModel(data, columns) 
   {
      @Override
      public boolean isCellEditable(int row, int column) 
      {
          switch (column) 
          {
            case 0: return true;
            case 1: return true;
            case 2: return TriState.NONE != getValueAt(row, 2);
          }
        return false;
      }

      @Override
      public Class<?> getColumnClass(int columnIndex) 
      {
         switch (columnIndex) 
         {
            case 0: return Boolean.class;
            case 1: return Boolean.class;
            case 2: return TriState.class;
            //case 2: return double.class;
         }
         return null;
      }

      @Override
      public void setValueAt(Object value, int row, int column)
      {
          if (column == 0)
          {
              if (Boolean.TRUE.equals(value)) 
              {
                  setValueAt(someData.getUnitTime(row), row, 2);
                  setValueAt(Boolean.FALSE, row, 1);
              }
              else 
              {
                  setValueAt(TriState.NONE, row, 2);
              }
          }
          else if (column == 1)
          {
              if (Boolean.TRUE.equals(value)) 
              {
                  setValueAt(0.00, row, 2);
                  setValueAt(Boolean.FALSE, row, 0);
              }
              else 
              {
                  setValueAt(TriState.NONE, row, 2);
              }
          }
      }


    };

   final JTable table = new JTable(model);

   HeaderRenderer headerRenderer = new HeaderRenderer(32);
   JTableHeader jtHeader = table.getTableHeader();
   jtHeader.setDefaultRenderer(headerRenderer);
   table.setRowHeight(25);
   frameHt = noOfRows*25;
   TableCellRenderer tristate = new TableCellRenderer() 
   {

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) 
    {
        if (value instanceof TriState) 
        {

            Double unitTime = someData.getUnitTime(row);
            Double theValue = map((TriState) value);

            if (theValue != null)
            {
                TableCellRenderer delegate = table.getDefaultRenderer(Double.class);
                return delegate.getTableCellRendererComponent(table, theValue, isSelected, hasFocus, row, column);
            }
            value = null;
        }
        TableCellRenderer delegate = table.getDefaultRenderer(Object.class);
        return delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }

    protected Double map(TriState value) 
    {
        if (TriState.VALUEB == value) 
        {
                double defaultNull = 0.00;
            return defaultNull;
        }
        if (TriState.VALUEA == value) 
        {
                return unitTime;
        }
        return null;
    }

   };
   table.setDefaultRenderer(TriState.class, tristate);

   TableCellEditor editor = new DefaultCellEditor(new JCheckBox()) 
   { 
    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) 
    {
        if (value instanceof TriState) 
        {
            if (TriState.NONE != value) 
            {
                value = (TriState) value == TriState.VALUEA ? Boolean.TRUE : Boolean.FALSE;
            }
        }
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

   };
   table.setDefaultEditor(TriState.class, editor);


   JFrame frame = new JFrame("TimeRepTable");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(208, 230);
   frame.setLocationRelativeTo(null);
   frame.add(new JScrollPane(table));
   frame.setVisible(true);
}

public static void main(String[] args) 
{
   SwingUtilities.invokeLater(new Runnable() 
   {
      public void run() 
      {
         new TableWithCheckBoxorBlank().makeUI();
      }
   });
}

}

A: 

Cross posted

http://www.coderanch.com/t/510484/GUI/java/Help-JCheckBox-JTable-return-value

and on forums.sun.com

db

Darryl Burke
Welcome to the forum user450134 :) How many forums is this now?
camickr
I've lost count :)
Darryl Burke
A: 

I have re-done this code...however I still have a problem. The value in column2 does not show up. It does get calculated - just does not show up. It is on totally 3 forums - forums.sun.com, stackoverflow and coderanch. Any other good ones out there that I should know off - I've just started java programming - about 2months in now. Thx.....

Here is the new code:

package JavaTutes;

import java.awt.*;

import javax.swing.; import javax.swing.event.; import javax.swing.table.*;

public class TablewChkBoxB extends JFrame implements TableModelListener { private JTable table;

private String[] columnNames = null;
private int noOfRows = 0;
private int frameHt = 0;
private double unitTimeD = 0.00;
private String unitTimeS = null;
private String unitTime = null;
double defaultNull = 0.00;

String nullString = String.valueOf(defaultNull); TCProcessData someData = new TCProcessData(); private String[] unitTimeSA = {"21.00", "11.45", "15.75", "55.23", "5.98", "31.56"};

public void makeUI() 
{

 Object[] columns = new Object[] {"Quick Est", "Detailed Est", "Ranked Time"};

   //noOfRows = someData.getNoOfRows();
   noOfRows = 6;
   Object[][] data = new Object[noOfRows][3];

   for (int i = 0; i<noOfRows; i++)
   {
    data[i][0] = new Boolean(false);
    data[i][1] = new Boolean(false);
       data[i][2] = "";
   }

   DefaultTableModel model = new DefaultTableModel(data, columns);
   model.addTableModelListener(this);
   table = new JTable(model)
   {
      @Override
      public boolean isCellEditable(int row, int column) 
      {
          switch (column) 
          {
           case 0: return true;
           case 1: return true;
           case 2: return false; //TriState.NONE != getValueAt(row, 2);
          }
        return false;
      }

      @Override
      public Class<?> getColumnClass(int columnIndex) 
      {
         switch (columnIndex) 
         {
            case 0: return Boolean.class;
            case 1: return Boolean.class;
            case 2: return String.class;
         }
         return null;
      }
   };

      table.setRowHeight(25);
      frameHt = noOfRows*25;

      JFrame frame = new JFrame("TimeRepTable");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(208, 230);
      frame.setLocationRelativeTo(null);
      frame.add(new JScrollPane(table));
      frame.setVisible(true);

}

      public void tableChanged(TableModelEvent e)
      {
       System.out.println(e.getSource());
       if(e.getType() == TableModelEvent.UPDATE)
       {
        int row = e.getFirstRow();
        int column = e.getColumn();
        if(column == 0)
        {
       TableModel model = table.getModel();
       Boolean bool = ((Boolean)model.getValueAt(row, 0));
       System.out.println("value at" + "row:" + row + "and col:" + column + "is:" + bool);
       if (bool == true)
       {
       //unitTimeD = someData.getUnitTime(row);
       //unitTimeS = String.valueOf(unitTimeD);
        System.out.println("unitTimeS at row:" + row + "is:" + unitTimeSA[row]);
        model.setValueAt(unitTimeSA[row], row, 2);
        model.setValueAt(Boolean.FALSE, row, 1);
       }
       else
       {
        model.setValueAt("", row, 2);
       }
        }
        if (column == 1)
        {
       TableModel model = table.getModel();
         Boolean bool = ((Boolean)model.getValueAt(row, 1));
         System.out.println("value at" + "row:" + row + "and col:" + column + "is:" + bool);
         if (bool == true)
         {
       //unitTimeD = someData.getUnitTime(row);
          //unitTimeS = String.valueOf(unitTimeD);
          model.setValueAt(nullString, row, 2);
          model.setValueAt(Boolean.FALSE, row, 0);
         }
         else
         {
          model.setValueAt("", row, 2);
         }
          }
       }
      }

public static void main(String[] args) 
{
   SwingUtilities.invokeLater(new Runnable() 
   {
      public void run() 
      {
         new TablewChkBoxB().makeUI();
      }
   });
}

}

akrrs999