JTable如何创建自定义列渲染器

huangapple go评论77阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年8月24日 13:11:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定