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

