如何在JavaFX中的ListView中缩放Label

时间:2017-08-04 15:22:35

标签: java listview javafx layout scroll

我有一个带有一些标签的ListView。标签的width属性绑定到ListView的width属性,但它们似乎略大,这意味着列表视图上会显示水平滚动条。我想要的是在列表视图中放置标签而不在底部滚动条。我在标签和列表视图中查看了各种填充和插入值,但我找不到的是罪魁祸首(大多数都是零)。

enter image description here

以下是一个演示此问题的示例。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class ListViewScrollExample extends Application {

   private ListView<Node> listView;

   @Override
   public void start(Stage stage) throws Exception {
       listView = new ListView<>();
       addItem("Some quite long string to demonstrate the problem");

       Scene scene = new Scene(listView);
       stage.setScene(scene);
       stage.show();
    }

    public void addItem(String item) {
        Label label = new Label(item);
        label.setWrapText(true);
        label.maxWidthProperty().bind(listView.widthProperty());
        listView.getItems().add(label);
    }

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

}

2 个答案:

答案 0 :(得分:3)

default CSS file将填充添加到$("#ae__orders__content").click(function () { //$(this).children().focus().remove(); alert("remove"); //$('li', ul).last().remove(); $(this).children().remove(); return false; }); (当前版本中的第2316行):

ListCell

使用.list-cell { -fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */ } 实例作为支持Node的数据通常是个坏主意:在此示例中应使用ListView,并使用单元工厂创建显示根据需要配置的字符串。以下似乎适用于您的示例:

String

List view at default width List view at wider width List view at narrow width

在这里,我创建了一个列表单元格,将标签显示为图形,标签文本设置为要显示的字符串。单元格的构造函数将标签的最大宽度绑定到单元格的宽度,减去填充所需的任何空间。对import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.scene.Scene; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.stage.Stage; public class ListViewScrollExample extends Application { private ListView<String> listView; @Override public void start(Stage stage) throws Exception { listView = new ListView<>(); listView.getItems().add("Some quite long string to demonstrate the problem"); listView.setCellFactory(lv -> { ListCell<String> cell = new ListCell<String>() { private Label label = new Label(); { label.setWrapText(true); label.maxWidthProperty().bind(Bindings.createDoubleBinding( () -> getWidth() - getPadding().getLeft() - getPadding().getRight() - 1, widthProperty(), paddingProperty())); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); } @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (empty) { setGraphic(null); } else { label.setText(item); setGraphic(label); } } }; return cell ; }); Scene scene = new Scene(listView); stage.setScene(scene); stage.show(); } public static void main(String[] args){ Application.launch(args); } } 的调用似乎是必要的,因此单元格不会尝试为文本分配任何空间。

可以通过直接在列表单元格上设置文本并在单元格上调用setContentDisplay(ContentDisplay.GRAPHIC_ONLY)(毕竟,这也是setWrapText(true)的子类)来实现这一点,但我无法和#39;让它以这种方式工作。

答案 1 :(得分:-1)

我无法复制问题,但您可以尝试以下操作,而不是label.maxWidthProperty().bind(listView.widthProperty());

double i = Double.parseDouble(listView.widthProperty().toString());
label.setMaxWidth((i-2.0));

您可以将2.0更改为更改屏幕所需的任何像素数。