2014-03-27 108 views
0

我試圖從here(我也嘗試了其他示例)實現自定義TableCellRenderer的示例。無論我做什麼,我的自定義TableCellRenderer都完全沒有效果,所以我認爲我在不同的層面上做錯了事情。任何人都可以請向我解釋我做錯了什麼?這裏是我的代碼:無效的自定義表格單元格渲染器

import java.text.DecimalFormat; 
import java.util.Random; 

import javax.swing.Icon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingConstants; 
import javax.swing.border.EmptyBorder; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 

public class Test { 
    public static void main(String[] args) { 
    // Show GUI 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
     GUI gui = new GUI(); 

     int numRows = 5; 
     int numCols = 5; 

     DefaultTableModel tableModel = new DefaultTableModel(0, 
      numCols); 

     // Add rows 
     for (int i = 0; i < numRows; i++) { 
      Float[] row = new Float[numCols]; 

      // Build columns 
      for (int j = 0; j < numCols; j++) { 
      Random random = new Random(); 
      row[j] = random.nextFloat(); 
      } 
      tableModel.addRow(row); 
     } 

     JTable table = new JTable(tableModel); 
     table.setDefaultRenderer(Float.class, new DecRenderer(
      new DecimalFormat("##"))); 

     JScrollPane scrollPane = new JScrollPane(table); 
     gui.getContentPane() 
      .add(scrollPane, BorderLayout.CENTER); 

     } 
    }); 
    } 
} 

class GUI extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public GUI() { 
    setTitle("GUI"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 350, 400); 
    setLocationRelativeTo(null); 
    setVisible(true); 

    JPanel contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 

    setContentPane(contentPane); 
    } 
} 

// Source: https://stackoverflow.com/questions/2833482/ 
// how-to-represent-double-values-as-circles-in-a-2d-matrix-in-java/ 
// 2834484#2834484 
@SuppressWarnings("serial") 
class DecRenderer extends DefaultTableCellRenderer implements 
    Icon { 

    private static final int SIZE = 32; 
    private static final int HALF = SIZE/2; 
    DecimalFormat   df; 

    public DecRenderer(DecimalFormat df) { 
    this.df = df; 
    this.setIcon(this); 
    this.setHorizontalAlignment(SwingConstants.RIGHT); 
    this.setBackground(Color.lightGray); 
    } 

    @Override 
    protected void setValue(Object value) { 
    setText((value == null) ? "" : df.format(value)); 
    } 

    @Override 
    public void paintIcon(Component c, Graphics g, int x, int y) { 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 
    g2d.setColor(Color.blue); 
    double v = Double.valueOf(this.getText()); 
    int d = (int) (v * SIZE); 
    int r = d/2; 
    g2d.fillOval(x + HALF - r, y + HALF - r, d, d); 
    } 

    @Override 
    public int getIconWidth() { 
    return SIZE; 
    } 

    @Override 
    public int getIconHeight() { 
    return SIZE; 
    } 
} 
+0

使用Icon實現DecRenderer不一定會將組件渲染爲圖標。嘗試覆蓋\t getTableCellRendererComponent –

+0

感謝您的提示,我現在就這樣做了。 –

回答

1

這是因爲在DefaultTableModel回報getColumnClassObject.class您的所有列(你的情況)。

快速修復是變化的模型旁邊似:

DefaultTableModel tableModel = new DefaultTableModel(0,numCols) { 
     @Override 
     public Class<?> getColumnClass(int columnIndex) { 
      return Float.class; 
     } 
    }; 
+0

非常感謝! –

+0

如果我的單元格中有混合類型,我應該怎麼做?我的意思是我有java.util.Date,String,Double和Boolean。這些類型中的每一個都位於我的表的不同列中。 – rysiekrysiek

+0

您需要通過'columnIndex'確定類型類。 – alex2410

-1

在DecRenderer類中添加這個API。我沒有測試代碼,所以請解決一些可變的編譯錯誤。

public Component getTableCellRendererComponent(JTable table, 
              Object value, 
              boolean isSelected, 
              boolean hasFocus, 
              int row, 
              int column) { 
    return new JPanel() { 
    @Override 
     public void paintComponent(Component c, Graphics g, int x, int y) { 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setColor(Color.blue); 
     double v = Double.valueOf(DecRenderer.this.getText()); 
     int d = (int) (v * SIZE); 
     int r = d/2; 
     g2d.fillOval(x + HALF - r, y + HALF - r, d, d); 
     } 
} 
} 
+0

請評論,如果你downvote,以便我可能知道我的錯誤! –