新手问题。我试图让javafx做列表类,但是我需要添加一个按钮和textfield来将数据插入到listview中。我想添加这些,但我无法将它们直接添加到列表视图中。我不知道怎么做。
package learning;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class ToDoList extends Application{
private ObservableList<String> doList = FXCollections.observableArrayList();
private String text="";
public void start(Stage primaryStage){
primaryStage.setTitle("2DoList");
TextField userTextField = new TextField();
Button enter=new Button("Add");
enter.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
text= userTextField.getText();
doList.add(text);
userTextField.setText("");
text="";
}
});
ListView<String> root=new ListView<String>(doList);
Scene scene=new Scene(root,250,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}