0
votes

I have this code which displays list with panels. I want based on a selected action to open appropriate new panel:

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

I want when I double click on a line to open the appropriate new panel for example test 11.

And also I want when I select the with one click and click Open button to open new window.

Can help to modify the code?

1
which panel do you want to show? Person is not a panel... - Magcus
The new panel code which I want to show when I double click or select and click "Select" button is not shown. The idea is that I want to open a new window based on the selection choice. - Peter Penzov
This new window constructor receives as a parameter the person then? - Magcus
Are you familiar with Oracle SQL Developer? Open the software. Click NEW from the menu above and you will see window with title "Create a new". And based on the selection you are forwarded to a new window. I want to implement the same navigation case. - Peter Penzov

1 Answers

2
votes

If I understood your coments correctly you wanto to do something like this instead of printing "selection changed":

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

                    }
                });

Of course that instead of creating the pane there, you will want to use your own panel wich displays what you want exactly.


EDIT (requested in comments):

If you want to detect the double click, you will have to work with EventHandlers:

here is an aproach (remove the code proposed above and add this):

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

                }
            }
        }
    });