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.Tooltip;  
import javafx.scene.layout.GridPane;  
import javafx.scene.layout.HBox;  
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  
        gridpane.add(leaderListView, 0, 1);  
        leaderListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {  
            @Override  
            public void changed(ObservableValue<? extends Person> observable,  
                    Person oldValue, Person newValue) {  
                System.out.println("selection changed");  
            }  
        });  

        // Buttons  

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

        btnYes.setOnAction(new EventHandler<ActionEvent>()  
        {  
            @Override  
            public void handle(ActionEvent event)  
            {  
                //Open Here the selected panel  
                primaryStage.close();  
            }  
        });  

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

我想当我双击一行以打开适当的新面板时,例如测试 11。

当我一键选择并单击“打开”按钮打开新窗口时,我也想要。

可以帮忙修改代码吗?

4

1 回答 1

2

如果我正确理解了您的评论,您想要做这样的事情而不是打印“选择更改”:

leaderListView.getSelectionModel().selectedItemProperty()
                .addListener(new ChangeListener<Person>() {
                    @Override
                    public void changed(
                            ObservableValue<? extends Person> observable,
                            Person oldValue, Person newValue) {
                        StackPane pane = new StackPane();
                        Scene scene = new Scene(pane);
                        Stage stage = new Stage();
                        stage.setScene(scene);

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

                        stage.show();

                    }
                });

当然,与其在此处创建窗格,不如使用您自己的面板来准确显示您想要的内容。


编辑(在评论中要求):

如果要检测双击,则必须使用 EventHandlers:

这是一个方法(删除上面提出的代码并添加它):

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

                }
            }
        }
    });
于 2013-10-04T14:32:10.867 回答