[英]Get JTable Integer cell values with cell formatting
我有一個 JTable 列,其中包含 integer 值,這些值的格式設置為添加千位分隔符。 我可以使用model.getValueAt()
或table.getValueAt()
獲取列的值,但這些值沒有格式。 如何獲取具有格式的單元格值?
格式化單元格的代碼:
table.getColumnModel().getColumn(5).setCellRenderer(new NumberTableCellRenderer());
處理單元格格式的 class 的代碼:
public static class NumberTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Number) {
value = NumberFormat.getNumberInstance().format(value);
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
如何獲取具有格式的單元格值?
您需要重新應用渲染邏輯。 就像是:
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(renderer, row, column);
if (c instanceof JLabel)
{
JLabel label = (JLabel)c;
String formatted = label.getText();
}
但是,這只會為您提供 String 值。 它不會得到任何顏色渲染,因為字符串不包含這樣的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.