我试图在javaFX对话框中禁用“确定”按钮,直到所有文本字段都有内容。
由于ButtonType没有FXML支持,因此必须将其添加到主窗口的Controller类中的Dialog。由于这个原因,我无法(无法找到方法)将按钮链接到对话框控制器内的变量。
我已尝试在主Controller类中处理该过程,如下所示:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("addDialog.fxml"));
try {
dialog.getDialogPane().setContent(loader.load());
} catch(IOException e) {
e.getStackTrace();
}
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
dialog.getDialogPane().lookupButton(ButtonType.OK).setDisable(true);
AddDialogController controller = loader.getController();
// below calls on a getter from the addDialogController.java file to check if the input fields are full
if (controller.getInputsFull()) {
dialog.getDialogPane().lookupButton(ButtonType.OK).setDisable(false);
}
遗憾的是,这不起作用,上面的代码只能在调用窗口之前或之后运行一次,并且不能在运行期间运行。
有没有办法在对话框控制器中访问带有javaFX的OK ButtonType,如果它已在外面声明?
或者是否有其他方法可以根据用户正在更新的对话框控制器中的信息来禁用按钮?
感谢您的帮助
编辑1:
按照addDialogController的要求,这是非常简单的骨头和不完整的,希望它有所帮助:
import data.Contact;
import data.ContactData;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class AddDialogController {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField numberField;
@FXML
private TextArea notesArea;
private boolean inputsFull;
public void processResults() {
String first = firstNameField.getText().trim();
String last = lastNameField.getText().trim();
String number = numberField.getText().trim();
String notes = notesArea.getText().trim();
Contact contact = new Contact(first, last, number, notes);
// ContactData.add(contact);
}
@FXML
public void handleKeyRelease() {
boolean firstEmpty = firstNameField.getText().trim().isEmpty() && firstNameField.getText().isEmpty();
boolean lastEmpty = lastNameField.getText().trim().isEmpty() && lastNameField.getText().isEmpty();
boolean numberEmpty = numberField.getText().trim().isEmpty() && numberField.getText().isEmpty();
boolean notesEmpty = notesArea.getText().trim().isEmpty() && notesArea.getText().isEmpty();
inputsFull = !firstEmpty && !lastEmpty && !numberEmpty && !notesEmpty;
System.out.println(firstEmpty);
System.out.println(lastEmpty);
System.out.println(numberEmpty);
System.out.println(notesEmpty);
System.out.println(inputsFull);
System.out.println();
}
public boolean isInputsFull() {
return this.inputsFull;
}
答案 0 :(得分:2)
首先,删除$object = "New-Object PSCustomObject -Property @{'Name' = ''; 'Age' = [int]}"
$array = 1..100 | %{Invoke-Expression $object}
$array[0].Name = 'Joe'
$array[0].Age = 12
$array[0]
方法。 从不在文本输入组件上使用关键事件处理程序:首先,如果用户使用鼠标将文本复制并粘贴到文本字段中,它们将无法工作。如果需要,只需使用handleKeyRelease
注册听众。此外,不是(例如)
textProperty()
当且仅当
时才为真firstNameField.getText().trim().isEmpty() && firstNameField.getText().isEmpty()
是真的吗?目前尚不清楚你想在那里实施什么逻辑。
您应该只将firstNameField.getText().isEmpty();
公开为JavaFX属性:
inputsFull
然后您需要的只是
public class AddDialogController {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField numberField;
@FXML
private TextArea notesArea;
private BooleanBinding inputsFull ;
public BooleanBinding inputsFullBinding() {
return inputsFull ;
}
public final boolean getInputsFull() {
return inputsFull.get();
}
public void initialize() {
inputsFull = new BooleanBinding() {
{
bind(firstNameField.textProperty(),
lastNameField.textProperty(),
numberField.textProperty(),
notesArea.textProperty());
}
@Override
protected boolean computeValue() {
return ! (firstNameTextField.getText().trim().isEmpty()
|| lastNameTextField.getText().trim().isEmpty()
|| numberField.getText().trim().isEmpty()
|| notesArea.getText().trim().isEmpty());
}
};
}
public void processResults() {
String first = firstNameField.getText().trim();
String last = lastNameField.getText().trim();
String number = numberField.getText().trim();
String notes = notesArea.getText().trim();
Contact contact = new Contact(first, last, number, notes);
// ContactData.add(contact);
}
}