2 Replies Latest reply: May 18, 2012 4:19 PM by Jeff Martin RSS

    ContextMenu setMinWidth

    Jeff Martin
      I'm trying to implement a suggestions drop-down for a textfield, so I create and show a ContextMenu on TextProperty changed(). I'd like the drop down to be at least as wide as the TextField, but ContextMenu seems to be ignoring setMinWidth() and setMinSize().
      ContextMenu menu = new ContextMenu();
      for(String string : new String[] {"Red", "Green", "Blue" }) menu.getItems().add(new MenuItem(string));
      menu.setMinWidth(400);
      menu.show(node, Side.BOTTOM, 0, 0);
        • 1. Re: ContextMenu setMinWidth
          jsmith
          There is probably a better way of doing this . . . but until you work that out . . .
          ContextMenu menu = new ContextMenu();
          for(String string : new String[] {"Red", "Green", "Blue" }) {
            menu.getItems().add(
               new MenuItem("", 
                 new Group(
                   new Rectangle(400, 1, Color.TRANSPARENT), 
                   new Label(string)
                 )
              )
            );
          }  
          Instead of the hardcoded 400 value, you could bind the transparent rectangle's width to the width of your textfield to get the behaviour you want.
          The above is not a great solution, so you may want to raise a jira if you can't come up with something better.
          • 2. Re: ContextMenu setMinWidth
            Jeff Martin
            I suppose I do need to file a bug. Here's the best I could come up with:
            ContextMenu menu = new ContextMenu();
            for(String string : new String[] {"Red", "Green", "Blue" }) menu.getItems().add(new MenuItem(string));
            menu.show(textField, Side.BOTTOM, 0, 0);
            Group popupRoot = (Group)menu.getScene().getRoot();
            Group popupCSSBridge = (Group)popupRoot.getChildren().get(0);
            StackPane popupContent = (StackPane)popupCSSBridge.getChildren().get(0);
            popupContent.setMinWidth(textField.getWidth());