1 package jalview.fts.core;
3 import java.awt.Component;
4 import java.text.DecimalFormat;
6 import javax.swing.JLabel;
7 import javax.swing.JTable;
8 import javax.swing.table.DefaultTableCellRenderer;
11 * The class to handle the formatting of the double values for JTable cells.
13 public class DecimalFormatTableCellRenderer extends
14 DefaultTableCellRenderer
16 private DecimalFormat formatter;
18 public DecimalFormatTableCellRenderer(boolean isFormated,
19 int significantFigures)
21 String integerFormater = isFormated ? "###,##0" : "0";
22 String fractionFormater = isFormated ? "###,##0." : "0.";
23 if (significantFigures > 0)
25 StringBuilder significantFigureBuilder = new StringBuilder();
26 for (int x = 1; x <= significantFigures; ++x)
28 significantFigureBuilder.append("0");
30 formatter = new DecimalFormat(fractionFormater
31 + significantFigureBuilder.toString());
35 formatter = new DecimalFormat(integerFormater);
37 super.setHorizontalAlignment(JLabel.RIGHT);
40 public DecimalFormatTableCellRenderer()
42 super.setHorizontalAlignment(JLabel.RIGHT);
46 public Component getTableCellRendererComponent(JTable table,
47 Object value, boolean isSelected, boolean hasFocus, int row,
55 value = formatter.format(value);
57 return super.getTableCellRendererComponent(table, value, isSelected,
58 hasFocus, row, column);