2017-02-28 1 views
0

4 개의 열이있는 JTable이 있습니다. 각 셀의 값에 따라 렌더링 될 인덱스 2 (즉, '변경'열)의 열만 원할 것입니다. 내 코드JTable - 셀 색상이 작동하지 않는 이유

private DefaultTableModel tmodel; 
private final int table_colID_at_compID = 0; 
private final int table_colID_at_compName = 1; 
private final int table_colID_at_stockPresentageGrowth = 2; 
private final int table_colID_at_compNetWorth = 3; 

..........more code(unimportant code).......... 

    tmodel = new DefaultTableModel(new String[][] {} ,new String[]{"Comp. ID","Com. Name","Change %","Net Worth"}); 

    table = new JTable(tmodel){ 
     public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){ 
      Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex); 

      if(columnIndex == table_colID_at_stockPresentageGrowth) { 
       double value = new Double(getValueAt(rowIndex, columnIndex).toString()); 
       if(value < 0) 
        componenet.setBackground(Color.RED); 
       else if(value == 0) 
        componenet.setBackground(Color.WHITE); 
       else 
        componenet.setBackground(Color.GREEN); 
      } 

      return componenet; 
     } 
    }; 
  • 에서 보라 이름 table_colID_at_<something> 모든 변수는 테이블 열 ID를 나타냅니다.
  • 아래 그림에서 볼 수 있듯이 프로그램은 2 개의 열 ('변경'및 '순수 가치') 만 페인트합니다. ('변경'열만 페인트해야합니다.)
  • 프로그램을 디버깅하고 발견했습니다. if 문은 괜찮습니다.
  • 그래서 문제는 작업 뒤에있는 논리와 함께하는 것이 좋습니다.

이미지 : (imgur에 오류가)

다시 흰색으로 색상을 설정해야 다른 모든 컬럼에 대한

http://i64.tinypic.com/fx5n2q.png

답변

0

.

+0

하지만 IF 문에서 특정 열 인덱스를 요청합니다. 나는 단지 '변경'열만 확인하고 나머지는 확인하지 않습니다. – Shlomi

+1

그리고 그것은 실수입니다. 구성 요소는 공유 객체입니다. 한 번 색상을 설정하면 남아 있습니다. –

+0

많은 기독교인 감사합니다! 배경을 흰색으로 설정하기 위해 큰 IF 문 다음에 ELSE를 추가했습니다. 왜 내가 색상으로 다음 행을 칠하는지 확신 할 수 없지만. – Shlomi