JavaFX使用List <label>比较用于将文本设置为标签的结果集显示空白标签

时间:2018-01-25 06:39:55

标签: javafx

如何使用数据库中的if-else语句将setText设置为labelList。我尝试将查询的结果集与labelList进行比较,这样我就可以将文本设置为带有匹配标签Id的标签,但是在labelList上进行迭代 示...

Label[id=labelts, styleClass=label]'' 
Label[id=labeltt, styleClass=label]''  
Label[id=labeltu, styleClass=label]'' 
Label[id=labeltv, styleClass=label]''

这是问题的FXML文件。

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

    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.VBox?>
    <?import java.util.ArrayList?>

    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="randomvar.FXMLDocumentController">
        <VBox layoutX="250.0" layoutY="83.0" spacing="5.0">
            <children>
                <Label fx:id="labelts" prefWidth="100.0" style="-fx-border-color: gray;" />
                <Label fx:id="labeltt" prefWidth="100.0" style="-fx-border-color: gray;" />
                <Label fx:id="labeltu" prefWidth="100.0" style="-fx-border-color: gray;" />
                <Label fx:id="labeltv" prefWidth="100.0" style="-fx-border-color: gray;" />

                <fx:define>
                    <ArrayList fx:id="labelList" >
                        <fx:reference source="labelts"/>
                        <fx:reference source="labeltt"/>
                        <fx:reference source="labeltu"/>
                        <fx:reference source="labeltv"/>
                    </ArrayList>
                </fx:define>          
            </children>
        </VBox>
    </AnchorPane>

这是Controller文件,如何让脚本显示标签ID&#34; labelts&#34;而不是显示&#34;标签[id = labelts,styleClass = label]&#34;

    FXMLDocumentController
    package randomvar;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import javafx.fxml.FXML;
    import javafx.scene.control.Label;

    public class FXMLDocumentController {

        @FXML
        private List<Label> labelList;

        public void initialize() throws ClassNotFoundException, SQLException {

            Connection conn = null;

            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:C:\\test.db");

            String qry = "Select caller, Count(callout) as total from tbldata where caller = ?";

            PreparedStatement ps_qry = conn.prepareStatement(qry);
            ps_qry.setString(1, "ts");
            ResultSet rs_qry = ps_qry.executeQuery();
            while (rs_qry.next()) {

                String name_ = rs_qry.getString("caller");
                String total_ = String.valueOf(rs_qry.getInt("total"));

                for (int i = 0; i < labelList.size(); i++) {
                    System.out.println(labelList.get(i));                        
                    if (labelList.get(i).equals(name_)) {
                        labelList.get(i).setText(total_);
                    }
                }
            }
            rs_qry.close();
            ps_qry.close();
            conn.close();
        }
    }

2 个答案:

答案 0 :(得分:1)

谢谢你Dmytro ......我使用了你的解决方案......

    for (int i = 0; i < labelList.size(); i++) {

        if (labelList.get(i).getId().contains(name_)) {
            System.out.println(labelList.get(i).getId());
            labelList.get(i).setText(total_);
        }

答案 1 :(得分:0)

此条件始终为false,因为您使用不同的对象类型:LabelString

if (labelList.get(i).equals(name_)) {

尝试这个变种,你得到一个id:

if (labelList.get(i).getId().equals(name_)) {

仅打印标签ID:

System.out.println(labelList.get(i).getId());