简体   繁体   English

子类foo扩展了JTable。 TableModelListener.getSource返回JTable而不是foo

[英]Subclass foo extends JTable. TableModelListener.getSource returns JTable not foo

Hopefully a simple question but I am struggling with inheritance. 希望这是一个简单的问题,但我在继承方面苦苦挣扎。

The ultimate goal is to detect when a cell is changed in a JTable and then run an action that involves getting an extra specific property associated with the table. 最终目标是检测JTable中的单元格何时更改,然后运行涉及获取与表关联的额外特定属性的操作。

I have a class that extends JTable, so that I can add some extra properties to the table. 我有一个扩展JTable的类,以便可以向表中添加一些额外的属性。 The point of extending JTable is so that when a TableModelListener is activated I can get at the extra properties. 扩展JTable的重点是,当激活TableModelListener时,我可以获得额外的属性。

public class dataTable extends JTable implements TableModelListener{

private final PowerpointTable extraProperty;

public dataTable(String[][] tableArray, String[] colNames, PowerpointTable extraProperty) {
    super(tableArray, colNames);
    this.extraProperty= extraProperty;

    }

    public PowerpointTable  getExtraProperty() {
        return this.extraProperty;
    }
}

I would then like to treat this class like a JTable and in another class create the table and add an table listener that fires when the cells are changed. 然后,我想将此类像JTable一样对待,并在另一个类中创建表并添加一个表侦听器,该侦听器在更改单元格时触发。

public secondClass {

     public secondClass() {}

     public void createTable() {
         dataTable newdataTable= new dataTable(tableArray, columnNames, currentTable);
         newdataTable.getModel().addTableModelListener(new TableModelListener() {

         @Override
         public void tableChanged(TableModelEvent tme) {
               PowerpointTable pptT = tme.getSource().getExtraProperty;
         });
}

however getSource only returns a JTable which cannot be cast to a dataTable. 但是getSource仅返回一个JTable,该JTable无法转换为dataTable。

Please let me know how I can access this property or if there is a better way to implement this functionality. 请让我知道如何访问此属性,或者是否有更好的方法来实现此功能。

Solved it, 解决了

as rdonuk pointed out, getSource returns a TableModel, 正如rdonuk指出的那样,getSource返回一个TableModel,

I therefore changed the first class to extend a DefaultTableModel and added the extra property. 因此,我更改了第一类以扩展DefaultTableModel并添加了额外的属性。

public class TryingTableModel extends DefaultTableModel {

public TryingTableModel(PowerpointTable pptRef, Object[][] os, Object[] os1) {
    super(os, os1);
    this.pptRef = pptRef;
}

private final PowerpointTable pptRef;

public PowerpointTable getPPTTable() {
    return this.pptRef;
}

public String testAccess() {
    return "it worked";
}

The JTable is now initialised in the second class JTable现在在第二类中初始化

TryingTableModel tableModel = new TryingTableModel(currentTable, tableArray, columnNames);
tableModel.addTableModelListener(new TableModelListener() {

    @Override
    public void tableChanged(TableModelEvent tme) {
    TryingTableModel tableModel = (TryingTableModel) tme.getSource();
    System.out.println(tableModel.testAccess());
    }
});

JTable newJTable = new JTable(tableModel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM