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

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.

Answers


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:


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));
            }

        }
    });

Need Your Help

Xcode 4.2. gets stuck "Attaching to myapp" when running

iphone xcode ios-simulator ios5 hang

In xcode 4.2, sometimes when you're going to run your ios proj on simulator, the status windows says "Attaching to myapp", and just gets stuck there... until you cancel.

getDeclaredMethod doesn't work, NoSuchMethodException

java reflection

I've been trying to use Reflection in Java, but it doesn't end up pretty well. Here's my code: