更改JTable单元格的颜色 [英] Changing JTable cell color

查看:163
本文介绍了更改JTable单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道,要用JTable来改变表格单元格的格式,我必须使用我自己的渲染器。但是,我似乎无法正确实施。



这是我目前的设置:

  public class MyClass 
{
public static void main(String args [])
{
JTable myTable = new JTable(10,10);
myTable.setDefaultRenderer([我不知道在这里放什么],新的CustomRenderer());



class CustomRenderer extends DefaultTableCellRenderer
{
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);

//格式化
return c;


$ / code $ / pre

我需要使用什么第一个参数 setDefaultRenderer ? API只是说'班'。我不知道该把什么放在那里。



有人可以用最简单的方式解释我如何去实现这个吗?请提供一个例子,说明如何在 main()方法中更改格式。

解决方案在 setDefaultRenderer 的第一个参数中,为要覆盖呈现的类放置类文字。也就是说,如果你的数据包含所有的字符串,你可以把

  myTable.setDefaultRenderer(String.class,new CustomRenderer()) ;如果你的数据也是由 BigDecimal 或 Integer 作为类,您必须为每个类类型( BigDecimal.class > Integer.class )。

最后,在渲染器中更改背景颜色:

 类CustomRenderer扩展了DefaultTableCellRenderer 
{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus,int row,int column)
{
组件c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
c.setBackground(new java.awt.Color(255,72,72));
返回c;






如果你编写一个适用于接口的所有类,您还需要修改表模型 getColumnClass 函数并让它返回接口实现这个接口的所有对象的类:

  public Class <?扩展对象> getColumnClass(int c){
Object object = getValueAt(0,c);
if(object == null){
return Object.class;
if(getValueAt(0,c)instanceof IColorable){
return ICarPart.class;
} else {
return getValueAt(0,c).getClass();




$ b $ p
$ b $ p $可以为IColorable.class注册一个渲染器并且不需要为每个实现注册单独的渲染器。


This is driving me absolutely insane.

I know that, to change the formatting of table cells with JTable, I have to use my own renderer. But I cannot seem to implement this properly.

This is my current setup:

public class MyClass
{
    public static void main(String args[])
    {
        JTable myTable = new JTable(10, 10);
        myTable.setDefaultRenderer ([I dont know what to put here], new CustomRenderer());
    }
}

class CustomRenderer extends DefaultTableCellRenderer 
{
    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);

        // Formatting
        return c;
    }
}

What do I need to use for the first parameter of setDefaultRenderer? The API just says 'class'. I have no idea what to put there.

Could someone just explain, in the simplest of terms, how I go about implementing this? Please provide an example of how I can change the formatting from within the main() method as well.

解决方案

In the first parameter for setDefaultRenderer, put the class literal for the Class that you want to override rendering. I.e., if your data consist all of strings, you can put

myTable.setDefaultRenderer(String.class, new CustomRenderer());

If your data also consists of values with BigDecimal or Integer as classes, you have to invoke that method several times for each class type (BigDecimal.class or Integer.class in each case).

And finally, to change the background color you do this in your renderer:

class CustomRenderer extends DefaultTableCellRenderer 
{
    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);
        c.setBackground(new java.awt.Color(255, 72, 72));
        return c;
    }
}

If you write a renderer that should work for all classes of an interface, you will also need to modify the getColumnClass function of your table model and let it return the interface class for all objects that implement this interface:

public Class<? extends Object> getColumnClass(int c) {
    Object object = getValueAt(0, c);
    if(object == null) {
        return Object.class;
    if(getValueAt(0, c) instanceof IColorable) {
        return ICarPart.class;
    } else {
        return getValueAt(0, c).getClass();
    }
}

With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.

这篇关于更改JTable单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