0

你能帮我修复这段代码吗?它没有像我预期的那样正常工作。

package javafxapplication2;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

class Person {

    private StringProperty aliasName;
    private StringProperty newPanelName;
    private StringProperty newPanelDescription;
    private ObservableList<Person> panels = FXCollections.observableArrayList();

    public final void setAliasName(String value) {
        aliasNameProperty().set(value);
    }

    public final String getAliasName() {
        return aliasNameProperty().get();
    }

    public StringProperty aliasNameProperty() {
        if (aliasName == null) {
            aliasName = new SimpleStringProperty();
        }
        return aliasName;
    }

    public final void setNewPanelName(String value) {
        newPanelNameProperty().set(value);
    }

    public final String getNewPanelName() {
        return newPanelNameProperty().get();
    }

    public StringProperty newPanelNameProperty() {
        if (newPanelName == null) {
            newPanelName = new SimpleStringProperty();
        }
        return newPanelName;
    }

    public final void setNewPanelDescription(String value) {
        newPanelDescriptionProperty().set(value);
    }

    public final String getNewPanelDescription() {
        return newPanelDescriptionProperty().get();
    }

    public StringProperty newPanelDescriptionProperty() {
        if (newPanelDescription == null) {
            newPanelDescription = new SimpleStringProperty();
        }
        return newPanelDescription;
    }

    public ObservableList<Person> panelsProperty() {
        return panels;
    }

    public Person(String alias, String newPanelName, String newPanelDescription) {
        setAliasName(alias);
        setNewPanelName(newPanelName);
        setNewPanelDescription(newPanelDescription);
    }
}

public class JavaFXApplication2 extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("test");
        Group root = new Group();
        Scene scene = new Scene(root, 500, 250, Color.WHITE);
        // create a grid pane
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);

        ObservableList<Person> leaders = FXCollections.observableArrayList();
        leaders.add(new Person("test 1", "test 11", "test 111"));    
        leaders.add(new Person("test 2", "test 22", "test 222"));    
        leaders.add(new Person("test 3", "test 33", "test 333"));    
        leaders.add(new Person("test 4", "test 44", "test 444"));  

        final ListView<Person> leaderListView = new ListView<>(leaders);
        leaderListView.setPrefWidth(450);
        leaderListView.setPrefHeight(150);

        // 
        leaderListView.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {
            @Override
            public ListCell<Person> call(ListView<Person> param) {
                final Label leadLbl = new Label();
                final Tooltip tooltip = new Tooltip();
                final ListCell<Person> cell = new ListCell<Person>() {
                    @Override
                    public void updateItem(Person item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            leadLbl.setText(item.getAliasName());
                            setText(item.getNewPanelName());// + " " + item.getNewPanelDescription());
                            tooltip.setText(item.getAliasName());
                            setTooltip(tooltip);
                        }
                    }
                }; // ListCell
                return cell;
            }
        }); // setCellFactory

        // Double click
        leaderListView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {

                if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
                    if (mouseEvent.getClickCount() == 2) {
                        Person item = leaderListView.getSelectionModel().getSelectedItem();
                        if (item != null) {
                            StackPane pane = new StackPane();
                            Scene scene = new Scene(pane);
                            Stage stage = new Stage();
                            stage.setScene(scene);

                            pane.getChildren().add(new TextField(item.getAliasName()));

                            stage.show();
                        }

                    }
                }
            }
        });

        gridpane.add(leaderListView, 0, 1);

        // Buttons

        // Button "Open"
        Button btnYes = new Button("Open");

        btnYes.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {

                leaderListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
                    @Override
                    public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {

                        Person item = leaderListView.getSelectionModel().getSelectedItem();
                                if (item != null) {
                                    StackPane pane = new StackPane();
                                    Scene scene = new Scene(pane);
                                    Stage stage = new Stage();
                                    stage.setScene(scene);

                                    pane.getChildren().add(new TextField(item.getAliasName()));

                                    stage.show();
                                }

                        System.out.println("selection changed");
                    }
                });
            }
        });

        // Button "Cancel"
        Button btnNo = new Button("Cancel");

        btnNo.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                primaryStage.close();
            }
        });

        HBox hbox = new HBox();
        hbox.setSpacing(10);
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(btnYes);
        hbox.getChildren().add(btnNo);

        gridpane.add(hbox, 0, 2);

        root.getChildren().add(gridpane);

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

当我单击按钮打开一个新窗口时,我想选择一行然后Select打开一个新窗口。你能帮我修复代码吗?

4

2 回答 2

1

替换为btnYes.setOnAction以下内容:

btnYes.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        Person item = leaderListView.getSelectionModel().getSelectedItem();
        if (item != null) {
            StackPane pane = new StackPane();
            Scene scene = new Scene(pane);
            Stage stage = new Stage();
            stage.setScene(scene);

            pane.getChildren().add(new TextField(item.getAliasName()));

            stage.show();
        }
    }
});
于 2013-10-04T20:58:26.323 回答
1

应该是btnYes的动作:

btnYes.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {


                        Person item = leaderListView.getSelectionModel().getSelectedItem();
                                if (item != null) {
                                    StackPane pane = new StackPane();
                                    Scene scene = new Scene(pane);
                                    Stage stage = new Stage();
                                    stage.setScene(scene);

                                    pane.getChildren().add(new TextField(item.getAliasName()));

                                    stage.show();
                                }



            }
        });

(可能有一个额外的刹车,刚刚在线编辑。)

此外,这与列表双击事件中的代码完全相同,可能希望将其放在不同的方法中,这样您就不会重复代码:

public void openNewWindow(){
                        Person item = leaderListView.getSelectionModel().getSelectedItem();
                                if (item != null) {
                                    StackPane pane = new StackPane();
                                    Scene scene = new Scene(pane);
                                    Stage stage = new Stage();
                                    stage.setScene(scene);

                                    pane.getChildren().add(new TextField(item.getAliasName()));

                                    stage.show();
}
于 2013-10-04T20:59:00.830 回答