0
I am making a Cellrender with currency format. I placed 13 columns in a jtable, one has the recipes (Salary, 13th salary, Overtime).. and other 12 with the months. each month the person will enter this information and then I will add another jtable with the expenses. someone has an example of Cellrender for that need?
I found this example that suits me well, but I’m not able to use it for a jframe and a defaulttablemode that pulls information from the bank.
import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.table.*;
public class EditorTest {
private JScrollPane getTableComponent() {
String[] colNames = {
"Preço", "Número"
};
final Object[][] data = {
{new Double(2), Double.valueOf(12.21)},
{Double.valueOf(12.21), Double.valueOf(12.21)},
{Double.valueOf(12.21), Double.valueOf(12.21)},
{Double.valueOf(12.21), Double.valueOf(12.21)}
};
DefaultTableModel model = new DefaultTableModel(data, colNames) {
public Class getColumnClass(int col) {
return data[0][col].getClass();
}
};
JTable table = new JTable(model);
TableColumnModel colModel = table.getColumnModel();
//colunas
colModel.getColumn(0).setCellRenderer(new DoubleRenderer());
colModel.getColumn(1).setCellRenderer(new DoubleRenderer());
table.setCellSelectionEnabled(true);
Dimension d = table.getPreferredSize();
table.setPreferredScrollableViewportSize(d);
return new JScrollPane(table);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new EditorTest().getTableComponent());
f.pack();
f.setLocation(100, 100);
f.setVisible(true);
}
}
class DoubleRenderer extends DefaultTableCellRenderer {
NumberFormat numeroFormatado = NumberFormat.getCurrencyInstance();
public DoubleRenderer() {
setHorizontalAlignment(RIGHT);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
setText(numeroFormatado.format(((Double) value).doubleValue()));
return this;
}
}
See the recent issue in the reply.
– user28595