문제

간단한 질문이지만 온라인 어디에서도 답을 찾을 수 없는 것 같습니다.

사용자 정의 TableCellRenderer를 사용하여 일부 표 셀을 굵은 글씨로 렌더링하려면 어떻게 해야 합니까?

TableCellRenderer를 사용하여 셀별로 배경색을 설정하는 방법을 알고 있습니다.다음과 같은 작업을 수행합니다.

  public class MyTableCellRenderer extends DefaultTableCellRenderer 
  {
    @Override 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);
        // modify the component "c" to have whatever attributes you like
        // for this particular cell
    }
  }

렌더링 텍스트 스타일을 변경하는 것이 비슷하다고 가정하지만 글꼴을 기본 테이블 글꼴과 동일하지만 굵은 글꼴로 어떻게 설정합니까?

도움이 되었습니까?

해결책

이미 기본 테이블 글꼴을 얻을 수 있다면 c.getFont()), 그냥 사용하십시오 deriveFont(Font.BOLD) 그 위에.

다른 팁

당신은 또한 테이블 행 렌더링 접근 방식 글꼴을 변경할 셀을 제어하는 ​​데 좀 더 유연성을 제공할 수 있습니다.선택한 행의 모든 ​​열에 있는 텍스트를 굵게 표시하는 데 사용했습니다.

이미 여기에 설명 된대로 캐싱으로 글꼴을 대담하게 설정하면 효과가 있습니다.

텍스트의 일부만 BOLD로 설정 해야하는 경우 HTML을 사용하십시오. 테이블 셀 렌더러는 기반입니다 JLabel (또는 반환 할 수 있습니다). 텍스트를 HTML로 변환하면 거의 모든 텍스트 속성이 변경 될 수 있습니다.

우리는이 기술을 광범위하게 사용했으며 상당한 성능 저하를 보지 못했습니다.

게으른 사람의 접근 방식은 다음과 같습니다.사용 DefaultTableCellRenderer (이것은 하위 클래스입니다. JLabel) HTML을 사용하여 굵은 서체를 사용하려는 경우를 지정합니다.

사용자 정의 렌더러를 정의하고 글꼴을 직접 제어하는 ​​것만큼 성능이 좋지는 않지만 일반적으로 코드가 더 간결하므로 간단한 애플리케이션에 좋습니다.

/**
 * Renderer implementation for rendering Strings.
 * Strings beginning with 'A' are rendered in bold.
 */
public class MyRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {

    String txt = String.valueOf(value);

    if (txt != null && txt.startsWith("A")) {
      // Reassign value as an HTML string.
      // Obviously need to consider replacing HTML special characters
      // if doing this properly.
      value = String.format("<body><b>%s</b></body>", txt);
    }

    // Delegate to superclass which will set the label text, background, etc.
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  }
}

당신은 이것도 사용할 수 있습니다 ..

        class SampleRenderer extends DefaultTableCellRenderer
        {

        public Component getJtableCellRendererComponent(Jtable table,Object value,boolean     isSelected , boolean hasFocus , int row, int column)

        {

        JLabel c = (JLabel)super.getJtableCellRendererComponent(table,value,isSelected ,hasFocus , row, column);

        Font f = c.getFont();

        c.setFont(f.getName(),Font.BOLD,f.getSize()));

        return c;

    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top