【问题标题】:JavaFX - ScrollPane in Dialog alternating heightJavaFX - Dialog 中的 ScrollPane 交替高度
【发布时间】:2017-04-30 16:24:54
【问题描述】:

我对 JavaFX 有疑问。

我想在 Dialog 中放置一个 ScrollPane 并给 Scrollpane 一个最大高度,没有最小高度或预置高度,因为它可以更短。

这是一个简单的例子来演示我的问题。
主类:

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application
{
  @Override
  public void start(Stage primaryStage)
  {
    try
    {
      StackPane root = new StackPane();
      MyDialog dialog = new MyDialog();
      Button button = new Button("Test");
      button.setOnMousePressed(e -> dialog.showAndWait());
      root.getChildren()
          .add(button);
      Scene scene = new Scene(root, 400, 400);
      primaryStage.setScene(scene);
      primaryStage.show();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

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

对话框类:

package application;

import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class MyDialog extends Dialog<ButtonType>
{
  public MyDialog()
  {
    super();
    DialogPane dialogPane = new DialogPane();
    VBox outer = new VBox();
    Button button = new Button("Test");
    setResult(new ButtonType("NO", ButtonData.LEFT));
    button.setOnMousePressed(e -> close());
    VBox inner = new VBox();
    inner.getChildren()
        .add(new Text("Test 1"));
    inner.getChildren()
        .add(new Text("Test 2"));
    inner.getChildren()
        .add(new Text("Test 3"));
    inner.getChildren()
        .add(new Text("Test 4"));
    inner.getChildren()
        .add(new Text("Test 5"));
    ScrollPane scroll = new ScrollPane(inner);
    scroll.setMaxHeight(80);
    outer.getChildren()
        .addAll(button, scroll);
    dialogPane.setContent(outer);
    this.setDialogPane(dialogPane);
  }
}

具体问题如下。
对话框应如下所示:

但是每次我打开它,它看起来像这样:

我不知道是什么问题。希望有人能告诉我为什么这不起作用。

【问题讨论】:

  • 你好,我找到了一个不太好的解决这个问题的方法。我覆盖了 ScrollPane 的 computeMinHeight(double width) 方法并返回内容的高度(或 ScrollPane 的最大高度)。但我想也许有一个更漂亮的解决方案。

标签: java javafx javafx-8


【解决方案1】:

我无法使用提供的代码重现该行为。对我来说,它总是这样:

但是,您不想使用 javafx ListView 的具体原因,它似乎正在做您正在尝试做的事情?

    public MyDialog() {
    super();
    DialogPane dialogPane = new DialogPane();
    VBox outer = new VBox();
    Button button = new Button("Test");
    setResult(new ButtonType("NO", ButtonData.LEFT));
    button.setOnMousePressed(e -> close());
    ListView<Text> inner = new ListView<Text>();
    inner.getItems().add(new Text("Test 1"));
    inner.getItems().add(new Text("Test 2"));
    inner.getItems().add(new Text("Test 3"));
    inner.getItems().add(new Text("Test 4"));
    inner.getItems().add(new Text("Test 5"));
    inner.getItems().add(new Text("Test 6"));
    inner.getItems().add(new Text("Test 7"));
    inner.setMaxHeight(80);
    outer.getChildren().addAll(button, inner);
    dialogPane.setContent(outer);
    this.setDialogPane(dialogPane);
}

【讨论】:

  • 您好,是的,您是对的,我也可以使用 ListView,但您的代码也有同样的问题。我正在使用 JDK 1.8.0_112 在 Ubuntu 12.04 LTS 下的 Eclipse neon 中进行开发。也许其中一件事情会导致问题。
  • 它不是我可以告诉你的 JDK,因为我使用的是相同的。至于 Eclipse,我在 Luna 上,但我怀疑这是否有用。它可能与 Ubuntu 有关,也许它以不同的方式处理它的窗口。您可能想修改您的问题并将其发布到不同的类别中,我不认为这是代码问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2020-10-29
  • 1970-01-01
相关资源
最近更新 更多