tags:

views:

27

answers:

2

Hello:

This is the first time for me to post here, so sorry if I made some mistake.

I am working on a JTable which column data have to verify some parameters, for example:

Column 3 values > 30
Column 4 values > 10
Column 5 values > 4

Also the first 2 columns are filled "automatically", putting 0s in the rest of the columns.

If that data is correct, in the Column 5 I would show an image of a tick, otherwise, I would show an image of a warning.

For verifying this I use the following code

    ImageIcon accept = new javax.swing.ImageIcon(getClass().getResource("/resources/accept.png"));
    ImageIcon deny = new javax.swing.ImageIcon(getClass().getResource("/resources/exclamation.png"));

    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        double d1 = Double.valueOf(jTable.getValueAt(row, 2).toString());
        double d2 = Double.valueOf(jT.getValueAt(row, 3).toString());
        double d3 = Double.valueOf(jT.getValueAt(row, 4).toString());

        if(d1>MAX_A||d2>MAX_B||d3>MAX_C){
            jTable.setValueAt(deny, row, 5);
        }
        else{
            jTable.setValueAt(accept, row, 5);
        }
    }

The problem of this code is that returns a Stack Overflow, and I don't know how to handle this.

Is there any other way to implement some verifier on a table that implies multiple cells?

Thanks in advance.

+1  A: 

You probably get your error because of getFirstRow call. I think it's a bad idea to validate table structure in JTable.tableChanged - how do you know that the table was fully filled and ready for validation? I would suggest filling the whole table first, then invokation of validation. Maybe there would be also a good idea to use separate table to display validation results

denis_k
I have tried what you suggested, and while is not as dynamic as I wanted it to be, solves the problem, thanks!
rlbisbe
Actually the suggestion below from camickr is of more use then mine - I really missed that you fire event recursively infinite amount of times, and `getFirstRow` has nothing to do with error. Just add `if` clause, as described below, and your app will be as dynamic of you want it to be
denis_k
+2  A: 

The problem of this code is that returns a Stack Overflow, and I don't know how to handle this.

The problem is that your code sets a value in the model listener so another tableChanged event is generated. Your code should be something like:

if (e.getColumn() != 5)
   // do your code

I don't see a problem using a TableModelListener to dynamically set the value of a column based on data in another column. Here is a simple example:

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

public class TableProcessing extends JFrame implements TableModelListener
{
    JTable table;

    public TableProcessing()
    {
        super("Table");
        String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
        Object[][] data =
        {
            {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
            {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
            {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
            {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        model.addTableModelListener( this );
//
        table = new JTable( model )
        {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }

            //  The Cost is not editable
            public boolean isCellEditable(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );
                return (modelColumn == 3) ? false : true;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
//
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );


        String[] items = { "Bread", "Milk", "Tea", "Coffee" };
        JComboBox editor = new JComboBox( items );

        DefaultCellEditor dce = new DefaultCellEditor( editor );
        table.getColumnModel().getColumn(0).setCellEditor(dce);
    }

    /*
     *  The cost is recalculated whenever the quantity or price is changed
     */
    public void tableChanged(TableModelEvent e)
    {
        if (e.getType() == TableModelEvent.UPDATE)
        {
            int row = e.getFirstRow();
            int column = e.getColumn();

            if (column == 1 || column == 2)
            {
                TableModel model = table.getModel();
                int quantity = ((Integer)model.getValueAt(row, 1)).intValue();
                double price = ((Double)model.getValueAt(row, 2)).doubleValue();
                Double value = new Double(quantity * price);
                model.setValueAt(value, row, 3);
            }
        }
    }

    public static void main(String[] args)
    {
        TableProcessing frame = new TableProcessing();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
camickr
Nice example - you're quite right, I really missed the point...
denis_k