Javafx SceneBuilder Tableview.getSelectionModel不工作

Javafx SceneBuilder Tableview.getSelectionModel不工作,javafx,scenebuilder,Javafx,Scenebuilder,Tableview.getSelectionModel不工作。在@James_D通知表单之后,我使用了一个模型类(软件)来选择我需要的列,同时将SQL拉到一个表视图中。我在这里搜索了网页,移动了代码,检查了intellisense,我能在任何地方找到的最好的例子都在下面的SoftwareController代码中注释掉了,什么都不管用 在使用示例4所在的SoftwareController中的所有模型类之前,它给出了我单击的行上第0列的单元格数据,我使用该数据提取更多SQL数据。现在在newVa

Tableview.getSelectionModel不工作。在@James_D通知表单之后,我使用了一个模型类(软件)来选择我需要的列,同时将SQL拉到一个表视图中。我在这里搜索了网页,移动了代码,检查了intellisense,我能在任何地方找到的最好的例子都在下面的SoftwareController代码中注释掉了,什么都不管用

在使用示例4所在的
SoftwareController
中的所有模型类之前,它给出了我单击的行上第0列的单元格数据,我使用该数据提取更多SQL数据。现在在
newValue.get(0)
处出现错误,
newValue
未显示
get()
或getid可用

我已将
SelectedItem
更改为索引,并将
toString
以及所有这些内容添加到索引中,我得到了fxml。software@sometext或行索引。示例1给出了任何单元格的单元格数据,但我只想要所选行的第一列,在我的示例中,这是一个ID,而不是行索引。 我现在还必须对“原始”错误使用
@SuppressWarnings
,这是因为我处于初始化状态吗? 任何帮助或指点都将不胜感激

软件控制器

public class SoftwareController extends Application implements Initializable {
private Statement statement;
Connection conn = null;
@FXML Button  btnSoftware;
@FXML Label lblTest;
@FXML TableView tblSoftware;
@FXML TableColumn CI_IDcol;
@FXML TableColumn Namecol;

public static void main(String[] args) {

    launch(args);

}

@Override
public void start(Stage primaryStage) throws Exception {


    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Software.fxml")); //load Software fxml file
    Parent root1 = (Parent) fxmlLoader.load();
    primaryStage.setScene(new Scene(root1));
    primaryStage.show();

}

private static ObservableList<Software>data;
@FXML   private TextField txtFilter;
private Object getCellData;

@SuppressWarnings({ "unchecked", "rawtypes" })  //added due to TableView getselecionModel code
@Override
public void initialize(URL location, ResourceBundle resources)  {

    try {

        data = FXCollections.observableArrayList();
        conn = DBconnection.makeConnection();
        statement = conn.createStatement();
        String SQL = "SELECT * FROM Data_CMDB_Main";
        ResultSet rs = statement.executeQuery(SQL);

        while (rs.next())   {
            data.add(new Software(rs.getString("CI_ID"),
                                  rs.getString("Name")));

            CI_IDcol.setCellValueFactory(new PropertyValueFactory("CI_ID"));
            Namecol.setCellValueFactory(new PropertyValueFactory("Name"));
            tblSoftware.setItems(null);
            tblSoftware.setItems(data);

//TableView.selection               
            //get row example 1
            /*tblSoftware.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
                if(tblSoftware.getSelectionModel().getSelectedItem() != null) {
                    TableViewSelectionModel selectionModel = tblSoftware.getSelectionModel();
                    ObservableList selectedCells = selectionModel.getSelectedCells();
                    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
                    Object val = tablePosition.getTableColumn().getCellData(newValue);
                    //Object val = tblSoftware.getColumns().get(0).toString();
                    System.out.println(val);                    //int row = tablePosition.getRow();

                }                       
                }
            });*/


            //get row example 2   only gives index of filtered rows
            //tblSoftware.getSelectionModel().selectedIndexProperty().addListener((v, oldValue, newValue) -> System.out.println(newValue)); //gets all row data

            //get row example 3 ItemProperty seems correct just not giving readable row identification
            //tblSoftware.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> System.out.println(newValue)); //gets all row data


            ///get row example 4
            //@Override
            /*tblSoftware.getSelectionModel().selectedItemProperty().addListener( //gets any row column
                    (observable, oldValue, newValue) -> {
                        if (newValue == null) {
                            lblTest.setText("");
                            return;
                        }

                        lblTest.setText("Selected Number: " + newValue.get(0)); 
                    }
                );*/

            ///get row example 5

            /*tblSoftware.getSelectionModel().selectedItemProperty().addListener( //gets any row column
                    new ChangeListener<IdentifiedName>() {
                        @Override
                        public void changed (
                            ObservableValue<? extends IdentifiedName> observable,
                            IdentifiedName oldValue,
                            IdentifiedName newValue
                        ){
                            if(newValue == null) {
                                lblTest.setText("");
                                return;
                            }

                        lblTest.setText("Selected Number: " + + newValue.getId(0));

                }
            }   
     );             */



//filter        
            txtFilter.setPromptText("Text Filter");
            txtFilter.textProperty().addListener(new InvalidationListener() {

                @Override
                public void invalidated(Observable o)   {
                    tblSoftware.getSelectionModel().clearSelection(); // this gives no errors when switching back to filter box when row previously selected
                    if(txtFilter.textProperty().get().isEmpty())    {
                        tblSoftware.setItems(data);
                        return;
                    }
                    ObservableList<Software> tableItems = FXCollections.observableArrayList();
                    ObservableList<TableColumn<Software, ?>> cols = tblSoftware.getColumns();
                    for(int i=0; i<data.size(); i++)    {

                    for(int j=0; j<cols.size(); j++)    {
                        TableColumn col = cols.get(j);
                        String cellValue = col.getCellData(data.get(i)).toString();
                        cellValue = cellValue.toLowerCase();
                        if(cellValue.contains(txtFilter.textProperty().get().toLowerCase()))    {
                            tableItems.add(data.get(i));
                            break;                  
                        }
                    }

                    } 
                    tblSoftware.setItems(tableItems);
                }
            });
        }               

    } catch (SQLException e) {

        e.printStackTrace();
    }

}


protected void setIndex(int selectedIndex) {
    // TODO Auto-generated method stub

}

public void btnSoftwarePressed(){
    lblTest.setText("Button works");


}
}
软件fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java .util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.SoftwareController">
   <center>
      <TableView fx:id="tblSoftware" prefHeight="200.0" prefWidth="600.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="CI_IDcol" prefWidth="100.0" text="CI_ID" />
          <TableColumn fx:id="Namecol" prefWidth="150.0" text="Name" />
        </columns>
      </TableView>
   </center>
   <top>
      <VBox prefHeight="83.0" prefWidth="600.0" BorderPane.alignment="CENTER">
         <children>
            <HBox prefHeight="100.0" prefWidth="200.0">
               <children>
                  <Button fx:id="btnSoftware" mnemonicParsing="false" onAction="#btnSoftwarePressed" text="Button" />
                  <Label fx:id="lblTest" prefHeight="28.0" prefWidth="158.0" text="Label" />
                  <ParallelCamera />
               </children>
            </HBox>
            <HBox>
               <children>
                  <TextField fx:id="txtFilter" />
               </children>
            </HBox>
         </children>
      </VBox>
   </top>
