英文:
JTable how can i create custom column renderer
问题
static class DateRenderer extends DefaultTableCellRenderer {
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
public DateRenderer() { super(); }
public void setValue(Object value) {
if (formatter==null) {
formatter = DateFormat.getDateInstance();
}
setText((value == null) ? "" : formatter.format(value));
}
}
我使用这段代码来渲染日期。我找到了这段代码,但现在我需要另外一种功能。我有一个包含数字的列,格式如下(1234.56)。我希望将这些数字渲染为(1234.56 TL)。但我是一个初学者,所以找不到实现的方法。
英文:
static class DateRenderer extends DefaultTableCellRenderer {
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
public DateRenderer() { super(); }
public void setValue(Object value) {
if (formatter==null) {
formatter = DateFormat.getDateInstance();
}
setText((value == null) ? "" : formatter.format(value));
}
}
I use this code to render dates. I found it as this but now i need something else. I have a column with numbers as this (1234.56). I want to render the numbers as (1234.56 TL). But i am kinda beginner so i can't find the way to do it.
答案1
得分: 1
TableColumnModel m = table.getColumnModel();
m.getColumn(5).setCellRenderer(new TableRendererExample());
class TableRendererExample extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column == 5) {
setText(value.toString() + " TL");
}
return this;
}
}
This code works fine for answer.
<details>
<summary>英文:</summary>
TableColumnModel m = table.getColumnModel();
m.getColumn(5).setCellRenderer(new TableRendererExample());
class TableRendererExample extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column == 5) {
setText(value.toString()+" TL");
}
return this;
}
}
This code works fine for answer.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论