problem with jtable listener

hi,

I am facing problem with JTable listener. I have 2 columns and the result of 2 columns multiplication should be print on 3 column. I have used JTable Listener and in tableChanaged() method I have written code. I got everything but while setting value at spcific row I used setValueAt(..) mehod giving exception .my code is giving below

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.event.*;

import java.awt.event.*;

import java.io.*;

public class TestTable extends JFrame implements ActionListener,TableModelListener

{

JTable caltable;

Object[][] rows;

Object[] cols;

JButton calculate;

JButton add;

DefaultTableModel tm;

Integer re;

int r=0;

int l=0;

boolean tabBool;

int columnNo;

int rowNo;

TestTable()

{

Object[] cols={"Price","Quantitiy","result"};

Object[][] rows={{"2","2",""},{"3","5",""}};

tm=new DefaultTableModel(rows,cols);

caltable=new JTable(tm);

JScrollPane tabScroll=new JScrollPane(caltable);

//creating new button for adding new row

add=new JButton("add");

add.addActionListener(this);

//adding listener to cloumn

TableColumnModel colModel=caltable.getColumnModel();

TableColumn colSingle=colModel.getColumn(2);

caltable.setSize(300,300);

calculate=new JButton("CALCULATE");

calculate.addActionListener(this);

getContentPane().setLayout(new FlowLayout());

tm.addTableModelListener(this);

this.getContentPane().add(tabScroll);

this.getContentPane().add(calculate);

this.getContentPane().add(add);

this.setSize(500,500);

this.setVisible(true);

}

public void tableChanged(TableModelEvent te)

{

columnNo=caltable.getSelectedColumn();

rowNo=caltable.getEditingRow();

if((columnNo!=-1)&&(columnNo==1))

{

calculate(rowNo,columnNo);

}

}

public void actionPerformed(ActionEvent e)

{

String button_info=e.getActionCommand();

if(button_info.equals("add"))

{

tm.addRow(new String[] {"", "",""});

}

}

void calculate(int rowNo,int colNo)

{

Object frowone=caltable.getValueAt(rowNo,colNo);

Object frowtwo =caltable.getValueAt(rowNo,colNo);

System.out.println("The value is frowone"+frowone);

System.out.println("The value is frowtwo"+frowtwo);

String t= ((String)frowone);

int l=Integer.parseInt(t);

String j=(String)frowtwo;

int k=Integer.parseInt(j);

System.out.println("The i is"+l+"The j is"+k);

int result=l*k;

re=new Integer(result);

System.out.println("The Result is"+re);

tm.setValueAt(re,rowNo,2);

}

public static void main(String args[])

{

TestTable testTable=new TestTable();

testTable.setSize(500,500);

testTable.setVisible(true);

}

}

if remove tm.setValueAt(...) Its giving result at command prompt .

Please try to rectify where I did mistake..

Message was edited by:

harishspi

[3203 byte] By [harishspia] at [2007-11-27 6:02:13]
# 1

Use [url http://forum.java.sun.com/help.jspa?sec=formatting]code formatting[/url] when posting code.

You are making things too complicated.

A better way would be to override getValueAt of DefaultTableModel and calculate the multiplication there.

Read the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Here's a simple example:

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

public class TestRowMultiplication {

public static void main(String[] args) {

try {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DefaultTableModel model = new DefaultTableModel(new Integer[][] {{1, 2}, {3, 4}}, new String[] {"col1", "col2"}) {

public int getColumnCount() {

return super.getColumnCount() + 1;

}

public Class<?> getColumnClass(int columnIndex) {

return Integer.class;

}

public Object getValueAt(int row, int column) {

if (column == 2)

return new Integer(((Integer ) getValueAt(row, 0)).intValue() * ((Integer ) getValueAt(row, 1)).intValue());

return super.getValueAt(row, column);

}

public String getColumnName(int column) {

if (column == 2)

return "Multiplication";

return super.getColumnName(column);

}

public boolean isCellEditable(int row, int column) {

if (column == 2)

return false;

return super.isCellEditable(row, column);

}

public void setValueAt(Object aValue, int row, int column) {

super.setValueAt(aValue, row, column);

fireTableCellUpdated(row, 2);

}

};

frame.add(new JScrollPane(new JTable(model)));

frame.pack();

frame.setVisible(true);

catch (Exception e) {e.printStackTrace();}

}

}

Rodney_McKaya at 2007-7-12 16:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 2
I like the idea of using a TableModelListener. This posting shows my working example of how to do this: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=566133
camickra at 2007-7-12 16:43:19 > top of Java-index,Desktop,Core GUI APIs...