我尝试了以下代码来创建动态TextField并将其添加到ScoxPane中的VBox中。但是我面临的问题是,如果添加的TextField数量超过了在ScrollPane中看不到TextField的数量。
行scrollPane.setVvalue(Double.MAX_VALUE);没有用。
ScrollPaneDemo.java
package scrollpanedemo;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author vikassingh
*/
public class ScrollPaneDemo extends Application {
VBox vboxInner = new VBox();
ScrollPane scrollPane = new ScrollPane();
List<TextFieldClass> textfieldClass = new ArrayList<TextFieldClass>();
@Override
public void start(Stage primaryStage) {
VBox.setVgrow(vboxInner, Priority.ALWAYS);
vboxInner.setPrefHeight(200);
vboxInner.getChildren().clear();
textfieldClass.clear();
newRow();
scrollPane.setContent(vboxInner);
Scene scene = new Scene(scrollPane, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void newRow() {
// Add row
textfieldClass.add(new TextFieldClass(new TextField()));
TextField newTextFileld = null;
HBox productDHbox = null;
for (TextFieldClass ctObject : textfieldClass) {
newTextFileld = ctObject.getTextField();
}
vboxInner.getChildren().add(newTextFileld);
// Rate TextField Listner
newTextFileld.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode().equals(KeyCode.ENTER) || event.getCode().equals(KeyCode.TAB) || event.getCode().equals(KeyCode.RIGHT)) {
// Add new TextField
newRow();
//
//**** Ensure Visibility of last TextField (Not working)
scrollPane.setVvalue(Double.MAX_VALUE);
//***** Ensure Visibility of last TextField (Not working)
//
textfieldClass.get(textfieldClass.size() - 1).getTextField().requestFocus();
event.consume();
}
});
}
}
TextFieldClass.java
package scrollpanedemo;
import javafx.scene.control.TextField;
/**
*
* @author vikassingh
*/
class TextFieldClass {
private final TextField newTextFiled;
TextFieldClass(TextField newTextFiled) {
this.newTextFiled = newTextFiled;
}
// First textfield
public TextField getTextField() {
return newTextFiled;
}
}
答案 0 :(得分:0)
我只是在条件阻塞之后移动你的setVvalue()语句。它没有完全解决您的问题,但是当用户输入内容时,最后一个文本字段将自动显示。
newTextFileld.addEventFilter(KeyEvent.KEY_PRESSED, event ->
{
if (event.getCode().equals(KeyCode.ENTER) || event.getCode().equals(KeyCode.TAB)
|| event.getCode().equals(KeyCode.RIGHT))
{
// Add new TextField
newRow();
textfieldClass.get(textfieldClass.size() - 1).getTextField().requestFocus();
event.consume();
}
scrollPane.setVvalue(Double.MAX_VALUE);
});