使用下一个和上一个按钮Javafx显示ArrayList

时间:2018-05-16 02:38:41

标签: java javafx arraylist

我正在做一个程序,我在其中创建一个名为City

的对象的Arraylist
    City c1 = new City("Miami", "Florida", 33175, -80.4068, 25.7341);
    City c2 = new City("Seattle", "Washington", 98109, -122.3476, 47.6339);
    City c3 = new City("Levittown", "Pennsylvania", 19058, -75.1028, 40.3286);
    City c4 = new City("Tampa", "Florida", 33625, -82.559, 28.0726);
    City c5 = new City("Topeka", "Kansas", 66617, -95.6384, 39.1271);

    ArrayList<City> cities = new ArrayList<City>();

    cities.add(c1);
    cities.add(c2);
    cities.add(c3);
    cities.add(c4);
    cities.add(c5);

我需要使用下一个和上一个按钮逐个显示它。 目前我有一个for循环通过ArrayList创建标签和文本字段并从增量中获取类数据(i) 这是代码:

 for (int i = 0; i < cities.size(); i++) {


    GridPane pane = new GridPane();
    pane.setHgap(5);
    pane.setVgap(5);
    pane.setPadding(new Insets(25,25,25,25));


    Label label1 = new Label("City:");
    TextField textField1 = new TextField (cities.get(i).getCity());
    pane.add(label1, 0, 1);
    pane.add(textField1, 1, 1);

    Label label2 = new Label("State:");
    TextField textField2 = new TextField (cities.get(i).getState());
    pane.add(label2, 0, 2);
    pane.add(textField2, 1, 2);

    Label label3 = new Label("Zip Code:");
    TextField textField3 = new TextField (String.valueOf(cities.get(i).getpostalCode()));
    pane.add(label3, 0, 3);
    pane.add(textField3, 1, 3);

    Label label4 = new Label("Longitude:");
    TextField textField4 = new TextField (String.valueOf(cities.get(i).getLongitude()));
    pane.add(label4, 0, 4);
    pane.add(textField4, 1, 4);

    Label label5 = new Label("Longitude:");
    TextField textField5 = new TextField (String.valueOf(cities.get(i).getLatitude()));
    pane.add(label5, 0, 5);
    pane.add(textField5, 1, 5);

    Button prev = new Button("Previous");
    pane.add(prev, 0, 20);

    Button next = new Button("Next");
    pane.add(next, 2, 20); 

    // buttons are work in progress
    next.setOnAction((ActionEvent e) -> {


            cities.get(i).getCity(); 
            textField1.setText(cities.get(i).getCity());



     });

    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(pane);

    Scene scene = new Scene(borderPane, 500, 500);
    primaryStage.setTitle("Cities");
    primaryStage.setScene(scene); 
    primaryStage.show(); 

    }

我可以在没有ForLoop的情况下显示它,但我不知道如何从这里开始。另外,如何在循环中实现我的上一个/下一个按钮setOnActions?

谢谢!

2 个答案:

答案 0 :(得分:2)

最好将显示城市的节点移动到新类。通过这种方式,您可以更轻松地分离问题。 (在实施导航时,您不需要考虑城市显示,反之亦然。)

public class CityView extends GridPane {

    public CityView() {
        setHgap(5);
        setVgap(5);
        setPadding(new Insets(25, 25, 25, 25));

        Label label1 = new Label("City:");
        TextField cityText = new TextField();

        Label label2 = new Label("State:");
        TextField stateText = new TextField();

        Label label3 = new Label("Zip Code:");
        TextField zipCodeText = new TextField();

        Label label4 = new Label("Longitude:");
        TextField longitudeText = new TextField();

        Label label5 = new Label("Longitude:");
        TextField latitudeText = new TextField();

        addColumn(0, label1, label2, label3, label4, label5);
        addColumn(1, cityText, stateText, zipCodeText, longitudeText, latitudeText);

        this.city.addListener((o, oldValue, newValue) -> {
            // fill TextFields with new city values
            String name = null;
            String state = null;
            String zipCode = null;
            String longitude = null;
            String latitude = null;

            if (city != null) {
                name = newValue.getCity();
                state = newValue.getState();
                zipCode = String.valueOf(newValue.getpostalCode());
                longitude = String.valueOf(newValue.getLongitude());
                latitude = String.valueOf(newValue.getLatitude());
            }

            cityText.setText(name);
            stateText.setText(state);
            zipCodeText.setText(zipCode);
            longitudeText.setText(longitude);
            latitudeText.setText(latitude);
        });
    }

    private final ObjectProperty<City> city = new SimpleObjectProperty<>(this, "city");

    public ObjectProperty<City> cityProperty() {
        return city;
    }

    public City getCity() {
        return cityProperty().get();
    }

    public void setCity(City city) {
        cityProperty().set(city);
    }

}
@Override
public void start(Stage primaryStage) {
    ObservableList<City> cities = FXCollections.observableArrayList(
            new City("Miami", "Florida", 33175, -80.4068, 25.7341),
            new City("Seattle", "Washington", 98109, -122.3476, 47.6339),
            new City("Levittown", "Pennsylvania", 19058, -75.1028, 40.3286),
            new City("Tampa", "Florida", 33625, -82.559, 28.0726),
            new City("Topeka", "Kansas", 66617, -95.6384, 39.1271));

    IntegerProperty index = new SimpleIntegerProperty();
    IntegerBinding displayIndex = index.add(1);
    IntegerBinding size = Bindings.size(cities);
    size.addListener((o, oldValue, newValue) -> {
        // keep index valid when cities are removed (not necessary for lists that are not modified)
        int newSize = newValue.intValue();
        int i = index.get();
        if (i+1 >= newSize) {
            index.set(Math.max(0, newSize - 1));
        }
    });

    CityView cityView = new CityView();
    cityView.cityProperty().bind(Bindings.valueAt(cities, index));

    Button last = new Button("<");
    last.setOnAction(evt -> {
        index.set(index.get() - 1);
    });
    last.disableProperty().bind(index.lessThanOrEqualTo(0));

    Button next = new Button(">");
    next.setOnAction(evt -> {
        index.set(index.get() + 1);
    });
    next.disableProperty().bind(displayIndex.greaterThanOrEqualTo(size));

    Label indexLabel = new Label();
    indexLabel.setMaxSize(Region.USE_PREF_SIZE, Double.MAX_VALUE);
    indexLabel.setAlignment(Pos.CENTER_RIGHT);
    indexLabel.setPrefWidth(20);
    indexLabel.textProperty().bind(displayIndex.asString());

    HBox navigation = new HBox(10, last, indexLabel, next);
    navigation.setFillHeight(true);

    VBox root = new VBox(cityView, navigation);

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.show();
}

答案 1 :(得分:1)

一个完整的最小样本将有助于回答,但正如你没有提供这个我只是描述你必须做的事情:

  • 摆脱for循环,
  • 创建一个接受城市并且可以访问ArrayList城市的方法(静态?或作为此方法的参数
  • 通过UI
  • 将城市的值分配给节点
  

这段代码是我写的,所以它可能无法编译,但它只是为了给你一个方向

// Note that you could also do this by using the index, instead of city...
public void set(final City city) {
    // assign values to the Nodes of the UI
    // ...
    // update the onAction Event for the NextButton
    getNextButton().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            //getNextIndexOf returns the index of the City+1 (if possible)
            set(cities.get(getNextIndexOf(city));
        }
    });
    // update the onAction Event for the PreviousButton
    // do the equivalend as for the NextButton but decreasing the index
}