来自ControlFX的JavaFX PopOver

时间:2014-02-09 05:41:57

标签: javafx popover controlsfx

有人可以从ControlFX编写一个简短的JavaFX Popover示例吗?我无法让它发挥作用。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

你必须谷歌出来

现在有很多网站可供使用

https://bitbucket.org/controlsfx/controlsfx/commits/dca9619e05de26d176aaafe785c3b94f022562ef

https://bitbucket.org/controlsfx/controlsfx/pull-request/158/initial-commit-of-popover-control/activity

等只是谷歌出来。

这里有程序称为“HelloPopOver”。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

import org.controlsfx.ControlsFXSample;
import org.controlsfx.control.popover.PopOver;
import org.controlsfx.control.popover.PopOverController;
import org.controlsfx.control.popover.PopOverHeader;
import org.controlsfx.control.popover.PopOverTitledPane;
import org.controlsfx.samples.Utils;

public class HelloPopOver extends ControlsFXSample {
    private PopOverController<PopOver, Button> controller = new MyController();

    @Override
    public Node getPanel(Stage stage) {
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setAlignment(Pos.CENTER);
        grid.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent evt) {
                controller.hidePopOver();
            }
        });

        for (int i = 0; i < 10; i++) {
            final Button button = new Button("Button " + i);
            grid.add(button, i % 2, i / 2);

            button.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent evt) {
                    controller.hidePopOver();

                    if (evt.getClickCount() == 2) {
                        controller.setupPopOver(button);
                        controller.showPopOver(button, evt.getScreenX(),
                                evt.getScreenY());
                    }
                }
            });
        }

        return grid;
    }

    class MyController extends PopOverController<PopOver, Button> {
        @Override
        protected PopOver createPopOver(final Button button) {
            PopOver editor = new PopOver();

            PopOverHeader<?> header = (PopOverHeader<?>) editor.getHeader();
            header.setTitle(button.getText() + " (edit me)");
            header.setSubtitle("Just some random controls (edit me)");

            editor.setDetachedTitle(button.getText());

            editor.getPanes().add(createTitledPane("Start Time & Duration"));
            editor.getPanes().add(createTitledPane("Dependencies"));
            editor.getPanes().add(createTitledPane("Priority"));
            editor.getPanes().add(createTitledPane("Assignments / Resources"));
            editor.setExpandedPane(editor.getPanes().get(0));
            editor.setFooter(new Footer());

            ColorPicker picker = (ColorPicker) header.getExtra();
            picker.valueProperty().addListener(new ChangeListener<Color>() {
                @Override
                public void changed(ObservableValue<? extends Color> value,
                        Color oldColor, Color newColor) {
                    button.setBackground(new Background(new BackgroundFill(
                            newColor, CornerRadii.EMPTY, Insets.EMPTY)));
                }
            });

            return editor;
        }
    }

    private TitledPane createTitledPane(String title) {
        VBox box = new VBox(5);
        box.getChildren().add(new Button("Test"));
        box.getChildren().add(new Slider());

        ListView<String> view = new ListView<>();
        view.setPrefHeight(100);
        box.getChildren().add(view);
        final TitledPane pane = new PopOverTitledPane(title, box);
        pane.setTextAlignment(TextAlignment.LEFT);

        Pane connectivityArrow = (Pane) pane.lookup(".arrow");
        if (connectivityArrow != null) {
            connectivityArrow.translateXProperty().bind(
                    pane.widthProperty().subtract(
                            connectivityArrow.widthProperty().multiply(2)));
        }

        return pane;
    }

    class Footer extends FlowPane {
        public Footer() {
            super(Orientation.HORIZONTAL);

            setAlignment(Pos.CENTER_RIGHT);

            Button delete = new Button("Delete");
            getChildren().add(delete);
            delete.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent evt) {
                }
            });
        }
    }

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

    @Override
    public String getSampleName() {
        return "PopOver";
    }

    @Override
    public String getJavaDocURL() {
        return Utils.JAVADOC_BASE
                + "org/controlsfx/control/popover/PopOver.html";
    }

    @Override
    public String getSampleDescription() {
        return "An implementation of a pop over control as used by Apple for its iCal application. A pop over allows"
                + "the user to see and edit an objects properties. The pop over gets displayed in its own popup window and"
                + "can be torn off in order to create several instances of it.";
    }
}

答案 1 :(得分:2)

这个答案很简单地使用了ControlsFX's PopOver

  

当鼠标移过Label时,会出现PopOver。当鼠标退出Label时,PopOver消失。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.PopOver;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication35 extends Application {

    @Override
    public void start(Stage primaryStage) {


        //Build PopOver look and feel
        Label lblName = new Label("John Doe");
        Label lblStreet = new Label("123 Hello Street");
        Label lblCityStateZip = new Label("MadeUpCity, XX 55555");   
        VBox vBox = new VBox(lblName, lblStreet, lblCityStateZip);
        //Create PopOver and add look and feel
        PopOver popOver = new PopOver(vBox);


        Label label = new Label("Mouse mouse over me");
        label.setOnMouseEntered(mouseEvent -> {
            //Show PopOver when mouse enters label
            popOver.show(label);
        });

        label.setOnMouseExited(mouseEvent -> {
            //Hide PopOver when mouse exits label
            popOver.hide();
        });


        StackPane root = new StackPane();
        root.getChildren().add(label);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}