I'm making a coin-shaped CellRender. I put 13 columns in a jtable, one has the recipes (Salary, 13th salary, Extra hour) .. and outas 12 with the months. every month the person will type this information and then I will add another jtable with the expenses. does anyone have an example of CellRender for this need?
IfoundthisexamplethatservesmewellbutI'mnotabletouseitforajframeandadefaulttablemodethatpullinformationfromthedatabase.
importjava.awt.*;importjava.text.NumberFormat;importjavax.swing.*;importjavax.swing.table.*;publicclassEditorTest{privateJScrollPanegetTableComponent(){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;
}
}