</BorderPane>

我不确定您到底想做什么,但我可以告诉您我将db中的数据插入tableview的方法,然后使用加载到tableview中的以前的数据从db中获取数据

我首先在控制器内创建了一个内部类,它将表示要保存在数据库中/从数据库加载的实例:

public static class Detector {
    private String name;
    private String conn_type;
    private int num_detect;
    private String serial_port;
    private int data_bits;
    private int stop_bits;
    private String parity;
    private String ip;
    private int eth_port;
    private int historic;

    public Detector(String name, String conn_type, int num_detect, String serial_port, int speed,
        int data_bits, int stop_bits, String parity, String ip, int eth_port, int his){

        this.name = name;
        this.conn_type = conn_type;
        this.num_detect = num_detect;
        this.serial_port = serial_port;
        this.data_bits = data_bits;
        this.stop_bits = stop_bits;
        this.parity = parity;
        this.ip = ip;
        this.eth_port = eth_port;
        this.historic = his;
    }
}
之后,我宣布了tableview

public class Controller implements Initializable {
   @FXML
   private TableView<Detector> detectors; 
   .
   .
   .

仍然无法使控制器中的示例正常工作。我知道的不多,不容易纠正这个问题?我只想选择一行,并从我选择的列中提取数据,而不管我在该行的何处单击。提前谢谢,谢谢,我会看的。我基本上有一个配置管理数据库,为了让它保持简单,我将最新的ci_id和名称拖到tablevie TBL软件中,谢谢。我基本上有一个配置管理数据库,现在据我所知,我在上次编辑日期前拉了两列。ci_id和名称。假设我将2个结果拉到tableview,第一行ci_id 1:name windows server 1,第二行ci_id 2:name Linux server 5。因此,用户需要Linux server 5的历史记录并单击该行。我需要侦听器选择该行的ci_id或名称,将其传递给变量,我可以使用该变量运行另一个SQL查询,以获取Linux server 5的所有更改历史。似乎是tableview上的GetSelectModel没有正确地提供数据?另外,如果您查看softwarecontroller中注释掉的示例1,如果我能够说只是显示列ci_id或名称,那么这将返回我单击的任何单元格上的数据coreclty。
public class Controller implements Initializable {
   @FXML
   private TableView<Detector> detectors; 
   .
   .
   .
    DBConnection c = new DBConnection();
    c.connect();
    try{
        String sql = "select * from detector order by name";
        ResultSet rs = c.query(sql);

        ObservableList<Detector> data = FXCollections.observableArrayList();
        while(rs.next()){               
            data.add(new Detector(rs.getString("name"),
                                    rs.getString("conn_type"),                                     
                                    Integer.parseInt(rs.getString("num_detect")),
                                    rs.getString("serial_port"),
                                    Integer.parseInt(rs.getString("speed")),
                                    Integer.parseInt(rs.getString("data_bits")),
                                    Integer.parseInt(rs.getString("stop_bits")),
                                    rs.getString("parity"),
                                    rs.getString("ip"),
                                    Integer.parseInt(rs.getString("puerto_socket")),
                                    Integer.parseInt(rs.getString("historico"))
            ));
        }
        TableColumn colName = new TableColumn("Name");
        colName.setCellValueFactory(new PropertyValueFactory<Detector, String>("name"));
        detectors.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> loadName(newValue));
        detectors.setItems(data);
        detectors.getColumns().addAll(nombreCol);           
        //Add a column for every column data you want to show
private void loadName(Detector r){
    //here you could, for example, generate a sql query with the data received in r
}