Results 1 to 11 of 11
- 06-07-2012, 09:25 PM #1
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
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
now i want help in 2 thingsJava Code:String[] columnNames = { "SNO", "NAME", "Class1", "Class2", "class3", "class4", "Total" };
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:-
so please tell me what i can do to make such calculations in run time for my problem....please...Java 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 ); }
please do help me in this asap...Last edited by danpotter; 06-07-2012 at 09:53 PM.
-
Re: adding values in JTable and print total help
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.
The TableModel interface has methods that will help you do this: getValueAt(....) and setValueAt(...) Please check these out and give them a try.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
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.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:-
so please tell me what i can do to make such calculations in run time for my problem....please...Java 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 ); }
please do help me in this asap...
- 06-08-2012, 10:35 PM #3
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: adding values in JTable and print total help
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
basing on that i need help...Java 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];
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
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.
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.i got my code like this way
basing on that i need help...Java 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];
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...
- 06-09-2012, 07:22 PM #5
Re: adding values in JTable and print total help
Cross posted without acknowledgement of help received here
JTable Runtime Addition (Swing / AWT / SWT / JFace forum at JavaRanch)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-09-2012, 07:36 PM #6
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
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
- 06-09-2012, 08:33 PM #8
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: adding values in JTable and print total help
well,i understood something from tutorial..and i wrote a code,please do check this...
getting error here model=new DefaultTableModel(data,columnNames) ,..please chk once...Java 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); } }); } }Last edited by danpotter; 06-09-2012 at 08:35 PM.
-
Re: adding values in JTable and print total help
Please post complete error message.
- 06-09-2012, 11:49 PM #10
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: adding values in JTable and print total help
at daily.<init>(daily.java:52) is nothing but model=new DefaultTableModel(data,columnNames)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)
-
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.
Similar Threads
-
Adding print streams together
By trishtren in forum Advanced JavaReplies: 1Last Post: 04-22-2012, 02:15 AM -
Adding print streams
By trishtren in forum New To JavaReplies: 0Last Post: 04-21-2012, 07:44 PM -
How to traverse the svg tag and print the particular values in the tag
By Hemanth Kumar in forum Advanced JavaReplies: 0Last Post: 04-16-2012, 08:35 AM -
hi i need help with adding the total in this java ready to program IDE.
By cold in forum New To JavaReplies: 1Last Post: 03-29-2010, 01:07 AM -
How to use Jtable to display File System as Total Commander?
By antiZzz in forum Advanced JavaReplies: 1Last Post: 01-16-2009, 05:44 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks