2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.fts.core;
23 import java.awt.Component;
24 import java.text.DecimalFormat;
26 import javax.swing.JLabel;
27 import javax.swing.JTable;
28 import javax.swing.table.DefaultTableCellRenderer;
31 * The class to handle the formatting of the double values for JTable cells.
33 public class DecimalFormatTableCellRenderer extends
34 DefaultTableCellRenderer
36 private DecimalFormat formatter;
38 public DecimalFormatTableCellRenderer(boolean isFormated,
39 int significantFigures)
41 String integerFormater = isFormated ? "###,##0" : "0";
42 String fractionFormater = isFormated ? "###,##0." : "0.";
43 if (significantFigures > 0)
45 StringBuilder significantFigureBuilder = new StringBuilder();
46 for (int x = 1; x <= significantFigures; ++x)
48 significantFigureBuilder.append("0");
50 formatter = new DecimalFormat(fractionFormater
51 + significantFigureBuilder.toString());
55 formatter = new DecimalFormat(integerFormater);
57 super.setHorizontalAlignment(JLabel.RIGHT);
60 public DecimalFormatTableCellRenderer()
62 super.setHorizontalAlignment(JLabel.RIGHT);
66 public Component getTableCellRendererComponent(JTable table,
67 Object value, boolean isSelected, boolean hasFocus, int row,
75 value = formatter.format(value);
77 return super.getTableCellRendererComponent(table, value, isSelected,
78 hasFocus, row, column);