adding values in JTable and print total help
well,im working on a staff attendance system..,and i just need to add the total number of days they have presented
i created a JTable and it has columns
Code:
String[] columnNames =
{
"SNO",
"NAME",
"Class1",
"Class2",
"class3",
"class4",
"Total"
};
now i want help in 2 things
1) creating rows in runtime
2)Adding values that entered in rows of class1,class2,class3,class4[the entered data will be in integer form] and all sum will be printed automatically in last row[cell]in Total
in case of compile time,we can do calculations something like..for example,lets take 3 columns and 3 rows,we can perform multiplication like below:-
Code:
Object[][] data = new Object[][]
{
{ new Double( 0.5 ), new Double( 1.2 ), new Double( 0.6 ) },
{ new Double( 0.4 ), new Double( 1.3 ), new Double( 0.52 ) },
{ new Double( 0.3 ), new Double( 1.4 ), new Double( 0.42 ) }
};
// and for addition we use
Object valueA = model.getValueAt( row, 0 );
Object valueB = model.getValueAt( row, 1 );
double valueC;
if ( valueA instanceof Double
&& valueB instanceof Double )
{
valueC = (Double) valueA * (Double) valueB;
}
else
{
valueC = Double.parseDouble( String.valueOf( valueA ) ) * Double.parseDouble( String.valueOf( valueB ) );
}
model.setValueAt( valueC, row, 2 );
}
so please tell me what i can do to make such calculations in run time for my problem....please...
please do help me in this asap...
Re: adding values in JTable and print total help
Quote:
Originally Posted by
danpotter
well,im working on a staff attendance system..,and i just need to add the total number of days they have presented
i created a JTable and it has columns
Code:
String[] columnNames =
{
"SNO",
"NAME",
"Class1",
"Class2",
"class3",
"class4",
"Total"
};
now i want help in 2 things
1) creating rows in runtime
First off, use a DefaultTableModel object for your table's model, and actually there is a good chance that you're already doing this. Extract your JTable's model by calling getModel() on it, and then check to see if it is a DefaultTableModel object by calling getClass() on the model, and then getName() on the class, and print that out. If it is this type of model, then you can easily add rows by calling the addRow(...) method. The DefaultTableModel will show you the two possible ways to do this. If your attempt doesn't work, please come on back and show us what you've tried, and let's see if we can help you some more.
Quote:
2)Adding values that entered in rows of class1,class2,class3,class4[the entered data will be in integer form] and all sum will be printed automatically in last row[cell]in Total
The TableModel interface has methods that will help you do this: getValueAt(....) and setValueAt(...) Please check these out and give them a try.
Quote:
in case of compile time,we can do calculations something like..for example,lets take 3 columns and 3 rows,we can perform multiplication like below:-
Code:
Object[][] data = new Object[][]
{
{ new Double( 0.5 ), new Double( 1.2 ), new Double( 0.6 ) },
{ new Double( 0.4 ), new Double( 1.3 ), new Double( 0.52 ) },
{ new Double( 0.3 ), new Double( 1.4 ), new Double( 0.42 ) }
};
// and for addition we use
Object valueA = model.getValueAt( row, 0 );
Object valueB = model.getValueAt( row, 1 );
double valueC;
if ( valueA instanceof Double
&& valueB instanceof Double )
{
valueC = (Double) valueA * (Double) valueB;
}
else
{
valueC = Double.parseDouble( String.valueOf( valueA ) ) * Double.parseDouble( String.valueOf( valueB ) );
}
model.setValueAt( valueC, row, 2 );
}
so please tell me what i can do to make such calculations in run time
for my problem....please...
please do help me in this asap...
Please avoid asking for help "asap" as this implies that you believe your question to be more important than the other questions here. Please understand that it isn't.
Re: adding values in JTable and print total help
Quote:
Originally Posted by
Fubarable
The TableModel interface has methods that will help you do this: getValueAt(....) and setValueAt(...) Please check these out and give them a try.
well,I got some idea about adding new rows,..but please give a clear view about addition of data in cells with code,as im really newbie in java professional programming...,
i got my code like this way
Code:
Object[][] data;
String[] columnNames=new String[13];
columnNames=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","Total"};
data=new Object[70][13];
basing on that i need help...
and sorry for using ASAP in my previous post,as i need to submit my Proj by 15th,im in little bit hurry..please dont mind...
Re: adding values in JTable and print total help
Quote:
Originally Posted by
danpotter
well,I got some idea about adding new rows,..but please give a clear view about addition of data in cells with code,as im really newbie in java professional programming...,
I would use a for loop to loop through the rows of your TableModel and then call the TableModel method, getValueAt(...) method inside of the for loop to extract that row's data in the column of interest. Try it, you'll likely get it right, and even if you don't you won't break your computer. Just be sure to check the DefaultTableModel and TableModel API for this method and the setValueAt(...) before using them.
Quote:
i got my code like this way
Code:
Object[][] data;
String[] columnNames=new String[13];
columnNames=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","Total"};
data=new Object[70][13];
basing on that i need help...
and sorry for using ASAP in my previous post,as i need to submit my Proj by 15th,im in little bit hurry..please dont mind...
I understand, but please understand that this is your issue, not ours. We are volunteers helping on our free time, and to us *all* questions are equally important. I'm sure you understand.
Re: adding values in JTable and print total help
Re: adding values in JTable and print total help
hey im sorry,actually i created same topic on both sites so that i can receive help in any of site and i dont know same members will be on both sites,and i dont mean to cheat anyone with fake names,,..java potter is my another username but i dont mean any wrong..,anyways...thanks for link....
Re: adding values in JTable and print total help
Quote:
Originally Posted by
danpotter
hey im sorry,actually i created same topic on both sites so that i can receive help in any of site and i dont know same members will be on both sites,
The issue has *nothing* to do with that. Please read the link that Darryl posted in your cross-post before making these erroneous statements.
Re: adding values in JTable and print total help
well,i understood something from tutorial..and i wrote a code,please do check this...
Code:
import java.awt.Container;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;
import java.awt.Dimension;
import java.lang.Double;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class daily extends JFrame implements CellEditorListener {
protected Container container;
protected JPanel panel;
protected JTable jtable;
TableModel model;
protected JScrollPane scrollingArea;
public daily()
{
double[][] data;
String[] columnNames=new String[13];
columnNames=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13"};
data=new double[70][13];
/*Double[][] temp=new Object[data.length+1][13];
for(int i=0;i<data.length;i++)
{
for(int j=0;j<13;j++)
{
temp[i][j]=data[i][j];
}
}*/
//jtable.setModel(new DefaultTableModel(data,columnNames));
model=new DefaultTableModel(data,columnNames)
{
@Override
public boolean isCellEditable(int row,int col )
{
return true;
}
};
jtable = new JTable( model );
jtable.getCellEditor( 0, 0 ).addCellEditorListener( this );
Double[][] temp=new Double[data.length][columnNames.length];
for(int i=0;i<data.length;i++)
{
for(int j=0;j<columnNames.length;j++)
{
temp[i][j]=new Double(data[i][j]);
}
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width, screenSize.height);
container = getContentPane();
jtable.setBorder(BorderFactory.createLineBorder(Color.pink,2));
jtable.setFont(new Font("Times New Roman",Font.PLAIN,18));
jtable.setRowHeight(20);
jtable.setGridColor(Color.blue);
Dimension dim = new Dimension(screenSize.height,600);
jtable.setPreferredScrollableViewportSize(dim);
panel = new JPanel();
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jtable.getColumnModel().getColumn(0).setPreferredWidth(30);
scrollingArea = new JScrollPane(jtable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollingArea);
container.add(panel);
}
public void editingStopped( ChangeEvent e )
{
int row = jtable.getSelectedRow();
// Here we calculate new result for column 'C' in 'row'
Double valueA = (Double) model.getValueAt( row, 0 );
Double valueB = (Double) model.getValueAt( row, 1 );
Double valueC = (Double) model.getValueAt( row, 2 );
Double valueD = (Double) model.getValueAt( row, 3 );
Double valueE = (Double) model.getValueAt( row, 4 );
Double valueF = (Double) model.getValueAt( row, 5 );
Double valueG = (Double) model.getValueAt( row, 6 );
Double valueH = (Double) model.getValueAt( row, 7 );
Double valueI = (Double) model.getValueAt( row, 8 );
Double valueJ = (Double) model.getValueAt( row, 9 );
Double valueK = (Double) model.getValueAt( row, 10 );
Double valueL = (Double) model.getValueAt( row, 11 );
Double valueM = (Double) model.getValueAt( row, 12 );
double valueN;
if ( valueA instanceof Double
&& valueB instanceof Double && valueC instanceof Double && valueD instanceof Double && valueE instanceof Double && valueF instanceof Double && valueG instanceof Double && valueH instanceof Double && valueI instanceof Double && valueJ instanceof Double && valueK instanceof Double && valueL instanceof Double && valueM instanceof Double
)
{
valueN = (Double) valueA + (Double) valueB +(Double) valueC + (Double) valueD +(Double) valueE + (Double) valueF +(Double) valueG + (Double) valueH +(Double) valueI + (Double) valueJ + (Double) valueK + (Double) valueL + (Double) valueM;
}
else
{
valueN = Double.parseDouble( String.valueOf( valueA ) ) + Double.parseDouble( String.valueOf( valueB )) + Double.parseDouble( String.valueOf( valueC ) ) + Double.parseDouble( String.valueOf( valueD )) + Double.parseDouble( String.valueOf( valueE ) ) + Double.parseDouble( String.valueOf( valueF ) )+ Double.parseDouble( String.valueOf( valueG ) ) + Double.parseDouble( String.valueOf( valueH ) )+ Double.parseDouble( String.valueOf( valueI ) ) + Double.parseDouble( String.valueOf( valueJ ) )+ Double.parseDouble( String.valueOf( valueK ) ) + Double.parseDouble( String.valueOf( valueL ))+
Double.parseDouble( String.valueOf( valueM ));
}
model.setValueAt( valueN, row, 12 );
}
public void editingCanceled( ChangeEvent e )
{
}
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
daily da= new daily();
da.setVisible(true);
da.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
getting error here model=new DefaultTableModel(data,columnNames) ,..please chk once...
Re: adding values in JTable and print total help
Please post complete error message.
Re: adding values in JTable and print total help
Quote:
Originally Posted by
Fubarable
Please post complete error message.
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: constructor DefaultTableModel(double[][],java.lang.String[])
location: class javax.swing.table.DefaultTableModel
at daily.<init>(daily.java:52)
at daily$2.run(daily.java:155)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 598)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
at daily.<init>(daily.java:52) is nothing but model=new DefaultTableModel(data,columnNames)
Re: adding values in JTable and print total help
The error message is telling you what's wrong: Your DefaultTableModel constructor is faulty. It must take a 2D array of some type of object, not of a primitive for its first parameter. Double[][] would work, but double[][] won't. Also, don't try to run code that doesn't compile, but first fix the compilation errors. Also, be sure to refer to the Java API often.