如何将标签文本更改为在ListView中选择的文本?

问题描述 投票:1回答:1

我正在尝试编写一个程序,该程序允许您添加状态,然后在ListView中选择它们时将其显示在标签中。我大部分时间都在那儿,但是我无法使其正确显示。

这是我的代码:

String[] emptyStates = {""};
    @Override
    public void start(Stage primaryStage) {
        VBox leftBox = new VBox(20);
        TextField stateField = new TextField();
        stateField.setPrefSize(100, 25);
        ListView<String> lv = new ListView<> (FXCollections.observableArrayList
            (emptyStates));
        lv.setPrefSize(200, 200);
        ScrollPane scrollPane = new ScrollPane(lv);
        leftBox.getChildren().addAll(stateField, scrollPane);
        Label selectedState = new Label();
        Button addButton = new Button("Add State");
        addButton.setOnAction(e-> {
            for(int i = 0; i < emptyStates.length; i++){
                emptyStates[0] = "k";
            }
            System.out.println("test");
            lv.getItems().add("k");
        });
        addButton.setDefaultButton(true);
        lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        lv.getSelectionModel().selectedItemProperty().addListener(ov -> {
            //selectedState.setText(null);
            for (Integer i: lv.getSelectionModel().getSelectedIndices()) {
                selectedState.setText(emptyStates[i]);
            }
        });
        VBox rightBox = new VBox(200);
        rightBox.getChildren().addAll(selectedState, addButton);
        BorderPane pane = new BorderPane();
        pane.setLeft(leftBox);
        pane.setRight(rightBox);
        Scene scene = new Scene(pane, 500, 300);
        primaryStage.setTitle("State List");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
java listview javafx label javafx-8
1个回答
0
投票

[而不是使用String[] emptyStates = {""};,为什么不使用ObservableList作为开头? lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);ListView的默认值,因此可以省去代码。当按下添加Button时,如果stateField具有文本,则仅添加到ObservableList。在ListView's侦听器中,当选择更改时使用新值更新标签。代码注释。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    ObservableList<String> arraylistItems = FXCollections.observableArrayList();
    @Override
    public void start(Stage primaryStage) {
        VBox leftBox = new VBox(20);
        TextField stateField = new TextField();
        stateField.setPrefSize(100, 25);        
        ListView<String> lv = new ListView<> ();
        lv.setItems(arraylistItems);        
        lv.setPrefSize(200, 200);
        //lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);//The default is single. 

        //ScrollPane scrollPane = new ScrollPane(lv);//ListView has its own ScrollPane
        leftBox.getChildren().addAll(stateField,lv);


        Label selectedState = new Label();
        Button addButton = new Button("Add State");
        addButton.setOnAction(e-> {
            if(!stateField.getText().isBlank())//If the TextField has text add the text to the ListView
            {
                arraylistItems.add(stateField.getText());
            }
        });
        addButton.setDefaultButton(true);
        lv.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {//If the selected value changes, set the new value to the Label.
            selectedState.setText(newValue);
        });

        VBox rightBox = new VBox(200);
        rightBox.getChildren().addAll(selectedState, addButton);
        BorderPane pane = new BorderPane();
        pane.setLeft(leftBox);
        pane.setRight(rightBox);
        Scene scene = new Scene(pane, 500, 300);
        primaryStage.setTitle("State List");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.