/*
* Created on 28.12.2004@14:23:47
*
* TODO Licence info
*/
package de.tutorials;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
/**
* @author Administrator
*
* TODO Explain what I do...
*/
public class JTableExample extends JFrame {
private JTable table;
private TableCellRenderer oldRenderer;
public JTableExample() {
super("");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Vector rows = new Vector();
Vector row0 = new Vector();
row0.add(Double.valueOf(2.334865D));
row0.add(Double.valueOf(20.33477D));
row0.add(Double.valueOf(200.3345D));
Vector row1 = new Vector();
row1.add(Double.valueOf(2.334865D));
row1.add(Double.valueOf(200.33477D));
row1.add(Double.valueOf(20.3345D));
Vector row2 = new Vector();
row2.add(Double.valueOf(200.334865D));
row2.add(Double.valueOf(20.33477D));
row2.add(Double.valueOf(2000.3345D));
rows.add(row0);
rows.add(row1);
rows.add(row2);
Vector headers = new Vector();
headers.add("S0");
headers.add("S1");
headers.add("S2");
table = new JTable(rows, headers);
oldRenderer = table.getDefaultRenderer(Object.class);
table.setDefaultRenderer(Object.class, new TableCellRenderer() {
private JLabel jlbl = new JLabel() {
{
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
};
private DecimalFormat df = new DecimalFormat("#.00");
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
// TODO Auto-generated method stub
if (!(value instanceof Double))
return oldRenderer.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
Double d = (Double) value;
String str = df.format(d.doubleValue());
jlbl.setText(str);
return jlbl;
}
});
getContentPane().add(new JScrollPane(table));
pack();
setVisible(true);
}
public static void main(String[] args) {
new JTableExample();
}
}