javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped, how to show a tip on such cell?

本秂侑毒 提交于 2019-12-31 07:24:10

问题


javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped end of '...', how to show a tip on such cell? I need a tip because sometimes i can't drag the colunm's head. So i can't see the whole content of the cell.


回答1:


Below a subclass of TableCell is used to set the ToolTip for a cell. It does not distinguish between cells in which the text fits and those where the text is too large. It is used in the cell factory for the table column.

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class SO extends Application {
    static class XCell extends TableCell<String, String> {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            this.setText(item);
            this.setTooltip(
                    (empty || item==null) ? null : new Tooltip(item));
        }
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane pane = new StackPane();
        Scene scene = new Scene(pane, 300, 100);
        primaryStage.setScene(scene);
        ObservableList<String> list = FXCollections.observableArrayList(
                "Short",
                "This is quite long - almost very long");
        TableView<String> tv = new TableView<>(list);
        TableColumn<String, String> col = new TableColumn<>("Column");
        col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String,String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<String, String> param) {
                return new ReadOnlyStringWrapper(param.getValue());
            }
        });
        col.setMaxWidth(50);
        col.setCellFactory(new Callback<TableColumn<String,String>, TableCell<String,String>>() {
            @Override
            public TableCell<String, String> call(TableColumn<String, String> param) {
                return new XCell();
            }
        });
        tv.getColumns().add(col);
        pane.getChildren().add(tv);
        primaryStage.show();
    }

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

The result will look like this:




回答2:


This should ACTUALLY solve your problem.

column.setCellFactory(col -> new TableCell<Object, String>()
    {
        @Override
        protected void updateItem(final String item, final boolean empty)
        {
            super.updateItem(item, empty);
            setText(item);
            TableColumn tableCol = (TableColumn) col;

            if (item != null && tableCol.getWidth() < new Text(item + "  ").getLayoutBounds().getWidth())
            {
                tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise(new Tooltip(item)));
            } else
            {
                tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise((Tooltip) null));
            }

        }
    });


来源:https://stackoverflow.com/questions/15683907/javafx-tableviews-cellvalue-is-not-enough-to-display-in-columns-it-will-be-cl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!