单击行时代码无法正常工作

时间:2013-10-04 16:27:51

标签: javafx-2 javafx javafx-8

你能帮我解决这个问题。它没有像我预期的那样正常工作。

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打开新窗口时打开一个新窗口。你能帮我解决一下代码吗?

2 个答案:

答案 0 :(得分: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();
        }
    }
});

答案 1 :(得分: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();
                                }



            }
        });

(可能有一个axtra braket,刚刚在线编辑。)

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

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();
}