2 of the columns in my JTable hold monetary values ( double).
If the value in cell A changes, then value B should be updated with the result of a calculation performed using the new value in A and the old value in B.
Sort of like a spreadsheet.
I can't seem to set the new value of A or B, whichever is updated, after a test calculation.
At present my code is as follows:
If the value in cell A changes, then value B should be updated with the result of a calculation performed using the new value in A and the old value in B.
Sort of like a spreadsheet.
I can't seem to set the new value of A or B, whichever is updated, after a test calculation.
At present my code is as follows:
Code:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TE extends JFrame
{
String[] cols = {"VAL_1", "VAL_2", "VAL_3"};
Object[][] filler = {
{new Double(100.00), new Double(100.00), new Double(100.00)},
{new Double(200.00), new Double(200.00), new Double(200.00)},
{new Double(400.00), new Double(400.00), new Double(400.00)}
};
JTable table;
TE(String title)
{
super(title);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MTM mtm = new MTM(3, cols.length);
mtm.setColumnIdentifiers(cols);
int nRows = filler.length;
for(int i = 0; i < nRows; ++i)
{
mtm.setValueAt(filler[0][i], 0, i);
mtm.setValueAt(filler[1][i], 1, i);
mtm.setValueAt(filler[2][i], 2, i);
}
table = new JTable(mtm);
table.getColumnModel().getColumn(1).setCellEditor(new SimpleCellEditor());
table.getColumnModel().getColumn(2).setCellEditor(new SimpleCellEditor());
//table.getColumnModel().getColumn(2).setCellEditor(new SimpleCellEditor());
JScrollPane jsp = new JScrollPane(table);
Container c = getContentPane();
c.add(jsp);
setSize(300,300);
setVisible(true);
}
class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
{
table.setValueAt("QQQQQQQ", 1,1);
}
}
}
class SimpleCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener
{
JTextField tf = new JTextField();
TableModel tm = table.getModel();
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
public SimpleCellEditor()
{
super();
tf.addMouseListener(new MyMouseListener());
tf.addActionListener(this);
}
public void addCellEditorListener(CellEditorListener listener) {
listenerList.add(CellEditorListener.class, listener);
}
public void removeCellEditorListener(CellEditorListener listener) {
listenerList.remove(CellEditorListener.class, listener);
}
protected void fireEditingStopped()
{
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++)
{
if (listeners[i] == CellEditorListener.class)
{
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled()
{
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++)
{
if (listeners[i] == CellEditorListener.class)
{
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
}
public void cancelCellEditing()
{
fireEditingCanceled();
}
public boolean stopCellEditing()
{
fireEditingStopped();
return true;
}
public boolean isCellEditable(EventObject event)
{
return true;
}
public boolean shouldSelectCell(EventObject event)
{
return true;
}
public Object getCellEditorValue()
{
return tf.getText();
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
if(tf.hasFocus() == true)
{
tf.setBackground(Color.CYAN);
}
return tf;
}
public void actionPerformed(ActionEvent e)
{
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
double nVal = 0.00;
Object currCostVal;
Object currSellVal;
Double costVal;
Double sellVal;
double newSellVal;
double currentCost;
if(table.getSelectedColumn() == 1)
{
currCostVal = table.getValueAt(row, col+1);
currSellVal = table.getValueAt(row, col);
costVal = new Double(currCostVal.toString());
currentCost = costVal.doubleValue();
sellVal = new Double(currSellVal.toString());
newSellVal = sellVal.doubleValue();
nVal = newSellVal*currentCost*100/100;
System.out.println("Recommended sell-price after change: " + nVal);
}else if(table.getSelectedColumn() == 2 )
{
currCostVal = table.getValueAt(row, col);
currSellVal = table.getValueAt(row, col-1);
costVal = new Double(currCostVal.toString());
currentCost = costVal.doubleValue();
sellVal = new Double(currSellVal.toString());
newSellVal = sellVal.doubleValue();
nVal = newSellVal*currentCost*100/100;
System.out.println("Recommended sell-price after change: " + nVal);
System.out.println("Cost column selected " + nVal);
}
}
}// end simple cell editor
class MTM extends DefaultTableModel
{
MTM(int rows, int cols)
{
super(rows, cols);
}
}
public static void main(String args[])
{
TE te = new TE("Test of table cell update");
}
}