1

我正在实现一种表。为此,我使用了AbstractTableModel,但我不知道我的代码在做什么(这就是为什么我得到一个完成他的工作的片段,但显然我想理解)。你可以看到代码

public class TablePanel extends JPanel
{
    private JTable table;
    public TablePanel()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
        table = new JTable(new MyTableModel());
        table.setFillsViewportHeight(true);     
        table.setPreferredScrollableViewportSize(new Dimension(500, 100));
        JScrollPane jps = new JScrollPane(table);
        add(jps);
        add(new JScrollPane(table));
        table.setCellSelectionEnabled(true);
    }
    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];
        }
        public Object myMethod (){
            //do something
        }
    }
}

嗯,这是什么意思table = new JTable(new MyTableModel());
我认为这table将是一个MyTableModel例子,但事实并非如此。事实上,如果我将 table 传递给另一个外部类,这个类将看myMethod不到MyTableModel.

4

3 回答 3

1

AMyTableModel是 a TableModel,但 a不一定TableModel是a 。MyTableModel

如您所知,您的表模型是 的实例MyTableModel,它具有所需的myMethod(),您可以使用强制转换:

JTable table = new JTable(new MyTableModel());
MyTableModel model = (MyTableModel) table.getModel();

创建和使用对模型的引用可能更容易:

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

不要忽略包括 的实现setValueAt()此处显示。

于 2013-09-14T17:50:18.640 回答
0

TableModelJTable 构造函数接受实现接口的类的任何对象

将帮助您了解更多信息。

于 2013-09-14T17:21:37.033 回答
0

其他类无法看到函数 myMethod 的预期行为,因为他们“认为”您的 MyTableModel 只是一个 AbstractTableModel (实际上它不是)您放入 MyTableModel 的代码和其他所有内容都被正确调用,但您只是看不到函数 myMethod那是因为您使用的是 OOP 语言

所以不,你错了。您正在创建 MyTableModel 的实例,因此如果您调用,您在其中所做的所有事情都将被执行

你应该在这里阅读这将解释更多关于 OOP 部分: http ://www.dummies.com/how-to/content/understanding-javas-objectorientated-programming-oop.html

快说

如果您需要访问您的 myMethod 函数,只需将其转换为: // 创建 MyTableInstance(这是可能的,因为 AbstractTableModel 是父级,MyTableModel 是子级) AbstractTableModel atmTestVariable = new MyTableModel();

 // Now to be able to call this:
 // atmTestVariable.myMethod();
 // we need to do this:

 if(atmTestVariable isntanceof MyTableModel) //make sure that atmTestVariable is REALY a MyTableModel
 {
    MyTableModel mtmTemp = (MyTableModel)atmTestVariable; //cast to MyTableModel
    mtmTemp.myMethod(); //Call the function
    // and now the short variant:
    ((MyTableModel)atmTestVariable).myMethod(); //cast to MyTableModel AND call the function
 }

希望我能提供帮助

哦,请把你的 MyTabelModel 类的定义移到一个单独的文件中:/

于 2013-09-14T17:36:51.550 回答