从表中选择行

时间:2013-06-27 08:05:04

标签: java javafx

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

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

当选择行时,我在表格视图上执行此操作:

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

2 个答案:

答案 0 :(得分:1)

YourChangeListener未完成。你有一个@override注释,但你没有覆盖任何方法。你的changeListener必须像那样

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

        }
    });

答案 1 :(得分: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
        }
    });

希望这有帮助。