2016-03-05 30 views
0

我在開始時沒有任何JCheckBox,在我的GUI中得到了(或多或少)空的JTable。按下按鈕後,我想根據數組的內容使用名稱(字符串)和JCheckBoxes來填充表格。具有名稱的部分已經正常工作,但我無法將單個單元格的類更改爲布爾值,而是在JCheckBox實際應該在的JTable中出現true或false。我知道如果您在開始時將列類型設置爲布爾值,那麼您可以使用JCheckBox填充整個列。如何將JTable單元的類更改爲JCheckBox?

我現在的問題是如果有可能改變單個單元格的列類型。

這是JTable中創建代碼:

table = new JTable(); 
    table.setRowMargin(3); 
    table.setRowHeight(25); 
    table.setFillsViewportHeight(true); 
    table.setModel(new DefaultTableModel(
      new Object[][] { 
       {null, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}, 
       {"", "2016-02-29", "2016-03-01", "2016-03-02", "2016-03-03", "2016-03-04"}, 
       {" 1. Lesson", null, null, null, null, null}, 
       {" 2. Lesson", null, null, null, null, null}, 
       {" 3. Lesson", null, null, null, null, null}, 
       {" 4. Lesson", null, null, null, null, null}, 
       {" 5. Lesson", null, null, null, null, null}, 
       {" 6. Lesson", null, null, null, null, null}, 
       {" 7. Lesson", null, null, null, null, null}, 
       {" 8. Lesson", null, null, null, null, null}, 
       {" 9. Lesson", null, null, null, null, null}, 
       {" 10. Lesson", null, null, null, null, null}, 
      }, 
      new String[] { "Empty", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" } 
     ) 
     { 
      Class[] columnTypes = new Class[] 
       { 
        String.class, Object.class, Object.class, Object.class, Object.class, Object.class 
       }; 
      public Class getColumnClass(int columnIndex) 
      { 
       return columnTypes[columnIndex]; 
      } 
     }); 
    table.setBorder(new LineBorder(new Color(0, 0, 0))); 
    table.setBounds(381, 11, 518, 300); 
    panel.add(table); 

這是JTable的內容被改變的部分:

public void tableUpdate(Object[][] a) 
{ 
    for(int row = 0; row < 11; row++) 
    { 
     for(int column = 0 ; column < 5; column++) 
     { 
      table.setValueAt(a[column][row], row+1, column+1); 

     } 
    } 
} 

如果您需要了解更多信息請諮詢我,提前謝謝了。

+2

從'getColumnClass'返回'Boolean.class'作爲你想顯示爲複選框的列 – MadProgrammer

+0

問題是我不想只用複選框選擇整個列,我想要名稱(字符串)和複選框,取決於數組的內容 – jonas001

+1

好的,這不是JTable的工作原理。一列只假設有一個單一的數據類型 – MadProgrammer

回答

1

你可以做這樣的事情:

import java.awt.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.table.*; 

public class TablePropertyEditor extends JFrame 
{ 
    public TablePropertyEditor() 
    { 
     String[] columnNames = {"Type", "Value"}; 
     Object[][] data = 
     { 
      {"String", "I'm a string"}, 
      {"Date", new Date()}, 
      {"Integer", new Integer(123)}, 
      {"Double", new Double(123.45)}, 
      {"Boolean", Boolean.TRUE} 
     }; 

     JTable table = new JTable(data, columnNames) 
     { 
      private Class editingClass; 

      public TableCellRenderer getCellRenderer(int row, int column) 
      { 
       editingClass = null; 
       int modelColumn = convertColumnIndexToModel(column); 

       if (modelColumn == 1) 
       { 
        Class rowClass = getModel().getValueAt(row, modelColumn).getClass(); 
        return getDefaultRenderer(rowClass); 
       } 
       else 
        return super.getCellRenderer(row, column); 
      } 

      public TableCellEditor getCellEditor(int row, int column) 
      { 
       editingClass = null; 
       int modelColumn = convertColumnIndexToModel(column); 

       if (modelColumn == 1) 
       { 
        editingClass = getModel().getValueAt(row, modelColumn).getClass(); 
        return getDefaultEditor(editingClass); 
       } 
       else 
        return super.getCellEditor(row, column); 
      } 

      // This method is also invoked by the editor when the value in the editor 
      // component is saved in the TableModel. The class was saved when the 
      // editor was invoked so the proper class can be created. 

      public Class getColumnClass(int column) 
      { 
       return editingClass != null ? editingClass : super.getColumnClass(column); 
      } 
     }; 

     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 
    } 

    public static void main(String[] args) 
    { 
     TablePropertyEditor frame = new TablePropertyEditor(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

它決定基於細胞不列類的數據要使用的渲染器/編輯器。

+0

'getModel()。getValueAt(row' <<使用視圖索引來索引模型。由於'getCellRenderer'只會在視圖中可見的單元格被調用,所以你可以使用' this.getValueAt(row,column)'。 –

+0

@TT,實際上我並不擔心這一行,我只是想強調,爲了渲染/編輯目的,「模​​型列」的類是相關的類因爲我在if條件中使用模型列,所以我想要保持一致並從模型中的特定列中獲取數據。 – camickr