将文本写入AbstractTableModel单元格

时间:2013-09-12 08:16:29

标签: java jtable cells

我已经实现了一个AbstractTableModel对象,这是一种时间表。我想通过使用方法(当时我不想将这些行声明为已编译)来写入当时将要教授的主题的单元格。 我能怎么做?这是我的AbstractTableModel对象。

private class MyTableModel extends AbstractTableModel {

    private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    private String[][] data = {{"8:30 - 9:30","","","","","",""},
            {"9:30 - 10:30","","","","","",""},
            {"10:30 - 11:30","","","","","",""},
            {"11:30 - 12:30","","","","","",""},
            {"12:30 - 13:30","","","","","",""},
            {"13:30 - 14:30","","","","","",""},
            {"14:30 - 15:30","","","","","",""},
            {"15:30 - 16:30","","","","","",""},
            {"16:30 - 17:30","","","","","",""}};

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

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

        public String getColumnName(int col) {
            return columns[col];
        }

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

}   

1 个答案:

答案 0 :(得分:1)

您必须覆盖setValueAt方法。如果您希望表格编辑有问题的单元格,您还需要覆盖isCellEditable方法

请查看How to use tables了解详情

更新了示例

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class TestTable20 {

    public static void main(String[] args) {
        new TestTable20();
    }

    public TestTable20() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JTable table = new JTable(new MyTableModel());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MyTableModel extends AbstractTableModel {

        private String[] columns = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        private String[][] data = {{"8:30 - 9:30", "", "", "", "", "", ""},
        {"9:30 - 10:30", "", "", "", "", "", ""},
        {"10:30 - 11:30", "", "", "", "", "", ""},
        {"11:30 - 12:30", "", "", "", "", "", ""},
        {"12:30 - 13:30", "", "", "", "", "", ""},
        {"13:30 - 14:30", "", "", "", "", "", ""},
        {"14:30 - 15:30", "", "", "", "", "", ""},
        {"15:30 - 16:30", "", "", "", "", "", ""},
        {"16:30 - 17:30", "", "", "", "", "", ""}};

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex > 0;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            String[] row = data[rowIndex];
            row[columnIndex] = (String)aValue;
            fireTableCellUpdated(rowIndex, columnIndex);
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

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

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

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

    }
}