2016-11-15 94 views
0

我在新的JavaFX應用程序中使用JFoenix。我已經成功創建了我自己的消息對話框。從JFoenix Dialog獲取響應

package Dialog; 

import com.jfoenix.controls.JFXButton; 
import com.jfoenix.controls.JFXDialog; 
import com.jfoenix.controls.JFXDialogLayout; 
import com.jfoenix.controls.events.JFXDialogEvent; 
import javafx.application.Platform; 
import javafx.beans.property.BooleanProperty; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.event.EventType; 
import javafx.geometry.HPos; 
import javafx.geometry.Insets; 
import javafx.geometry.VPos; 
import javafx.scene.control.Label; 
import javafx.scene.input.KeyCode; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 


public class OK_Message extends JFXDialog { 

    private StackPane Container; 
    private JFXDialogLayout Content; 
    String DialogText; 
    String Headline; 

    JFXButton btn; 
    boolean OK = false; 

    private BooleanProperty okval; 

    public OK_Message(
      StackPane Container, 
      String Headline, 
      String DialogText 
    ){ 
     this.Container = Container; 
     this.DialogText = DialogText; 
     this.Headline = Headline; 
    } 

    public void ShowDialog(){ 
     setDialogContainer(Container); 
     setContent(getDialogContent()); 
     setTransitionType(JFXDialog.DialogTransition.TOP);  
     setOverlayClose(false); 


     setFocusTraversable(true); 
     setOnDialogOpened((JFXDialogEvent event) -> { 
      Platform.runLater(()->{ 
      btn.requestFocus(); 
     });}); 


     setOnKeyPressed((KeyEvent e)->{ 
     e.consume(); 
     if(e.getCode()== KeyCode.ENTER){close();} 
     else if(e.getCode()== KeyCode.SPACE){btn.requestFocus();} 
     else{} 
     }); 

     show(); 
    } 

    private JFXDialogLayout getDialogContent(){ 
     Content = new JFXDialogLayout(); 
     Content.setHeading(HeadLine()); 
     Content.setBody(Body()); 


     // Platform.runLater(()->{b.requestFocus();}); 

     Content.setActions(getButton()); 
     return Content; 
    } 

    private Label HeadLine(){ 
     Label l = new Label(Headline); 
     l.setFont(new Font(18)); 
     return l; 
    } 

    private GridPane Body(){ 
     Label l = new Label(DialogText); 
     l.setFont(new Font(14)); 

     GridPane grid = new GridPane(); 
     grid.setHgap(10); 
     grid.setVgap(8); 
     GridPane.setConstraints(l, 0, 0, 1, 1, HPos.LEFT, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS, new Insets(0, 0, 0, 0)); 
     grid.getChildren().addAll(l); 
     return grid; 
    } 

    private JFXButton getButton(){ 
    btn = new JFXButton("OK"); 
    btn.setButtonType(JFXButton.ButtonType.FLAT); 
    btn.setPrefWidth(50); 
    btn.setTextFill(Color.WHITE); 
    btn.setOnAction((ActionEvent event) -> { 
     CloseDialog(); 
    }); 
    btn.setStyle("-fx-background-color:#FFFFFF"); 
    btn.focusedProperty().addListener(new ChangeListener<Boolean>(){ 
     @Override 
     public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
      if(newValue){ 
      btn.setStyle("-fx-background-color:red"); 
      } 
     } 
    }); 
    return btn; 
    } 

    private void CloseDialog(){ 
    close(); 
    } 

} 

現在我可以打電話給我的任何地方對話:是這樣的:

Dialog.OK_Message message = 
new Dialog.OK_Message((StackPane)app_setup.getParent(), 
       "Message", 
       "Changes will be affected after restart."); 
    message.ShowDialog(); 

我無法找到一個方法來檢測是否已單擊按鈕或沒有。或者用戶按下什麼鍵。如果用戶點擊了確定按鈕,我想做點什麼。或者如果使用按了一些鍵我想做其他事情?我怎樣才能聽到上面的對話框?

回答

0

您可以在您的OK_Message類上使用setOnKeyPressed類,就像您在類中使用的類一樣,因爲它擴展了JFXDialog。

message.setOnKeyPressed(new EventHandler<KeyEvent>() { 
    @Override 
    public void handle(KeyEvent event) { 
     System.out.println(event.getCode().getName()); 
    } 
}); 

收聽點擊爲什麼不創建customEventListenerfor您OK_Message類按鈕?

在你的OK_Message.java文件在類的外面創建一個接口;

interface OnEventListener { 
    void onButtonCliked(String id); 
} 

和你OK_Message類增加一個新的方法

public void setOnEventListener(OnEventListener listener) { 
    mOnEventListener = listener; 
} 

首先 讓我們改變你的getButton()方法來這樣一來創建不同的ID和文本多個按鈕的添加功能。

private JFXButton getButton(String id,String text){ 
    JFXButton btn = new JFXButton(text); 
    btn.setId(id); 
    btn.setButtonType(JFXButton.ButtonType.FLAT); 
    btn.setPrefWidth(50); 
    btn.setTextFill(Color.WHITE); 
    btn.setOnAction((ActionEvent event) -> { 
     mOnEventListener.onButtonCliked(btn.getId()); //Here we are firing the event 
     CloseDialog(); 
    }); 
    btn.setStyle("-fx-background-color:red"); 
    return btn; 
} 

getDialogContent()允許創建多個按鈕。

Content.setActions(getButton("Button1","OK"),getButton("Button2","CANCEL")); 

現在可以宣佈新OK_Message對象

OK_Message message = 
    new OK_Message((StackPane)stackpane, 
         "Message", 
         "Changes will be affected after restart."); 
message.ShowDialog(); 
message.setOnKeyPressed(new EventHandler<KeyEvent>() { 
    @Override 
    public void handle(KeyEvent event) { 
     System.out.println(event.getCode().getName()); 
    } 
}); 
message.setOnEventListener(new OnEventListener() { 
    @Override 
    public void onButtonCliked(String id) { 
     System.out.println(id); 
    } 
}); 

確定什麼做根據onButtonClicked()功能觀察到的ID後,使用監聽器。