2012-01-23 27 views
1

jtableにカスタムTableCellRendererを実装しようとしています。テーブルは100行100列に設定されています。このテーブルには、指定されたフォントのすべてのグリフが含まれている必要があります。私の問題は、テーブルが完全に値で完結していない場合、最初のカラムでテーブルの最後に達するまで最後の値を置きます。私はカスタムレンダラのコードと奇妙な動作のスクリーンショットがあります。どんな援助も感謝します。TableCellRendererの奇妙な動作java

enter image description here

public class FontRenderer extends JLabel implements TableCellRenderer 
{ 
Font desired_font; 
Object prec_value; 

public FontRenderer(Font f) 
{ 
    desired_font = f; 
} 

public Component getTableCellRendererComponent(JTable table, Object value, 
     boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) 
{ 


    setOpaque(true); 
    setHorizontalAlignment(SwingConstants.CENTER); 
    setBackground(new Color(255, 255, 255)); 
    if (isSelected) 
    { 
     if (value == null) 
     { 
      setText(""); 
     } 
     else 
     { 
      setText(value.toString()); 
     } 
     setFont(desired_font); 
     setBackground(new Color(56, 195, 254)); 
    } 
    if (value == null) 
    { 
     setText(""); 
    } 
    else 
    { 
     if(value==null) 
      table.setValueAt(null, rowIndex, vColIndex); 
     else 
     setText(value.toString()); 
      //table.setValueAt(value.toString(), rowIndex, vColIndex); 

    } 
    setFont(desired_font); 

    return this; 
} 
} 

編集:ここで私はテーブルを読み込むコードです。

解決済み。 Ty all。それは私がどのようにテーブルを占めていたかでした。次のコードは変更があります

while (cnt_i < 100) { 
    while (cnt_j < 100) { 
     if (my_fnt.canDisplay((char) unicode_char) && glyph_count <= total_glyphs) { 
      if (glyph_count == total_glyphs) { 
       break; 
      } 
      else { 
       jTable1.setValueAt((char) unicode_char, cnt_i, cnt_j); 
       cnt_j++; 
       glyph_count++; 
      } 
     } 
     unicode_char++; 
    } 
    cnt_i++; 
    cnt_j = 0; 
} 
+1

あなたのレンダラが非常に冗長であっても、あなたの問題の起源ではないようです。 – stryba

+0

あなたのレンダラに渡されるデータは 'TableModel'から来ているので、問題がどこにあるのでしょうか。レンダリングコードの代わりにそのコードを投稿してください – Robin

+0

あなたのコードはtable.setValueAtを呼び出すようではありません(レンダラーはテーブルの内容を変更すべきではありません)。だから私は、この問題はテーブルに人口が埋まっているようだと思います。 –

答えて

2

1)がおよそUnicode文字数をしている、私はそれが

2)Renderer

ためJTableむしろ渡すパラメータについてJTable#Fontを設定Rendererための仕事ではないと思います3)実行時にデータの束を変更したい場合はprepareRendererを使用してください

4)最も重要なのはどのようにデータを埋め込むのでしょうかJTable'sのデータと、私はあなたの問題がCellRendererのあることと思ういけないFont(s)

2

用/設定を定義する。..

しかし、私はさておきとして、あなた

public class FontRenderer extends JLabel implements TableCellRenderer 
{ 
    Font desired_font; 
    Object prec_value; 

    public FontRenderer(Font f) 
    { 
     desired_font = f; 
    } 

    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) 
    { 
     setOpaque(true); 
     setHorizontalAlignment(SwingConstants.CENTER); 
     setBackground(new Color(255, 255, 255)); 
     setFont(desired_font); 

     if (value == null) 
     { 
      setText(""); 
     } 
     else 
     { 
      setText(value.toString()); 
     } 

     if (isSelected) 
     { 
      setBackground(new Color(56, 195, 254)); 
     } 

     //what was that for? 
     //table.setValueAt(null, rowIndex, vColIndex); 

     return this; 
    } 
} 
2

のための少しそれをクリーンアップし、canDisplay(int)を助けるかもしれません特定のコードポイントに与えられたグリフがあるかどうかを調べるFontREPLACEMENT CHARACTERは便利なプレースホルダであり、GlyphSetが関連する例です。

+0

まずquck回答をありがとうございました – Andrei

+0

ここで私はテーブル – Andrei

+0

しばらく(cnt_i <100){ しばらく(cnt_j <100){ 場合(my_fnt.canDisplay((文字)unicode_charを移入コードがあります) && glyph_count <= total_glyphs) { jTable1.setValueAt((char)unicode_char、cnt_i、cnt_j); cnt_j ++; if(glyph_count == total_glyphs) { break; } glyph_count ++; } unicode_char ++; } cnt_i ++; cnt_j = 0; } – Andrei