JXTable の背景画像がレンダリングされない
質問
背景画像を使用して JXTable を生成しようとしています (テキストでも問題ありません)。これが私の拡張された JXTable クラスです。
public class JXTableWithBackground extends JXTable{
ImageIcon image;
public JXTableWithBackground(ParticipantTableModel pTableModel, ImageIcon image){
super(pTableModel);
this.image=image;
}
public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
Component c = super.prepareRenderer( renderer, row, column);
// We want renderer component to be transparent so background image is visible
if( c instanceof JComponent )((JComponent)c).setOpaque(false);
return c;
}
@Override
public void paint(Graphics g) {
//draw image in centre
final int imageWidth = image.getIconWidth();
final int imageHeight = image.getIconHeight();
final Dimension d = getSize();
final int x = (d.width - imageWidth)/2;
final int y = (d.height - imageHeight)/2;
g.drawImage(image.getImage(), x, y, null, null);
super.paint(g);
}
画像が表示されません - 空白だけが表示されます。何か案が?
解決
将来の参考のため:
問題は、テーブル自体が透過的にレンダリングされていないようです。テーブル自体をopaque= falseに設定します。
他のヒント
のために SwingX
たとえば、レンダリングに不透明なコンポーネントを使用する場合に推奨される方法は、 Highlighter
インターフェース。したがって、オーバーライドする代わりに、 prepareRenderer
メソッドを作成することをお勧めします。 Highlighter
そして、 JXTable#setHighlighters
テーブルにセットする方法
所属していません StackOverflow