0

尝试在表格视图中获取所选行的第一列值时遇到一些问题。我动态填充我的表数据:

 private TableView<Category> viewCategory = new TableView<>();

public List<Category> populateCategoryTable() {
    List ll = new LinkedList();

    try {
        db.getConnection();
        String sql = "SELECT * FROM sm_category";
        ResultSet rs = null;
        // Call readRequest to get the result
        rs = db.readRequest(sql);

        while (rs.next()) {
            String ID = rs.getString("categoryID");
            String name = rs.getString("categoryName");
            String desc = rs.getString("categoryDescription");
            Category row = new Category();
            row.setid(ID);
            row.setname(name);
            row.setdesc(desc);
            ll.add(row);
        }
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Error SQL!!!");
        System.exit(0);
    }
    return ll;
}

public class Category {

private SimpleStringProperty ID;
private SimpleStringProperty name;
private SimpleStringProperty desc;

public void setid(String value) {
    IDProperty().set(value);
}

public String getid() {
    return IDProperty().get();
}

public SimpleStringProperty IDProperty() {
    if (ID == null) {
        ID = new SimpleStringProperty(this, "ID");
    }
    return ID;
}

public void setname(String value) {
    nameProperty().set(value);
}

public String getname() {
    return nameProperty().get();
}

public SimpleStringProperty nameProperty() {
    if (name == null) {
        name = new SimpleStringProperty(this, "name");
    }
    return name;
}

public void setdesc(String value) {
    descProperty().set(value);
}

public String getdesc() {
    return descProperty().get();
}

public SimpleStringProperty descProperty() {
    if (desc == null) {
        desc = new SimpleStringProperty(this, "desc");
    }
    return desc;
}
}

And when the row is selected, I perform this on my table view:

viewCategory.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        // this method will be called whenever user selected row
        @override
        TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
        ObservableList selectedCells = selectionModel.getSelectedCells();
        TablePosition tablePosition = (TablePosition) selectedCells.get(0);
        int row = tablePosition.getRow(); // yields the row that the currently selected cell is in
    }
);

但是,我收到错误消息:

<anonymous shopManagement.boundary.RetrieveProductUI$1> is not abstract and does not override abstract method changed(ObservableValue,Object,Object) in ChangeListener

我不知道为什么。有人可以帮忙吗?

更新部分

viewCategory.getSelectionModel().setCellSelectionEnabled(true);
    viewCategory.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); // just in case you didnt already set the selection model to multiple selection.
    viewCategory.getSelectionModel().getSelectedCells().addListener(new ListChangeListener<TablePosition>() {
        @Override
        public void onChanged(Change<? extends TablePosition> change) {
            TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
            ObservableList selectedCells = selectionModel.getSelectedCells();
            TablePosition tablePosition = (TablePosition) selectedCells.get(0);
            int row = tablePosition.getRow(); // yields the row that the currently selected cell is in
            System.out.println(row);
        }
    });

我设法显示数据,但我无法获得选定的行。它给了我一条错误消息:

IndexOutOfBoundsException:索引:0,大小:0

4

2 回答 2

1

YourChangeListener 不完整。你有一个 @override 注释,但你没有覆盖任何方法。你的 changeListener 需要这样

    t.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {

        }
    });
于 2013-06-27T08:26:05.110 回答
0

尝试做这样的事情:

t.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {
                 DefaultTableModel model = (DefaultTableModel) table.getModel();
                 int pos = table.getSelectedRow();
                 String value = (String) model.getValueAt(pos, 0); //current row, first column
        }
    });

希望这可以帮助。

于 2013-06-27T08:25:59.247 回答