Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093090

Regression: Wide BorderPane within ListCell with prefWidth causes ListCell to grow beyond boundaries

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 8u20
    • 8
    • javafx

      Hi,

      In JavaFX 2.2 I used setPrefWidth() on a ListCell in order to avoid horizontal scrollbars. The ListCell then always took the width it needed and if it would grow beyond its boundaries, Labels were displayed with "..." This worked fine!

      Now, in JavaFX 8 it doesn't work anymore. I figured out it is because of a BorderPane as part of the graphic.

      In JavaFX 2.2 the following code produces "..." for the long item and displays the rest right of it. Therefore the button was always visible.

      In JavaFX 8 it produces no scrollbars either, but it also just cuts the long text without "..." and moves the button even more to the right, making it essentially hidden and unreachable for the user.


      import javafx.application.Application;
      import javafx.collections.FXCollections;
      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.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Priority;
      import javafx.stage.Stage;
      import javafx.util.Callback;


      public class TestApp5 extends Application {
          public static void main(String[] args) {
              launch();
          }

          @Override
          public void start(final Stage stage) throws Exception {

              ListView<String> listView = new ListView<String>();
              listView.setItems(FXCollections.observableArrayList("Short Item", "This is a very very very very very very very long item with a lot of text"));
              listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                  @Override
                  public ListCell<String> call(ListView<String> stringListView) {
                      ListCell<String> cell = new ListCell<String>() {
                          @Override
                          public void updateItem(String item, boolean empty) {
                              super.updateItem(item, empty);
                              setGraphic(null);
                              if (item != null) {
                                  HBox hBox = new HBox();
                                  BorderPane borderPane = new BorderPane();
                                  borderPane.setLeft(new Label("Label1"));
                                  borderPane.setRight(new Label(item));
                                  hBox.getChildren().add(borderPane);
                                  hBox.getChildren().add(new Button("+"));
                                  borderPane.setMaxWidth(Double.MAX_VALUE);
                                  HBox.setHgrow(borderPane, Priority.ALWAYS);
                                  setGraphic(hBox);
                              }
                          }
                      };
                      cell.setPrefWidth(1); // Avoids horizontal scrollbars.
                      return cell;
                  }
              });

              Scene scene = new Scene(listView);
              stage.setScene(scene);
              scene.getStylesheets().add("/styles.css");
              stage.show();
          }
      }

            msladecek Martin Sládeček
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: