2012-08-24 4 views
1

격자 구조를 만들기 위해 JXTreeTable을 사용하고 있습니다. 이제 특정 셀의 색상을 동적으로 변경하고 싶습니다. 세포의 색을 어떻게 바꿀 수 있습니까?JXTreeTable에서 특정 셀의 색상을 동적으로 변경하는 방법

이 코드를 사용하여 색상을 변경했지만 작동하지 않습니다. 셀이 컬러의 이름과 텍스트가 포함

leftTree.setDefaultRenderer(Object.class, new 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);   
     if(Integer.parseInt(rowvalue[0])==row && column==0) { 
     c.setBackground(Color.red); 
     } 
     return c; 
     } 
}); 
+0

JXTreeTable 및 트리 및 테이블 렌더러에 대한 몇 가지 버그가 있으며, SwingX 포럼에서 검색하고, – mKorbel

답변

2

형광펜을 사용하십시오.

addHighlighter(new ColorHighlighter()); 
0

경우이 값을 수정할 수있을 것입니다 : 여기

코드입니다.

import java.awt.Color; 
import java.awt.Component; 

import javax.swing.DefaultCellEditor; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.DefaultTableCellRenderer; 

public class MainClass extends JFrame { 
    ColorName colors[] = { new ColorName("Red"), new ColorName("Green"), new ColorName("Blue"), 
     new ColorName("Black"), new ColorName("White") }; 

    public MainClass() { 
    super("Table With DefaultCellEditor Example"); 
    setSize(500, 300); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    JTable table = new JTable(new AbstractTableModel() { 
     ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0], 
      colors[1], colors[2], colors[3], colors[4] }; 

     public int getColumnCount() { 
     return 3; 
     } 

     public int getRowCount() { 
     return 10; 
     } 

     public Object getValueAt(int r, int c) { 
     switch (c) { 
     case 0: 
      return (r + 1) + "."; 
     case 1: 
      return "Some pithy quote #" + r; 
     case 2: 
      return data[r]; 
     } 
     return "Bad Column"; 
     } 

     public Class getColumnClass(int c) { 
     if (c == 2) 
      return ColorName.class; 
     return String.class; 
     } 

     public boolean isCellEditable(int r, int c) { 
     return c == 2; 
     } 

     public void setValueAt(Object value, int r, int c) { 
     data[r] = (ColorName) value; 
     } 
    }); 

    table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors))); 
    table.setDefaultRenderer(ColorName.class, new TableCellRenderer()); 
    table.setRowHeight(20); 
    getContentPane().add(new JScrollPane(table)); 
    } 

    public static void main(String args[]) { 
    MainClass ex = new MainClass(); 
    ex.setVisible(true); 
    } 

    public class ColorName { 
    String cname; 

    public ColorName(String name) { 
     cname = name; 
    } 

    public String toString() { 
     return cname; 
    } 
    } 

    public class TableCellRenderer extends DefaultTableCellRenderer { 

    public Component getTableCellRendererComponent(
     JTable table, Object value, 
     boolean isSelected, boolean hasFocus, 
     int row, int col) 
    { 
     // get the DefaultCellRenderer to give you the basic component 
     Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 
     // apply your rules 
     if (value.toString().equals("Red")) 
      c.setBackground(Color.RED); 
     else 
      c.setBackground(Color.GRAY); 
     return c; 
    } 
} 
} 
관련 문제