trouble JTable with strings edited with no update

hi im writing a program to calculate resestors in parelell though it is only half complete ive hit a wall, i cannon (when i have alterd strings get the table to update and displey new information. im only new to java so sorry if its obvious mistake

also for some reason now the input is not being places into the string?

use the test menu item to check variables comtents

i have made it out of 2 code files as follows

/*

* Nicholas

* ResistorTableFrame.java

*

* Assignment 1: Resistors in Parallel

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.TableColumn;

publicclass ResistorTableFrameextends JFrameimplements ActionListener

{

private JMenuItem itmAuthour, itmInput, itmExit, itmHowTo, itmcheck/*deleteme*/;// Dropdown menu contents

private JButton btnClr, btnCalc;//Defines buttons

private ResistorTableModel resistorTable;//Defines names for tables

ResistorTableFrame()

{

// ============== MENU'S & CONTENTS ==============

// Main menu definitions & names

JMenu mnuFile =new JMenu("File");

JMenu mnuHelp =new JMenu("Help");

// Main menu items

mnuFile.add(itmcheck =new JMenuItem("test"));/*deleteme*/

mnuFile.add(itmAuthour =new JMenuItem("Author"));

mnuFile.addSeparator();

mnuFile.add(itmInput =new JMenuItem("Input Values"));

mnuFile.addSeparator();

mnuFile.add(itmExit =new JMenuItem("Exit"));

mnuHelp.add(itmHowTo =new JMenuItem("How To"));

// Main menu hotkeys

mnuFile.setMnemonic('f');

mnuHelp.setMnemonic('h');

int keycode = java.awt.event.KeyEvent.VK_A;

int modifier = java.awt.event.KeyEvent.CTRL_MASK;

// Main menu Decloration

JMenuBar mainBar =new JMenuBar();

this.setJMenuBar(mainBar);

mainBar.add(mnuFile);

mainBar.add(mnuHelp);

// Menu selection listening

itmcheck.addActionListener(this);/*deleteme*/

itmAuthour.addActionListener(this);

itmHowTo.addActionListener(this);

itmExit.addActionListener(this);

itmInput.addActionListener(this);

// ============== TABLE ==============

resistorTable =new ResistorTableModel();

JTable tblTable =new JTable(resistorTable);// Apply data to table

// ============== PANEL'S & CONTENTS (Buttons & Table) ==============

// BUTTON TEXT

btnClr =new JButton("Clear");

btnCalc =new JButton("Calculate");

// Button selection listening

btnClr.addActionListener(this);

btnCalc.addActionListener(this);

//GUI layout

Container cn = this.getContentPane();

cn.add(tblTable, BorderLayout.NORTH);

cn.add(btnClr, BorderLayout.CENTER);

cn.add(btnCalc, BorderLayout.SOUTH);

}

// ============== MENU & BUTTON ACTIONS ==============

publicvoid actionPerformed(ActionEvent e)

{

if (e.getSource() == itmAuthour)

{

JOptionPane.showMessageDialog(null,"Nicholas");

}

elseif (e.getSource() == itmExit)

{

System.exit(0);

}

elseif (e.getSource() == itmHowTo)

{

JOptionPane.showMessageDialog(null," How To Use This Program" +

"\n\nTo insert 'Supply Voltage' and 'Resistor Values' go to"+

"\nFile> Input Values\n\nThen use the 'Calculate' button to fill table"+

"\n\nUse the 'Clear' button to clear all values in table"+

"\n\n\nIMPORTANT: 'Resistor Values' must be within the E24 range");

}

elseif (e.getSource() == btnClr)

{

resistorTable.resistorClear();

}

elseif (e.getSource() == btnCalc)

{

resistorTable.resistorCalculate();

}

elseif (e.getSource() == itmInput)

{

resistorTable.resistorInput();

}

/*deleteme*/

elseif (e.getSource() == itmcheck)/*deleteme*/

{/*deleteme*/

resistorTable.check();/*deleteme*/

}/*deleteme*/

}

publicstaticvoid main(String[] args)

{

ResistorTableFrame app =new ResistorTableFrame();

app.setTitle("Resistor Evaluator");

app.setDefaultCloseOperation(ResistorTableFrame.EXIT_ON_CLOSE );

app.setSize(400, 187);

app.setResizable(false);

app.setVisible(true);

}

}

seccond file

