2014-05-10 84 views
2

我在尋找類似這些例子的形式驗證:表單驗證消息

enter image description here

enter image description here

這是自定義的JavaFX組件或者這可以使用標準的JavaFX來完成?

是否有任何示例我如何創建類似於這些的表單驗證器?

+0

的可能重複的[JavaFX的 - 字段驗證](http://stackoverflow.com/questions/18825436/ javafx-field-validation) – Perneel

+0

您可以使用Javafx標準組件製作這樣的自定義組件。我不知道有任何這樣的自定義組件可用,但您可以根據需要製作一個! – ItachiUchiha

+1

對於第二個選項(對於該字段的彈出窗口),請參閱:[ControlsFX PopOver](http://fxexperience.com/controlsfx/features/#popover) – Jurgen

回答

15

只是爲了給您一個啓程,這裏是一個小例子,使用ContextMenu。可能是ContextMenu對於這種情況不是理想的控制,你可以用Label,Text等自由替換它。保持TextFieldLabel位於HBox(最初隱藏標籤)和HBox到GridPane之內。或者,您可以一起使用任何其他方法!

您可以使用CSS設計驗證消息/控件!

我想補充(可能是你意識到這一點):

功能,這取決於你,你要觸發驗證和應您的驗證條件是什麼 。在我的示例中,我正在檢查空的Textfields,並且我已觸發驗證點擊登錄按鈕。您可以觸發驗證,在類似的情況下刪除焦點從任何TextField或其他情況!

有很多方法可以幫助您尋找什麼,並且javafx在UI控件和設計上如此豐富,完全取決於您的創造力!

ValidationDemo.java

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.geometry.Side; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ContextMenu; 
import javafx.scene.control.Label; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class ValidationDemo extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("Validation Demo"); 
     BorderPane borderPane = new BorderPane(); 

     borderPane.setCenter(loadLoginScreen()); 
     Scene scene = new Scene(borderPane, 700, 500); 
     scene.getStylesheets().add(
       ValidationDemo.class.getResource("context.css") 
         .toExternalForm()); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

    private GridPane loadLoginScreen() { 

     GridPane grid = new GridPane(); 
     grid.setAlignment(Pos.CENTER); 
     grid.setHgap(10); 
     grid.setVgap(10); 
     grid.setPadding(new Insets(25, 25, 25, 25)); 

     Text scenetitle = new Text("Welcome"); 
     scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); 
     grid.add(scenetitle, 0, 0, 2, 1); 

     Label userName = new Label("User Name:"); 
     grid.add(userName, 0, 1); 

     final TextField userTextField = new TextField(); 
     grid.add(userTextField, 1, 1); 

     Label pw = new Label("Password:"); 
     grid.add(pw, 0, 2); 

     final PasswordField pwBox = new PasswordField(); 
     grid.add(pwBox, 1, 2); 

     Button btn = new Button("Sign in"); 
     HBox hbBtn = new HBox(10); 
     hbBtn.setAlignment(Pos.BOTTOM_RIGHT); 
     hbBtn.getChildren().add(btn); 
     grid.add(hbBtn, 1, 4); 

     final Text actiontarget = new Text(); 
     grid.add(actiontarget, 1, 6); 

     // Context Menu for error messages 
     final ContextMenu usernameValidator = new ContextMenu(); 
     usernameValidator.setAutoHide(false); 
     final ContextMenu passValidator = new ContextMenu(); 
     passValidator.setAutoHide(false); 

     // Action on button press 
     btn.setOnAction(new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent e) { 
       // Clearing message if any 
       actiontarget.setText(""); 

       // Checking if the userTextField is empty 
       if (userTextField.getText().equals("")) { 
        usernameValidator.getItems().clear(); 
        usernameValidator.getItems().add(
          new MenuItem("Please enter username")); 
        usernameValidator.show(userTextField, Side.RIGHT, 10, 0); 
       } 
       // Checking if the pwBox is empty 
       if (pwBox.getText().equals("")) { 
        passValidator.getItems().clear(); 
        passValidator.getItems().add(
          new MenuItem("Please enter Password")); 
        passValidator.show(pwBox, Side.RIGHT, 10, 0); 
       } 
       // If both of the above textFields have values 
       if (!pwBox.getText().equals("") 
         && !userTextField.getText().equals("")) { 
        actiontarget.setFill(Color.GREEN); 
        actiontarget.setText("Welcome"); 
       } 
      } 
     }); 

     userTextField.focusedProperty().addListener(
       new ChangeListener<Boolean>() { 
        @Override 
        public void changed(
          ObservableValue<? extends Boolean> arg0, 
          Boolean oldPropertyValue, Boolean newPropertyValue) { 
         if (newPropertyValue) { 
          // Clearing message if any 
          actiontarget.setText(""); 
          // Hiding the error message 
          usernameValidator.hide(); 
         } 
        } 
       }); 

     pwBox.focusedProperty().addListener(new ChangeListener<Boolean>() { 
      @Override 
      public void changed(ObservableValue<? extends Boolean> arg0, 
        Boolean oldPropertyValue, Boolean newPropertyValue) { 
       if (newPropertyValue) { 
        // Clearing message if any 
        actiontarget.setText(""); 
        // Hiding the error message 
        passValidator.hide(); 
       } 
      } 
     }); 
     return grid; 
    } 

} 

context.css

.root { 
    -fx-background-color: cornsilk; 
    -fx-padding: 10; 
} 

.context-menu { 
    -fx-background-color: #006699; 
    -fx-text-fill: white; 
    -fx-padding: 0; 
} 

.context-menu:hover { 
    -fx-background-color: #006699; 
    -fx-text-fill: white; 
} 

.menu-item .label { 
    -fx-text-fill: white; 
} 

.menu-item:focused .label { 
    -fx-text-fill: white; 
} 
+0

我在這裏發現了一個問題。當我點擊按鈕而不輸入任何文本時,我會看到驗證消息。在此期間,當我打開另一個軟件時,我會看到Firefox上的驗證消息。隱藏JavaFX應用程序時如何隱藏消息? – user1285928

+0

我無法在我的mac上覆制javafx 2.2(jdk 1.7.0_51)上的問題 – ItachiUchiha

+0

我正在使用最新的Java 8.也許這是Java 8中的一個錯誤? – user1285928