private void updatePopup() {
   if (getText() == null) {
     return;
   }
   LinkedList<String> searchResult = new LinkedList<>();
   searchResult.addAll(
       entries.stream().filter(x -> x.startsWith(getText())).collect(Collectors.toList()));
   if (entries.size() > 0) {
     populatePopup(searchResult);
     if (!popup.isShowing()) {
       Point2D p = this.localToScene(0.0, 0.0);
       double x = p.getX() + this.getScene().getX() + this.getScene().getWindow().getX();
       double y =
           p.getY()
               + this.getScene().getY()
               + this.getScene().getWindow().getY()
               + this.getHeight();
       popup.show(AutoCompleteTextField.this, x, y);
     }
   } else {
     popup.hide();
   }
 }
  /**
   * A Popup containing Listview is trigged from this function This function automatically resize
   * it's height and width according to the width of textbox and item's cell height
   */
  public void showPopup(String newValue) {
    ListView<String> listView = new ListView<>();
    listView.setPrefWidth(textField.getWidth());

    listView.getSelectionModel().clearSelection();
    listView.getFocusModel().focus(-1);

    listView.setItems(searchItems(newValue));

    // Set ListView height
    if (listView.getItems().size() > getDefaultLimitCount()) {
      listView.setPrefHeight(getDefaultLimitCount() * getDefaultPrefHeight());
    } else {
      listView.setPrefHeight(listView.getItems().size() * getDefaultPrefHeight());
    }

    listView.setOnMouseReleased(
        event -> {
          textField.setText(listView.getSelectionModel().getSelectedItem());
          textField.requestFocus();
          textField.requestLayout();
          textField.end();
          popup.hide();
          onSelectAction(listView.getSelectionModel().getSelectedItem());
        });

    listView.setOnKeyReleased(
        event -> {
          switch (event.getCode()) {
            case ENTER:
              textField.setText(listView.getSelectionModel().getSelectedItem());
              textField.requestFocus();
              textField.requestLayout();
              textField.end();
              popup.hide();
              onSelectAction(listView.getSelectionModel().getSelectedItem());
              break;
            default:
              break;
          }
        });

    // This cell factory helps to know which cell has been selected so that
    // when ever any cell is selected the textbox rawText must be changed
    listView.setCellFactory(
        view -> {
          // A simple ListCell containing only Label
          final ListCell<String> cell =
              new ListCell<String>() {
                public void updateItem(String item, boolean empty) {
                  super.updateItem(item, empty);
                  setText(item);
                }
              };

          return cell;
        });
    popup.getContent().clear();
    popup.getContent().add(listView);

    // SHOWING THE POPUP JUST BELOW TEXTBOX
    popup.show(
        getWindow(),
        getWindow().getX() + textField.localToScene(0, 0).getX() + textField.getScene().getX(),
        getWindow().getY()
            + textField.localToScene(0, 0).getY()
            + textField.getScene().getY()
            + getDefaultPrefHeight());
  }
Exemplo n.º 3
0
  public GridPane build() {
    area = getNewGrid();

    for (int i = 0; i < eaTabs.size(); i++) {
      EATab eaTab = eaTabs.get(i);

      /*Label label = new Label(eaTab.getTabText() + "  Gen: 0");
      eaTab.fitnessGrapher.genHolder.addListener((val) -> {
      	label.setText(eaTab.getTabText() + "  Gen: " + (Integer)val);
      });*/

      final Label label = new Label(eaTab.getTabText() + "  Fit: --");
      eaTab.fitnessGrapher.fitHolder.addListener(
          (val) -> {
            label.setText(
                eaTab.getTabText() + "  Fit: " + (Math.round(((Double) val) * 100.0) / 100.0));
          });

      final Popup popup = new Popup();
      final Label popupMessage = new Label(label.getText());
      popupMessage.getStylesheets().add("./ui/css/style.css");
      popupMessage.getStyleClass().add("popup");

      label.setOnMouseEntered(
          event -> {
            popup.setAnchorX(event.getScreenX() + 20);
            popup.setAnchorY(event.getScreenY());
            popup.setAutoFix(true);
            popup.setHideOnEscape(true);
            popup.getContent().clear();
            popup.getContent().add(popupMessage);
            popup.show(GUI.stage);
          });

      label.setOnMouseExited(
          event -> {
            popup.hide();
          });

      Line line = new Line();
      line.setEndX(100);
      line.setStroke(Paint.valueOf("B4B4B4"));
      GridPane.setColumnSpan(line, 2);
      GridPane.setRowSpan(line, 1);
      GridPane.setHalignment(line, HPos.CENTER);
      GridPane.setValignment(line, VPos.CENTER);
      area.add(line, 0, i * 2 + 1);

      TetheredProgressBar progress = new TetheredProgressBar(eaTab.progressBar);

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

      StackPane sp2 = new StackPane();
      sp2.getChildren().add(progress);

      VBox vbox = new VBox();
      vbox.getChildren().addAll(sp, sp2);

      HBox hbox = new HBox();

      TetheredButton button = new TetheredButton(eaTab.startButton);

      MenuButton mb = new MenuButton();
      mb.setText("...");
      MenuItem save = new MenuItem("Save");
      save.setOnAction(
          (event) -> {
            System.out.println("SAVE");
            fxController.saveEvolution(eaTab);
          });

      MenuItem close = new MenuItem("Close");
      close.setOnAction(
          (event) -> {
            fxController.closeTab(eaTab);
          });
      mb.getItems().addAll(save, close, new MenuItem("Send"));

      hbox.getChildren().addAll(button, mb);
      hbox.setSpacing(5);

      area.add(vbox, 0, i * 2);
      area.add(hbox, 1, i * 2);

      // button = null;
    }

    return area;
  }
  public void hide() {

    popup.hide();
  }