在悬停到行中时更改单元格的内容?

时间:2015-08-11 11:52:39

标签: javafx javafx-2

我希望在将鼠标悬停在Table的行上时禁用/启用单元格的组件,如下所示:

enter image description here

我所做的是,

    userTable.setRowFactory(new Callback<TableView<UserOpenFire>, TableRow<UserOpenFire>>() 
            {

                @Override
                public TableRow<UserOpenFire> call(TableView<UserOpenFire> arg0) 
                {
                    final TableRow<UserOpenFire> row = new TableRow<UserOpenFire>();
                    row.hoverProperty().addListener(new ChangeListener<Boolean>() 
                            {
                                @Override
                                public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
                                {
                                    if(row.isHover())
                                    {
                                        System.out.println("hello i am hover");
                                        userTable.getSelectionModel().select(row.getIndex());

                                         final UserOpenFire selectedRow = userTable.getItems().get(row.getIndex());
                                         final TableColumn<UserOpenFire,?> selectedColumn = userTable.getColumns().get(0);

                                         selectedColumn.getCellValueFactory();

                               // here I am unable to get AddSnNumber class which 
                               // hold that component And according to requirements 
                               // that component should enable or disable from here                                      

                                    }
                                }

                            });
                    return row;

                }
            });

任何帮助都会非常有帮助 谢谢!

1 个答案:

答案 0 :(得分:3)

您可以创建一个属性,该属性表示属于鼠标悬停在其上的行的单元格中的值。让您的表格行更新属性hoverProperty更改。

然后让相应列中的每个单元格观察属性,并在元素等于属性值时启用/禁用元素。

SSCCE:

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class EnableOnHoverTableColumn extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table = new TableView<>();
        TableColumn<Person, Number> idCol = column("ID", Person::idProperty);

        // ID of currently hovered row (or -1 if no row hovered):
        IntegerProperty hoveredID = new SimpleIntegerProperty(-1);

        // update hoveredID when hover on row:
        table.setRowFactory(tv -> {
            TableRow<Person> row = new TableRow<>();
            row.hoverProperty().addListener((obs, wasHovered, isNowHovered) -> {
                if (isNowHovered && ! row.isEmpty()) {
                    hoveredID.set(row.getItem().getId());
                } else {
                    hoveredID.set(-1);
                }
            });
            return row ;
        });

        idCol.setCellFactory(col -> {
            Button editButton = new Button("Edit");
            Button deleteButton = new Button("Delete");
            HBox buttons = new HBox(5, editButton, deleteButton);

            TableCell<Person, Number> cell = new TableCell<Person, Number>() {
                @Override
                public void updateItem(Number id, boolean empty) {
                    super.updateItem(id, empty);
                    if (empty) {
                        setText(null);
                        setGraphic(null);
                    } else {
                        setText(id.toString());
                        setGraphic(buttons);
                    }
                }
            };

            BooleanBinding idIsHovered = Bindings.createBooleanBinding(() -> 
                hoveredID.getValue().equals(cell.getItem()), 
                cell.itemProperty(),
                hoveredID);

            buttons.disableProperty().bind(idIsHovered.not());

            cell.setContentDisplay(ContentDisplay.RIGHT);

            return cell ;
        });

        table.getColumns().add(idCol);
        table.getColumns().add(column("First Name", Person::firstNameProperty));
        table.getColumns().add(column("Last Name", Person::lastNameProperty));

        table.getItems().addAll(
                new Person(1, "Jacob", "Smith"),
                new Person(2, "Isabella", "Johnson"),
                new Person(3, "Ethan", "Williams"),
                new Person(4, "Emma", "Jones"),
                new Person(5, "Michael", "Brown")
        );

        primaryStage.setScene(new Scene(new BorderPane(table), 600, 600));
        primaryStage.show();
    }

    private static <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    private static class Person {
        private final IntegerProperty id = new SimpleIntegerProperty();
        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();

        public Person(int id, String firstName, String lastName) {
            setId(id);
            setFirstName(firstName);
            setLastName(lastName);
        }
        public final IntegerProperty idProperty() {
            return this.id;
        }
        public final int getId() {
            return this.idProperty().get();
        }
        public final void setId(final int id) {
            this.idProperty().set(id);
        }
        public final StringProperty firstNameProperty() {
            return this.firstName;
        }
        public final java.lang.String getFirstName() {
            return this.firstNameProperty().get();
        }
        public final void setFirstName(final java.lang.String firstName) {
            this.firstNameProperty().set(firstName);
        }
        public final StringProperty lastNameProperty() {
            return this.lastName;
        }
        public final java.lang.String getLastName() {
            return this.lastNameProperty().get();
        }
        public final void setLastName(final java.lang.String lastName) {
            this.lastNameProperty().set(lastName);
        }


    }

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