2017-05-21 6 views
0

ボタンイベントはテキストフィールドに出力を書き込む方法は、ボタンがのhboxで、もう1つがテキストフィールドであるため、borderpaneを使用しているためです。 アクションイベントの最後に、ボタンはテキストフィールドに選択したファイルの署名を書き込む必要があります この問題の解決方法は何ですか?ボタンイベントをテキストフィールドに出力する方法

public class Filrsystemencryption extends Application 
{ 

private HBox getHBox() 
{ 
HBox hbButtons = new HBox(15); 
Button btnimport = new Button("import"); 
TextField textfieldd = new TextField(); 
    btnimport.setOnAction((event) -> 
    { 



    btnimport.setOnAction(new EventHandler<ActionEvent>() 
    { 

     @Override 
     public void handle(ActionEvent event) { 

JButton open = new JButton(); 
JFileChooser fc = new JFileChooser(); 
fc.setCurrentDirectory(new java.io.File("C:/Users/hannah/Desktop")); 
fc.setDialogTitle("choose a file"); 
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){ 
    textfieldd.setText("file chosen"); 
} 
     } 

    }); 

Button btnDelete = new Button("Remove"); 
TextField textfield = new TextField(); 

final ComboBox ComboBox = new ComboBox(); 
    ComboBox.getItems().addAll(
     "Encrypt", 
     "Decrypt" 
    ); 

Label label = new Label("password"); 
hbButtons.setSpacing(30); 
hbButtons.setPadding(new Insets(10, 20, 30, 20)); 
hbButtons.getChildren().addAll(btnimport, btnDelete, 
label,textfield,ComboBox); 
    return hbButtons ; 
} 


    @Override 
    public void start(Stage primaryStage) { 


    BorderPane pane = new BorderPane(); 
    pane.setTop(getHBox()); 
    pane.setCenter(getHBoxx()); 


primaryStage.setTitle("File system encryption"); 
Scene scene = new Scene(pane, 600, 600); 
primaryStage.setScene(scene); 
primaryStage.show(); 
} 
    private TextField getHBoxx() { 

TextField textfieldd = new TextField(); 
textfieldd.setPrefWidth(400); 
textfieldd.setPrefHeight(200); 
return textfieldd;  
    } 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    launch(args); 
    } 

    } 
+0

なぜ、このアプリではJavaFXのクラスでボタンとJFileChooserのようなSwingクラスをミックス?それは間違っているだけです。 – jewelsea

+0

[アイデアを使用してコードを書式設定する](https://www.jetbrains.com/help/idea/2017.1/reformatting-source-code.html)は、そのテクニックを使用して投稿したコードに対して行うか、ソースエディタに適用可能なものです。 – jewelsea

答えて

1

JavaFXでSwingクラスを使用すると、実際には必要ないときに両方のプラットフォームのメインスレッドに問題が発生します。 (この場合のように)ファイル用やディレクトリのためのより良い使用FileChooserDirectoryChooser

btnimport.setOnAction(evt -> { 
    DirectoryChooser dirChooser = new DirectoryChooser(); 
    dirChooser.setInitialDirectory(new java.io.File("C:/Users/hannah/Desktop")); 
    dirChooser.setTitle("choose a file"); 

    File choice = dirChooser.showDialog(btnimport.getScene().getWindow()); 

    if (choice != null) { 
     // dialog not aborted 
     textfieldd.setText("file chosen"); 
    } 
}); 
+0

私はこの関数を書くべきですか? –

+1

自分のコードで、壊れた 'setOnAction'パーツ(naomymusクラスとラムダ式を使ってみてください)を置き換えてください。 – fabian

関連する問題