답변
바로 시작할 수 있도록 다음은 ContextMenu을 사용하는 작은 예입니다. ContextMenu
이이 시나리오에 대한 이상적인 컨트롤이 아니며 Label, Text 등으로 자유롭게 바꿀 수 있습니다. TextField
및 Label
은 HBox (처음에는 레이블이 숨겨 짐)에 넣고 HBox는 GridPane
에 보관하십시오. 또는 다른 방법을 함께 사용할 수 있습니다!
CSS로 유효성 검사 메시지/컨트롤을 디자인 할 수 있습니다!
그냥 추가 (당신이 알고있을 수 있음) :
기능적, 그것은 따라 달라에, 당신이 당신의 검증 조건이 있어야한다 무엇 검증 및 을 트리거 할 할 때 . 내 예에서는 비어있는 텍스트 필드을 확인하고 의 버튼을 클릭하여 유효성 검사를 트리거했습니다. 비슷한 시나리오 또는 에서 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;
}
여기서 한 가지 문제를 발견했습니다. 텍스트를 입력하지 않고 버튼을 클릭하면 유효성 확인 메시지가 표시됩니다. 이 시간 동안 다른 소프트웨어를 열면 파이어 폭스에 대한 유효성 검사 메시지가 표시됩니다. JavaFX 응용 프로그램을 숨기면 어떻게 메시지를 숨길 수 있습니까? – user1285928
내 Mac에서 javafx 2.2 (jdk 1.7.0_51)의 문제를 복제 할 수 없습니다. – ItachiUchiha
최신 Java 8을 사용하고 있습니다. Java 8의 버그 일 수 있습니까? – user1285928
- 1. 젠드 유효성 검사기 조기 메시지
- 2. Hibernate 유효성 검사기 메시지 지우기
- 3. jquery 유효성 검사기 HTML 양식
- 4. 양식 생성 후 양식 유효성 검사기 업데이트
- 5. 검도 숫자 입력란 범위 유효성 검사기 메시지
- 6. Laravel 4의 유효성 검사기 오류 메시지 검색
- 7. 플렉스 4 스파크, 유효성 검사기 메시지
- 8. 필수 유효성 검사기 오류 메시지 표시
- 9. jquery 유효성 검사기 addmethod 동적 메시지
- 10. symfony2 기본 유효성 검사기 메시지 변경
- 11. jQuery 도구 유효성 검사기 오류 메시지 위치
- 12. 각도 2 비동기 양식 유효성 검사기
- 13. 젠드 양식 : 사용자 정의 유효성 검사기 isvalid()
- 14. 양식 유효성 검사 후 메시지
- 15. 양식의 유효성 검사기 도시
- 16. 맞춤 비교 유효성 검사기
- 17. JQuery - 유효성 검사기 플러그인
- 18. 자바 스크립트 양식 검사기
- 19. 커스텀 jQuery 검사기 메시지
- 20. Symfony2 검사기 메시지
- 21. Liferay 합금 -ui 유효성 검사기
- 22. ASP.NET 유효성 검사기 확장
- 23. 웹 양식 검사기 레이아웃 문제
- 24. 다른 jquery 유효성 검사기 버그?
- 25. WCF 메시지 검사기
- 26. 빈 필드의 양식 검사기
- 27. Zend_Form에 다중 유효성 검사기 추가하기
- 28. Umbraco에서 실행되는 폼 유효성 검사기
- 29. Zend Framework의 양식 요소에 정규식 유효성 검사기 추가
- 30. jQuery 검사기, 암호 필드 및 메시지 재설정
가능 중복 [자바 FX - 필드 검증 (http://stackoverflow.com/questions/18825436/ javafx-field-validation) – Perneel
Javafx 표준 구성 요소를 사용하여 이러한 사용자 지정 구성 요소를 만들 수 있습니다. 사용할 수있는 이러한 맞춤 구성 요소를 인식하지 못하고 있지만 필요에 따라 만들 수 있습니다. – ItachiUchiha
두 번째 옵션 (필드 팝업) : [ControlsFX PopOver] (http://fxexperience.com/controlsfx/features/#popover) – Jurgen