如何在fxml元素上使用drop事件? (JavaFX的)

时间:2014-05-31 02:45:10

标签: java event-handling javafx fxml

我启动了fxml应用程序,我发现很难找到相关文档。我想将文件拖到我的fxml应用程序上,特别是tabpane,并获取文件的位置。我是fxml场景的新手,所以请尽量做到简单。

fxmlcontroller

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>

craigencdec

package craigencdec;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author craig
 */

    public class CraigEncDec extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);
        stage.setTitle("Encoder/Decoder");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);

    }
    private void getBits(String fileLoc){
        StringBuilder sb = new StringBuilder();
        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(""))) {
            for (int b; (b = is.read()) != -1;) {
                String s = "0000000" + Integer.toBinaryString(b);
            s = s.substring(s.length() - 8); 
            sb.append(s).append(' ');
            }
        }catch(IOException e){
            System.out.println("This is not a valid file");
        }
        System.out.println(sb);
    }

}

1 个答案:

答案 0 :(得分:1)

如果您正在使用场景构建器,请转到代码部分,并在On Drag Dropped文本字段中将方法的名称放在控制器中,以便在此节点上删除某些内容时处理拖动事件 enter image description here 然后在你的控制器中创建当发生丢弃时要调用的函数体

    @FXML private void somethingDropped(Event event) {
      System.out.println("something dropped!");
      //TODO More useful code
   }