/*

* Nicholas

* ResistorTableModel.java

*

* Assignment 1: Resistors in Parallel

*/

import javax.swing.table.AbstractTableModel;

import javax.swing.JOptionPane;

class ResistorTableModelextends AbstractTableModel

{

java.text.DecimalFormat df =new java.text.DecimalFormat("0.");//decimal format

private Float RT,R1,R2,R3,VT,V1,V2,V3,C,PT,P1,P2,P3;

private String sR1,sR2,sR3,sVT;

private Object[][] Resistor ={

{" ","Resistance","Voltage","Current","Power"},

{"Totals", RT +" Ohms", VT +" V", C +" mA", PT +" mW"},

{"R1", R1 +" Ohms", V1 +" V", C +" mA", P1 +" mW"},

{"R2", R2 +" Ohms", V2 +" V", C +" mA", P2 +" mW"},

{"R3", R3 +" Ohms", V3 +" V", C +" mA", P3 +" mW"}};

private String[] columnNamesHor={" ","Resistance","Voltage",

"Current","Power"};

private Class[] columnClasses =new Class[]

{String.class, String.class, String.class, String.class, String.class};

publicint getColumnCount()

{

return columnNamesHor.length;

}

publicint getRowCount()

{

return Resistor.length;

}

public Object getValueAt(int row,int col)

{

return Resistor[row][col];

}

publicboolean isCellEditable(int row,int col)

{

returnfalse;// All cells not editable

}

public String getColumnName(int col)

{

return columnNamesHor[col];

}

public Class getColumnClass(int col)

{// For cell renderers

return columnClasses[col];

}

publicvoid setValueAt(Object Resistor,int row,int col)

{// Set value in grid

this.fireTableCellUpdated(row, col);// Update the table model

}

publicvoid resistorCalculate()

{

JOptionPane.showMessageDialog(null,"How To Use This Program");

}

publicvoid resistorInput()

{

sVT = JOptionPane.showInputDialog("Supply Voltage:");

VT = Float.parseFloat(sVT);

}

publicvoid resistorClear()

{

RT =null;

R1 =null;

R2 =null;

R3 =null;

VT =null;

V1 =null;

V2 =null;

V3 =null;

C =null;

PT =null;

P1 =null;

P2 =null;

P3 =null;

}

publicvoid check()

{

JOptionPane.showMessageDialog(null,"variables float "+RT+" "+R1+" "+R2+" "+R3+" "+VT+" "+V1+" "+V2+" "+V3+" "+C+" "+PT+" "+P1+" "+P2+" "+P3+" "+"variables string "+sR1+" "+sR2+" "+sR3+" "+sVT);

}

}

Message was edited by:

nick_chompa

[15155 byte] By [nick_chompaa] at [2007-11-27 5:01:43]
# 1

If I understand what you are trying to do then I think you need to change your design a little.

You can't change the strings because you implemented the setValueAt(...) method incorrectly. You don't save the data in the model anywhere.

A model is used to store data. An editor is used to edit the data and a renderer is used to display (format) the data. So I would change your approach to simply use the DefaultTableModel. There is no need to create a custom model. In the model you would store the actual number data as Float values. Then you would create a custom renderer that would append to the "Ohms", "V", "mA" and "mW" suffixes to the numberic value when it is displayed. When you double click the cell to place it in edit mode only the number would be displayed so you would always be editing it in a numeric format.

Now I believe that you have a second problem in that when you change one of the values on a column you need to calculate another value. This is easily done by using a TableModelListener as demonstrated in this posting:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=566133

camickra at 2007-7-12 10:19:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

thanks for the help i fixed my setValueAt() statement and its is working like a dream

here is the soulution for anybody who is having the same or similar problems

/*

* Nicholas

* ResistorTableModel.java

*

* Assignment 1: Resistors in Parallel

*/

import javax.swing.table.AbstractTableModel;

import javax.swing.JOptionPane;

class ResistorTableModel extends AbstractTableModel

