KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA table list

Best books about JAVA programming:

JAVA JTable

The following code shows the last row in a Jtable dynamiclly sum column values. This example needs 2 files: EachRowEditor.java and TotalRowExample.java

//Example modified from http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html
//Original example doesn't work
/**
* @version 1.0 12/03/98
*/


/*
* (swing1.1beta3) swing#960
*/

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

/**
* @version 1.0 12/03/98
*/

class DecimalRenderer extends DefaultTableCellRenderer {
DecimalFormat formatter;

DecimalRenderer(String pattern) {
this(new DecimalFormat(pattern));
}

DecimalRenderer(DecimalFormat formatter) {
this.formatter = formatter;
setHorizontalAlignment(JLabel.RIGHT);
}

public void setValue(Object value) {
setText((value == null) ? ""
: formatter.format(((Double)value).doubleValue()));
}

}

public class TotalRowExample extends JFrame {
final private int TOTAL_ROW = 3;
final private int TOTAL_COLUMN = 1;

TotalRowExample() {
super( "Total Row Example" );

final DecimalFormat formatter = new DecimalFormat("###,##0.00");
DefaultTableModel dm = new DefaultTableModel() {
public void setValueAt(Object value, int row, int col) {
Vector rowVector = (Vector)dataVector.elementAt(row);
if (col == TOTAL_COLUMN) {
Double d = null;
if (value instanceof Double) {
d = (Double)value;
} else {
try {
d = new Double(
((Number)formatter.parse((String)value)).doubleValue());
} catch (ParseException ex) {
d = new Double(0.0);
}
}
rowVector.setElementAt(d, col);
} else {
rowVector.setElementAt(value, col);
}
}

public boolean isCellEditable(int row, int col) {
if (row == TOTAL_ROW) return false;
return true;
}

public Class getColumnClass(int col) {
if (col == TOTAL_COLUMN) return Number.class;
return String.class;
}
};

dm.setDataVector(
new Object[][]{
{"coffee",new Double(0.0)},
{"tea" ,new Double(0.0)},
{"cocoa" ,new Double(0.0)},
{"total" ,new Double(0.0)}},
new Object[]{"Item","Price"});

JTable table = new JTable( dm ) {
public void editingStopped(ChangeEvent e) {
super.editingStopped(e);
reCalcurate(getModel());
repaint();
}
};

table.getColumn("Price").setCellRenderer(
new DecimalRenderer(formatter));
EachRowEditor rowEditor=new EachRowEditor (table);
table.getColumn("Price").setCellEditor(rowEditor);
JScrollPane scroll = new JScrollPane(table);
Container content = getContentPane();
content.add(scroll);
setSize( 300, 120 );
setVisible(true);
}

private void reCalcurate(TableModel ml) {
if (ml == null) return;
double total = 0.0;
for (int i=0;i<TOTAL_ROW;i++) {
total += ((Double)ml.getValueAt(i,TOTAL_COLUMN)).doubleValue();
}
ml.setValueAt(new Double(total),TOTAL_ROW,TOTAL_COLUMN);
}

public static void main(String[] args) {
TotalRowExample frame = new TotalRowExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}

 


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer