java


how to calculate total of column in JTable but started at particular index


I have Amount column in JTable and one 'Total' and 'Final Total' button. So if I put 2 and 3 in amount column and hit 'Total' then i got 5 in another cell of same column.(My programm is worked fine till this step ) Now I want to insert 3 and 4 below 5. I inserted and hit 'Final Total' then want to display only 12(5+3+4) in same column(Amount) at the last row. (5 is the answer which i got hitting total).
Here I paste my code of 'total' and 'Final Total' button. I am not sure about for loop using in 'Final Total' button. So please give solution for it.
JButton total = new JButton("Total");
total.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
flag=1;
add.setEnabled(false);
Object source = ae.getSource();
if (source == total)
{
model.addRow(new Object[]{new Integer(0),"TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
for( i = 0; i < model.getRowCount(); i++)
{
amount = Double.parseDouble(model.getValueAt(i, 6).toString());
sum = new Double(sum+amount);
}
model.setValueAt(sum,model.getRowCount()-1,6);
}
JOptionPane.showMessageDialog(null,"If you pressed Total then you can't select 'Add Row' button ");
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
}
});
JButton btnFinalTotal = new JButton("Final total");
btnFinalTotal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
flag=2;
Object source = ae.getSource();
if (source == btnFinalTotal)
{
d= Double.parseDouble(model.getValueAt(i,6).toString());
System.out.println("Current value of d is " +d);
model.addRow(new Object[]{new Integer(0),"FINAL TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
for(i=0;i>d;i++)
{
amount = Double.parseDouble(model.getValueAt(i, 6).toString());
finalamount= new Double(d+amount);
}
model.setValueAt(finalamount,model.getRowCount()-1,6);
}
}
});
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
System.out.println("Cell " + e.getFirstRow() + ", "
+ e.getColumn() + " changed. The new value: "
+ table.getModel().getValueAt(e.getFirstRow(),
e.getColumn()));
int row = e.getFirstRow();
int column = e.getColumn();
if(flag==1)
{
if(column == 6)
{
// taxonamt= Double.parseDouble(model.getValueAt(row, 6).toString());
sum=Double.parseDouble(model.getValueAt(row, 6).toString());
// Double total = new Double (sum*(taxonamt/100));
Double total = new Double (sum);
model.setValueAt(total,model.getRowCount()-1,7);
}
}
}
}
You need to place some kind of "marker" into your data which designates a row as a "total" row, so you can find the last "total" row and calculate the rows below it.
Personally, I'd start with a plain old java object (POJO) which encapsulate the functionality you're aiming for and build the table model around it

Related Links

Is there a difference in terms of algorithm efficiency between subtracting ASCII values and simply subtracting an integer?
Sockets peer-to-peer
Proxy Pattern: how is it more efficent that creating the real object?
how to set frangmenttabhost Tabs at the middle of the screen
How enable hardware acceleration in vlcj?
How to Upload Files with WebView in Android Studio?
Error with BSTree insert
How to Calculate the factorial of a decimal number in Java?
Upload information from XML to Java and HTML
Why are my objects not keeping the data they are constructed with?
Can't read an enum type .name() into a jsp: uncaught reference error
Unable to get javac to automatically recompile a source file
ExecutorService with Runnable share CyclicBarrier
how to get the value at specific column and specific row in jtable by a mouse clicked
Java Best way to parse quarterly dates
Finding and replacing specified character in java

Categories

HOME
date
gremlin
drivers
mfc
hashmap
raspberry-pi
session
grep
bpmn
alfresco
kalman-filter
bookshelf.js
programming-languages
elm
microservices
sqlite3
fsm
postgres-xl
worldwind
evopdf
devops
format-specifiers
restful-authentication
angular2-aot
fluentvalidation
jspm
libuv
webkitspeechrecognition
r-raster
galsim
http-get
catch-all
hybridauth
geopositioning
http-digest
retina-display
lto
gesture
streamsets
pdf-reactor
nomethoderror
outlook-api
slick-3.0
google-datalayer
fault
jquery-validate
dotnetzip
libusb-win32
akka-cluster
arrow-keys
iso8601
spring-mongodb
spring-cache
orthogonal
chord-diagram
pycaffe
abcpdf9
android-fonts
android-textview
xml-attribute
qpid
make-install
sdf
captivenetwork
design-by-contract
system.management
maven-tomcat-plugin
intel-fortran
sdhc
wireshark-dissector
security-testing
dukescript
responsive-images
truevault
formatjs
facebook-graph-api-v2.4
dia
offloading
operator-precedence
oxwall
coldbox
tt-news
fpml
nsbutton
client-side-templating
expected-exception
mechanize-ruby
meteor-velocity
prettify
apc
orchardcms-1.7
sabredav
android-hardware
armcc
netbeans-6.9
removeclass
krl
oncheckedchanged
sql-server-profiler
xfbml
w3wp.exe
cxxtest
blitz++
.nettiers
icanhaz.js
project-hosting
caching-application-block
uiq3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile