我试图通过点击按钮打开文件打开对话框,如下面的屏幕截图所示:
单击“选择文件”按钮时,第一次单击时不会打开“文件打开”对话框。我必须再次单击它以打开文件对话框窗口。
我的代码设置如下:
Main.java
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public void start(final Stage stage) throws Exception {
Parent root = (Parent) javafx.fxml.FXMLLoader.load(getClass()
.getResource("xPressionTestClient-GUI.fxml"));
Scene scene = new Scene(root);
stage.setTitle("xPression Test Client");
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
FXML Controller.java
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
public class FXMLController {
@FXML
private Button inputXmlFileBtn;
@FXML
private TextField inputXmlName;
//private Stage savedStage;
final FileChooser fileChooser = new FileChooser();
@FXML
public void inputXmlFileChooser(){
inputXmlFileBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
//Set the path for inputXmlName text field
inputXmlName.setText(file.getPath());
}
});
//savedStage = stage;
//inputXmlName.setText("Hello");
}
FXML代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="660.0" prefWidth="782.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.lowes.xpression.application.FXMLController">
<children>
<Label layoutX="319.0" layoutY="14.0" text="xPression Test Client">
<font>
<Font name="Calibri Bold" size="16.0" />
</font>
</Label>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="565.0" layoutX="14.0" layoutY="45.0" stroke="BLACK" strokeType="INSIDE" width="748.0" />
<Label layoutX="28.0" layoutY="72.0" text="Input XML File">
<font>
<Font name="Segoe UI Bold" size="14.0" />
</font>
</Label>
<TextField fx:id="inputXmlName" layoutX="202.0" layoutY="68.0" prefHeight="26.0" prefWidth="371.0" promptText="Input XML" />
<Button fx:id="inputXmlFileBtn" layoutX="596.0" layoutY="69.0" onAction="#inputXmlFileChooser" onmnemonicParsing="false" prefHeight="25.0" prefWidth="96.0" text="Select File" />
<Label layoutX="28.0" layoutY="107.0" text="Output XML File Name">
<font>
<Font name="Segoe UI Bold" size="14.0" />
</font>
</Label>
<TextField fx:id="outputXmlName" layoutX="202.0" layoutY="103.0" prefHeight="26.0" prefWidth="371.0" promptText="Output XML File Name" />
<Label layoutX="344.0" layoutY="646.0" text="© CopyRight 2018">
<font>
<Font name="Calibri Bold Italic" size="12.5" />
</font>
</Label>
<TabPane fx:id="XmlData" layoutX="28.0" layoutY="148.0" prefHeight="448.0" prefWidth="726.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Vendor Header & FeatureBenefits">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label layoutX="29.0" layoutY="14.0" text="Vendor Header">
<font>
<Font name="Segoe UI Bold" size="14.0" />
</font>
</Label>
<Line endX="100.0" layoutX="129.0" layoutY="34.0" startX="-100.0" />
<Label layoutX="29.0" layoutY="48.0" text="Vendor Header1">
<font>
<Font name="Segoe UI Bold" size="14.0" />
</font>
</Label>
<TextField fx:id="VendorHeader1" layoutX="29.0" layoutY="71.0" prefHeight="26.0" prefWidth="305.0" promptText="VendorHeader1" />
<Label layoutX="383.0" layoutY="48.0" text="Vendor Header2">
<font>
<Font name="Segoe UI Bold" size="14.0" />
</font>
</Label>
<TextField fx:id="VendorHeader2" layoutX="383.0" layoutY="71.0" prefHeight="26.0" prefWidth="305.0" promptText="VendorHeader2" />
<ComboBox fx:id="envCombo1" layoutX="29.0" layoutY="340.0" prefWidth="150.0" promptText="Select Environment">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="DEV" />
<String fx:value="SIT" />
<String fx:value="UAT" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label text="Vendor Header1">
<font>
<Font name="Calibri Bold" size="14.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</Pane>
应该修复哪些问题才能解决上述问题?