简体   繁体   中英

How to get the selected index of checkbox from tableview on javafx

I am very new to JavaFX, I need help in identifying the selected/checked checkbox from the table-view to . See the screenshot for the UI and code i used to the populate data on the table view. I am using scene builder for creating the UI

Code to initialize the table view

public void initialize(URL location, ResourceBundle resources) {

    ddUrls.setItems(urls);
    ddbrowserNames.setItems(browsers);
    ddFrames.setItems(frames);

    //testClassCl.setCellValueFactory(new PropertyValueFactory<TestSuite,String>("testClass"));
    testMethodCl.setCellValueFactory(new PropertyValueFactory<TestSuite,String>("testMethod"));
    testDescCl.setCellValueFactory(new PropertyValueFactory<TestSuite,String>("testDesc"));
    //runModeCl.setCellValueFactory(new PropertyValueFactory<TestSuite,Boolean>("runMode"));
    runModeCl.setCellFactory(column -> new CheckBoxTableCell()); 
    table.setItems(list);
    table.setEditable(true);
}

Image of the UI

在此处输入图片说明

Here is the data model.

package com.automation.UI;

import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty;

public class TestSuite {

private SimpleStringProperty testClass;
private SimpleStringProperty testMethod;
private SimpleStringProperty testDesc;
private SimpleBooleanProperty runMode;

public TestSuite(String testClass, String testMethod, String testDesc, boolean runMode) {
    this.testClass = new SimpleStringProperty(testClass);
    this.testMethod = new SimpleStringProperty(testMethod);
    this.testDesc = new SimpleStringProperty(testDesc);
    this.runMode = new SimpleBooleanProperty(runMode);
}

public String getTestClass() {
    return testClass.get();
}


public String getTestMethod() {
    return testMethod.get();
}

public String getTestDesc() {
    return testDesc.get();
}

public boolean getRunMode() {
    return runMode.get();
}

}

My aim is to get the Description( column next to check) of all the selected checkbox on clicking another button

There is a couple of ways you can do this the first is to create a list of check boxes and iterate through them and check if(checkBox.isSelected()) otherwise you may have to iterate though all of your nodes to check if it is selected here is an example

List<Object> checkedList = new ArrayList<>();
for (Object node : vbox.getChildren())
    if (checkBox instanceof CheckBox)
        if (((CheckBox) checkBox).isSelected())
            checkedList.add(node); 

Then you will have a list of selected checkboxes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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