How to apply Font color to specific cells on JTable that uses DefaultTableModel

孤者浪人 提交于 2019-12-06 14:47:02
mKorbel
  • easiest of ways is look at prepareRenderer(), best of all is @camickr Table Row Rendering

  • JTable is View, based on TableModel, in most cases you have to convert the view against model converXxxToXxx from inside of prepareRenderer or getTableCellRendererComponent, because JTable could be sorted of filtered

  • methods

code

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);

returns access to the specific cell in JTable - (TableCellRenderer renderer, int row, int column)

Use DefaultTableCellRenderer, then you can use setForeground() and setBackground().

refer to the page.. http://www.jyloo.com/news/?pubId=1282737395000

or see this example...

/*This is the raw code I have written.*/ 
JTable Tbl=new JTable(2,2){ 
    DefaultTableCellRenderer colortext=new DefaultTableCellRenderer();
    {
        colortext.setForeground(Color.RED);
    }
    @Override
    public TableCellRenderer getCellRenderer(int arg0, int arg1) {
        return colortext;
    }
};

I believe you can specify this behavior in

TableCellRenderer.getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, 
                        int row, int column)

method of the table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!