试图设置textField的文本没有运气

时间:2015-01-27 04:17:18

标签: javafx fxml

我正在尝试从数据库中读取数据,并在单击按钮时将其设置为文本字段中的文本。我不能为我的生活弄清楚为什么这段代码不起作用。任何帮助表示赞赏。标签有效,而textfield则没有。它们位于同一个锚窗格中。

这是我的FXMLcontroller.java文件中的代码。我使用SceneBuilder来创建UI。

package winfin_test;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 *
 * @author Sam
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    private TextField textField1 = new TextField();


    @FXML
    private void handleButtonData(ActionEvent event) {

            try {
            //Connect to the database
            String host = "jdbc:mysql://localhost:3306/my_database";
            String uName = "root";
            String uPass = "data";
            Connection con = DriverManager.getConnection(host, uName, uPass);

            //Execute some SQL and load the records into the resultset
            Statement stmt = con.createStatement();
            String SQL = "Select * FROM data_test";
            ResultSet rs = stmt.executeQuery(SQL);

            //Move the cursor to the first record and get data
            rs.next();
            int id_col = rs.getInt("Auto_ID");
            String id = Integer.toString(id_col);
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dob = rs.getString("Birthday");
            String phone = rs.getString("Phone");

            //Display the first record in the text fields
              label.setText(first);
              textField1.setText(last);
        }
        catch (SQLException err) {
            System.out.println(err.getMessage());
        }
        System.out.println("You clicked me!");
        //label.setText("Well Done!");
    }



    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }    

}

1 个答案:

答案 0 :(得分:1)

问题是你永远不会将textField添加到场景中,你的Label有@FXML标签,但是你想要动态创建的textField,但从不显示。而是在.fxml文档中定义文本字段,然后将代码编辑为以下内容:

package winfin_test;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 *
 * @author Sam
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    @FXML
    private TextField textField1;


    @FXML
    private void handleButtonData(ActionEvent event) {

            try {
            //Connect to the database
            String host = "jdbc:mysql://localhost:3306/my_database";
            String uName = "root";
            String uPass = "data";
            Connection con = DriverManager.getConnection(host, uName, uPass);

            //Execute some SQL and load the records into the resultset
            Statement stmt = con.createStatement();
            String SQL = "Select * FROM data_test";
            ResultSet rs = stmt.executeQuery(SQL);

            //Move the cursor to the first record and get data
            rs.next();
            int id_col = rs.getInt("Auto_ID");
            String id = Integer.toString(id_col);
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dob = rs.getString("Birthday");
            String phone = rs.getString("Phone");

            //Display the first record in the text fields
              label.setText(first);
              textField1.setText(last);
        }
        catch (SQLException err) {
            System.out.println(err.getMessage());
        }
        System.out.println("You clicked me!");
        //label.setText("Well Done!");
    }



    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }    

}

我知道你需要在链接到fx:id的每个变量声明之前编写@FXML似乎很愚蠢,但这就是它的样子。如果您有多个相同类型的变量(例如:一组标签),您只需要将它放一次然后用逗号分隔,如下所示:

@FXML
Label label1, label2, label3, label4;

这为您节省了一些代码。