2011-08-23 3 views
-1

JTable에 3 개의 열이 있습니다. 하나의 열을 편집 할 수 있습니다. 다른 열은 편집 할 수 없습니다. 편집 가능한 열은 녹색으로 표시되고 편집 할 수없는 열은 빨간색으로 표시되어야합니다. DefaultRenderer 클래스로 시도했지만 작동하지 않습니다. 아무도 이걸 알면 제발 도와주세요.편집 가능한 필드가 아닌 경우 JTable의 Color Renderer?

답변

2

글쎄 거기에 몇 가지 방법이 있습니다. 다음은 1입니다. 열 1은 회색으로 표시됩니다.

JTable table = new JTable() { 
    public Component prepareRenderer(TableCellRenderer renderer, 
            int rowIndex, int vColIndex) { 
     Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); 
     if (vColIndex == 0) {//if first column 
      c.setBackground(Color.red); 
     } else { 
      c.setBackground(Color.green); 
     } 
     return c; 
    } 
}; 

또는 당신은 내가 table.setDefaulRenderer (Color.class, 새로운 CustomTableCellRenderer처럼 호출 한 두 번째 only..i을 사용했다 2

public class CustomTableCellRenderer extends DefaultTableCellRenderer 
{ 
    public Component getTableCellRendererComponent (JTable table, Object obj, 
         boolean isSelected, boolean hasFocus, int row, int column){ 
     Component cell = super.getTableCellRendererComponent(table, obj, 
          isSelected, hasFocus, row, column); 

     if (column == 0){ 
      cell.setBackground(Color.red); 
     } 
     else{ 
      cell.setBackground(Color.green); 
     } 
     return cell; 
    } 
} 
+0

안녕을 다음과 같이 클래스의 재정의 DefaultTableCellRenderer을 가질 수있다()); ..하지만 작동하지 않습니다 .. – shree

+0

+1 두 접근법 모두 정상입니다. 여전히 문제가 발생하면 질문을 편집하여 [sscce] (http://sscce.org/)를 포함 시키십시오. 왼쪽에있는 [빈 녹색 체크 표시] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)를 클릭하면이 대답을 수락 할 수 있습니다. – trashgod