2010-08-30 104 views

回答

2

是的。但我認爲你需要一個複合渲染器,這意味着你必須創建自己的CellRenderer,實現TableCellRenderer或擴展現有的DefaultTableCellRenderer。至少只要你想在你的表中顯示這些值,這應該適合你。

您的化合物將包含用於顯示您的浮標的標籤和用於顯示您的布爾值的複選框。

編輯: 好吧,這裏一個小例子:

/** 
* Example for CompoundRenderer 
* 
* @author ymene 
*/ 
public class CompoundRendererExample extends JPanel 
{ 

    public static void main(String[] args) 
    { 
    JFrame frame = new JFrame("Example for rendering JTable - values with CompoundRenderer"); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    frame.add(new CompundRendererExample()); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    public CompoundRendererExample() 
    { 
    JScrollPane scrollPane = new JScrollPane(); 
    JXTable table; 

    table = new JXTable(new TableModel()); 
    table.setFillsViewportHeight(true); 

    for (int i = 0; i < table.getModel().getColumnCount(); i++) 
     table.getColumn(i).setPreferredWidth(200); 

    scrollPane.setViewportView(table); 
    add(scrollPane); 

    //Declaring compound-renderer 
    table.setDefaultRenderer(FloatBool.class, new FloatBoolRenderer()); 
    } 
} 


class TableModel extends AbstractTableModel 
{ 
    private String[] columnNames = { "Float-Boolean" }; 
    private Object[][] data  = { { new FloatBool(2.2f, true) }, { new FloatBool(3.2f, false) } }; 

    public int getColumnCount() 
    { 
    return columnNames.length; 
    } 

    public int getRowCount() 
    { 
    return data.length; 
    } 

    @Override 
    public String getColumnName(int col) 
    { 
    return columnNames[ col ]; 
    } 

    public Object getValueAt(int row, int col) 
    { 
    return data[ row ][ col ]; 
    } 

    @Override 
    public Class getColumnClass(int c) 
    { 
    if (getValueAt(0, c) == null) 
     return Object.class; 
    return getValueAt(0, c).getClass(); 
    } 

    @Override 
    public boolean isCellEditable(int row, int col) 
    { 
    return true; 
    } 

    @Override 
    public void setValueAt(Object value, int row, int col) 
    { 
    data[ row ][ col ] = value; 
    fireTableCellUpdated(row, col); 
    } 
} 


class FloatBoolRenderer extends DefaultTableCellRenderer 
{ 
    JLabel floatPartLabel; 
    JCheckBox booleanPartCheckBox; 
    JPanel container; 

    public FloatBoolRenderer() 
    { 
    floatPartLabel = new JLabel(); 
    booleanPartCheckBox = new JCheckBox(); 
    container = new JPanel(); 

    container.setLayout(new BorderLayout()); 
    container.add(floatPartLabel, BorderLayout.CENTER); 
    container.add(booleanPartCheckBox, BorderLayout.EAST); 
    container.setVisible(true); 
    } 

    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                boolean hasFocus, int row, int column) 
    { 
    if (value != null) 
    { 
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

     if (value instanceof FloatBool) 
     { 
     FloatBool floatboolean = (FloatBool) value; 
     booleanPartCheckBox.setSelected(floatboolean.getBooleanValue()); 
     floatPartLabel.setText("" + floatboolean.getFloatValue()); 
     } 
    } 

    return container; 
    } 
} 


class FloatBool 
{ 
    float floatValue; 
    boolean booleanValue; 

    public FloatBool(float floatValue, boolean booleanValue) 
    { 
    this.floatValue = floatValue; 
    this.booleanValue = booleanValue; 
    } 

    public boolean getBooleanValue() 
    { 
    return booleanValue; 
    } 

    public float getFloatValue() 
    { 
    return floatValue; 
    } 
} 

還不完善,但應該給你的想法如何設計自己的渲染。

+0

請確定:既沒有DefaultCellRenderer也沒有AbstractDefaultCellRenderer ;-) – kleopatra 2012-02-10 16:02:49

+0

呵呵,當然你是對的!只是想糾正我的一些舊條目。但沒有注意到一個。希望你現在更喜歡它! :P – crusam 2012-02-10 17:35:34