Forums Register Login

How to do add/delete operations in Cells in Swing table

+Pie Number of slices to send: Send
Hi guys , I tired to make one very useful class to implement +/- operation in a cell of Swing table. I hope it will be useful for you guys.
public class JTableSample exetends JFrame
{
.
.
public void setValueAt(Object value, int row, int column)
{
// write your formula identification code here
Formula.ComputeFormula(this, strValue);
}
}
class Formula
{
public Formula()
{
}
static String ComputeFormula(TableModel table, String strFormula)
{
strFormula = strFormula.substring(1);
String s;
try
{
StringTokenizer st = new StringTokenizer(strFormula, "+-", true);
int count = st.countTokens();
if(count != 3)
{
String s1 = "BAD";
return s1;
}
String lhand = st.nextToken();
String op = st.nextToken();
String rhand = st.nextToken();
Cell lCell = GetCellAt(table, lhand);
Cell rCell = GetCellAt(table, rhand);
double dLVal = Double.parseDouble(lCell.getValue().toString());
double dRVal = Double.parseDouble(rCell.getValue().toString());
double dRetVal;
if(op.equals("+"))
dRetVal = dLVal + dRVal;
else
if(op.equals("-"))
{
dRetVal = dLVal - dRVal;
} else
{
String s2 = "BAD OP";
return s2;
}
String s3 = Double.toString(dRetVal);
return s3;
}
catch(Exception e)
{
s = "Cannot Parse the formula";
}
return s;
}
[ January 07, 2003: Message edited by: Yasin Kazi ]
[ February 10, 2003: Message edited by: Yasin Kazi ]
[ February 15, 2003: Message edited by: Yasin Kazi ]
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1155 times.
Similar Threads
String Tokenizer Problem
Need help with read method
Comparing two dates
Test Input Calculator Help
Another method of parsing a command line string
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:02:15.