{

java.text.DecimalFormat df = new java.text.DecimalFormat("0.00"); //decimal format

private double RT,R1,R2,R3,V,I,PT,P1,P2,P3;

private String sR1,sR2,sR3,sV;

int row, col;

Object value;

private Object[][] Resistor = {

{" ","Resistance", "Voltage", "Current", "Power"},

{"Totals", RT + " Ohms", V + " V", I + " mA", PT + " mW"},

{"R1",R1 + " Ohms", V + " V", I + " mA", P1 + " mW"},

{"R2",R2 + " Ohms", V + " V", I + " mA", P2 + " mW"},

{"R3",R3 + " Ohms", V + " V", I + " mA", P3 + " mW"} };

private String[] columnNamesHor= {" ", "Resistance", "Voltage",

"Current", "Power"};

private Class[] columnClasses = new Class[]

{String.class, String.class, String.class, String.class, String.class};

public ResistorTableModel(){ }

public int getColumnCount()

{

return columnNamesHor.length;

}

public int getRowCount()

{

return Resistor.length;

}

public Object getValueAt(int row, int col)

{

return Resistor[row][col];

}

public boolean isCellEditable(int row, int col)

{

return false; // All cells not editable

}

public String getColumnName(int col)

{

return columnNamesHor[col];

}

public Class getColumnClass(int col)

{// For cell renderers

return columnClasses[col];

}

public void setValueAt() //move ints to globals

{ // Set value in grid

Resistor[row][col] = value;

fireTableCellUpdated(row, col);// Update the table model

}

// =============== ACTIONS ====================

public void resistorInput()

{

sV = JOptionPane.showInputDialog("Supply Voltage:");

V = Double.parseDouble(sV);

fillTable();

sR1 = JOptionPane.showInputDialog("Resistor 1:");

R1 = Double.parseDouble(sR1);

fillTable();

sR2 = JOptionPane.showInputDialog("Resistor 2:");

R2 = Double.parseDouble(sR2);

fillTable();

sR3 = JOptionPane.showInputDialog("Resistor 3:");

R3 = Double.parseDouble(sR3);

fillTable();

}

public void resistorCalculate()

{

RT = (R1+R2+R3);

I = (V/RT);

P1 = (V*V/R1);

P2 = (V*V/R2);

P3 = (V*V/R3);

PT = (P1+P2+P3);

fillTable();

}

public void resistorClear()

{

RT = 0;

R1 = 0;

R2 = 0;

R3 = 0;

V = 0;

I = 0;

PT = 0;

P1 = 0;

P2 = 0;

P3 = 0;

fillTable();

}

public void fillTable()

{

row = 1; //set row altered

col = 1; //set column altered

value = RT + " Ohms"; //set value

setValueAt();//set value in cell

row = 2; //set row altered

col = 1; //set column altered

value = R1 + " Ohms"; //set value

setValueAt();//set value in cell

row = 3; //set row altered

col = 1; //set column altered

value = R2 + " Ohms"; //set value

setValueAt();//set value in cell

row = 4; //set row altered

col = 1; //set column altered

value = R3 + " Ohms"; //set value

setValueAt();//set value in cell

row = 1; //set row altered

col = 2; //set column altered

value = V + " V"; //set value

setValueAt();//set value in cell

row = 2; //set row altered

col = 2; //set column altered

value = V + " V"; //set value

setValueAt();//set value in cell

row = 3; //set row altered

col = 2; //set column altered

value = V + " V"; //set value

setValueAt();//set value in cell

row = 4; //set row altered

col = 2; //set column altered

value = V + " V"; //set value

setValueAt();//set value in cell

row = 1; //set row altered

col = 3; //set column altered

value = I + " mA"; //set value

setValueAt();//set value in cell

row = 2; //set row altered

col = 3; //set column altered

value = I + " mA"; //set value

setValueAt();//set value in cell

row = 3; //set row altered

col = 3; //set column altered

value = I + " mA"; //set value

setValueAt();//set value in cell

row = 4; //set row altered

col = 3; //set column altered

value = I + " mA"; //set value

setValueAt();//set value in cell

row = 1; //set row altered

col = 4; //set column altered

value = PT + " mW"; //set value

setValueAt();//set value in cell

row = 2; //set row altered

col = 4; //set column altered

value = P1 + " mW"; //set value

setValueAt();//set value in cell

row = 3; //set row altered

col = 4; //set column altered

value = P2 + " mW"; //set value

setValueAt();//set value in cell

row = 4; //set row altered

col = 4; //set column altered

value = P3 + " mW"; //set value

setValueAt();//set value in cell

}

}

nick_chompaa at 2007-7-12 10:19:19 > top of Java-index,Desktop,Core GUI APIs...