簡體   English   中英

如何將JTable單元格的類更改為JCheckBox?

[英]How to change class of JTable cell to JCheckBox?

我的GUI中有一個(或多或少)空的JTable,開頭沒有任何JCheckBoxes。 按下按鈕后,我想根據數組的內容用名稱(字符串)和JCheckBoxes填充表。 具有名稱的部分已經可以正常工作,但是我無法將單個單元格的類更改為布爾值,而是在JTable實際應位於的JTable中顯示true或false。 我知道如果在開始時將列類型設置為布爾值,則可以用JCheckBoxes填充完整的列。

我現在的問題是,是否可以更改單個單元格的列類型。

這是創建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);

        }
    }
}

如果您需要更多信息,請先問我,並在此先感謝。

您可以執行以下操作:

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);
    }
}

它根據單元格中的數據而不是列類確定要使用的渲染器/編輯器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM