如何使用fxml元素上的drop事件?(javafx)
我已经启动了一个fxml应用程序,我发现很难找到相关文档。我想将一个文件拖到我的fxml应用程序上,特别是一个选项卡窗格,并获取文件的位置。我是fxml领域的新手,所以请尽量简单 fxmlcontroller如何使用fxml元素上的drop事件?(javafx),java,event-handling,javafx,fxml,Java,Event Handling,Javafx,Fxml,我已经启动了一个fxml应用程序,我发现很难找到相关文档。我想将一个文件拖到我的fxml应用程序上,特别是一个选项卡窗格,并获取文件的位置。我是fxml领域的新手,所以请尽量简单 fxmlcontroller public class FXMLDocumentController implements Initializable { @FXML protected void onDropEndEvent(ActionEvent event) { System.out.pri
public class FXMLDocumentController implements Initializable {
@FXML protected void onDropEndEvent(ActionEvent event) {
System.out.println(event.toString());
}
@FXML protected void onDragEvent(ActionEvent event) {
System.out.println("stuff happened");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
fxmldocument
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="485.0" prefWidth="577.0" style="-fx-background-color: #c4c4c4; -fx-border-radius: 0px;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="craigencdec.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button layoutX="458.0" layoutY="13.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="110.0" style="-fx-border-radius: 1 1 1 1; -fx-background-color: #4A4DFF; -fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 0); -fx-background-radius: 0 0 0 0;" text="Do Stuff" textFill="WHITE">
<font>
<Font name="Ubuntu" size="19.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<TextField layoutX="11.0" layoutY="13.0" prefHeight="45.0" prefWidth="441.0" promptText="Enter your key here" style="-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 0); -fx-border-radius: 0 0 0 0; -fx-background-radius: 0 0 0 0;">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<font>
<Font name="Ubuntu" size="19.0" />
</font>
</TextField>
<ProgressBar layoutX="332.0" layoutY="453.0" prefHeight="24.0" prefWidth="236.0" progress="0.06" style="-fx-background-radius: 0 0 0 0; -fx-border-radius: 0 0 0 0;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</ProgressBar>
<TabPane layoutY="69.0" onDragDetected="#onDragEvent" onDragDropped="#onDropEndEvent" prefHeight="416.0" prefWidth="325.0" style="-fx-background-color: #FFFFFF;" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab closable="false" text="Encode" />
<Tab closable="false" text="Decode" />
</tabs>
</TabPane>
<Label alignment="CENTER" layoutX="332.0" layoutY="70.0" prefHeight="33.0" prefWidth="236.0" style="-fx-border-radius: 3 3 3 3; -fx-background-color: #4A4DFF; -fx-background-radius: 0 0 0 0;" text="Status" textFill="WHITE">
<font>
<Font size="27.0" />
</font>
</Label>
<Circle fill="WHITE" layoutX="344.0" layoutY="120.0" radius="12.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" />
<Label alignment="TOP_LEFT" layoutX="369.0" layoutY="110.0" prefHeight="75.0" prefWidth="200.0" text="drag a file into a tab to encode or decode it" textFill="WHITE" textOverrun="CLIP" wrapText="true" />
<LineChart layoutX="333.0" layoutY="199.0" prefHeight="243.0" prefWidth="236.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis prefHeight="258.0" prefWidth="24.0" side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>
如果使用的是场景生成器,请转到“代码”部分,并在“拖放时的文本”字段中,将要在该节点上拖放内容时处理拖动事件的方法的名称放入控制器中 然后在控制器中创建此函数的主体,以便在发生丢弃时调用
@FXML private void somethingDropped(Event event) {
System.out.println("something dropped!");
//TODO More useful code
}
@FXML private void somethingDropped(Event event) {
System.out.println("something dropped!");
//TODO More useful code
}