#navi(contents-page-name): No such page: ST

Terai Atsuhiro 2023-12-14 (木) 10:20:14

http://terai.s55.xrea.com/swing/tablecellrenderer/screenshot.png

class MyJLabel extends JLabel {
  private GlyphVector gvtext;
  public MyJLabel() {super();}
  protected void paintComponent(Graphics g){
    //super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    int WRAPPING_WIDTH = getWidth()-getInsets().left-getInsets().right;
    FontRenderContext frc = g2.getFontRenderContext();
    gvtext = getWrappedGlyphVector(getText(), WRAPPING_WIDTH, getFont(), frc);
    g2.setPaint(getBackground());
    g2.fillRect(0, 0, getWidth(), getHeight());
    g2.setPaint(getForeground());
    g2.drawGlyphVector(gvtext,
                       getInsets().left,
                       getFont().getSize()+getInsets().top);
  }
  private GlyphVector getWrappedGlyphVector(String str, float Wrapping,
                                            Font font, FontRenderContext frc){
    Point2D gmPos    = new Point2D.Double(0.0d, 0.0d);
    GlyphVector gv   = font.createGlyphVector(frc, str);
    float lineheight = (float) (gv.getLogicalBounds().getHeight());
    float xpos       = 0.0f;
    float advance    = 0.0f;
    int   lineCount  = 0;
    GlyphMetrics gm;
    for(int i=0;i<gv.getNumGlyphs();i++){
      gm = gv.getGlyphMetrics(i);
      advance = gm.getAdvance();
      if(xpos<Wrapping && Wrapping<=xpos+advance){
        lineCount++;
        xpos = 0.0f;
      }
      gmPos.setLocation(xpos, lineheight*lineCount);
      gv.setGlyphPosition(i, gmPos);
      xpos = xpos + advance;
    }
    return gv;
  }
}
class TestRenderer extends MyJLabel implements TableCellRenderer{
  public TestRenderer(){

ST/GlyphVector

class MyCellRenderer extends JTextArea implements TableCellRenderer {
  MyCellRenderer() {
    super();
    setLineWrap(true);
  }
  public Component getTableCellRendererComponent(
      JTable table, Object value,
      boolean isSelected, boolean hasFocus,
      int row, int column){
    setText((value == null) ? "" : value.toString());
    return this;
  }
}