Basic File I/O JavaFX FXML Application built with Scene Builder and Netbeans IDE

时间:2016-07-11 21:02:00

标签: java netbeans javafx io fxml

I have utilized Netbeans IDE 8.1 and JavaFX Scene Builder from Gluon to create a basic user interface with text fields.

User Action Pathway

  1. User types in Password.
  2. User types in the three directories in the three text fields (1. PDB..., 2. 4D..., 3. 2D...).
  3. User clicks Enter and JavaFX checks if the password is "passwd_1234" and if it is, JavaFX outputs the three directories on three separate lines in a text file (to be used by other non-Java code as inputs).

I tried learning about Java I/O (FileReader and FileWriter) but it's implementation is quite different from that of JavaFX and FXML.

FXML Code

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="292.0" prefWidth="478.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.UIController">
    <children>
      <PasswordField fx:id="Passwd" layoutX="208.0" layoutY="52.0" />
      <TextField fx:id="PDB" layoutX="208.0" layoutY="94.0" />
      <TextField fx:id="D4" layoutX="208.0" layoutY="129.0" />
      <TextField fx:id="D2" layoutX="208.0" layoutY="169.0" />
      <Button fx:id="Enter" layoutX="208.0" layoutY="215.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Enter" />
      <Text layoutX="48.0" layoutY="112.0" strokeType="OUTSIDE" strokeWidth="0.0" text="PDB File Directory" wrappingWidth="180.3525390625">
         <font>
            <Font name="Lucida Sans Regular" size="13.0" />
         </font>
      </Text>
      <Text layoutX="48.0" layoutY="147.0" strokeType="OUTSIDE" strokeWidth="0.0" text="4D NOESY Peak List" wrappingWidth="155.00000256299973">
         <font>
            <Font name="Lucida Sans Regular" size="13.0" />
         </font>
      </Text>
      <Text layoutX="48.0" layoutY="187.0" strokeType="OUTSIDE" strokeWidth="0.0" text="2D HSQC Peak List" wrappingWidth="142.3525390625">
         <font>
            <Font name="Lucida Sans Regular" size="13.0" />
         </font>
      </Text>
      <Text layoutX="48.0" layoutY="70.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password" wrappingWidth="180.3525390625">
         <font>
            <Font name="Lucida Sans Regular" size="13.0" />
         </font>
      </Text>
    </children>
</AnchorPane>

Java Code

package ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class UI extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("UI.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    } 
}

Java Controller Code

package ui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
public class UIController implements Initializable {  
    private Label label;
    @FXML
    private PasswordField Passwd;
    @FXML
    private TextField PDB;
    @FXML
    private TextField D4;
    @FXML
    private TextField D2;
    @FXML
    private Button Enter;
    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }
}

Please assist me in the Java code (I can auto-generate the controller using Source > Make Controller in Netbeans) to implement the User Action Pathway. Thanks.

1 个答案:

答案 0 :(得分:0)

您可以使用PrintWriter。要检查密码是否相同,只需使用getText()并使用equals

您创建文件并写入:

PrintWriter writer = new PrintWriter(new FileWriter(file));

然后你阅读每个领域并做  writer.println(directoryName);

不要忘记使用writer.flush();,因为默认情况下它不是自动的。

- &gt; Oracle tutorial更好地解释了它。

使用和try with resources