作爲我的項目的一部分,我希望顯示一段時間內應該禁用的tableview(編輯時間nb:不編輯表格)。因此,我得到了一個工作代碼,它將禁用tableview.This是工作代碼,JavaFX tableview啓用和禁用行選擇
table.setSelectionModel(null);
所以我的問題是,經過編輯過程結束時,點擊一個按鈕,我想能夠回來,但不幸的是我could'nt找到that.Any一個任何其他代碼請給我推薦行選擇的代碼。任何答案都可以。
作爲我的項目的一部分,我希望顯示一段時間內應該禁用的tableview(編輯時間nb:不編輯表格)。因此,我得到了一個工作代碼,它將禁用tableview.This是工作代碼,JavaFX tableview啓用和禁用行選擇
table.setSelectionModel(null);
所以我的問題是,經過編輯過程結束時,點擊一個按鈕,我想能夠回來,但不幸的是我could'nt找到that.Any一個任何其他代碼請給我推薦行選擇的代碼。任何答案都可以。
當您創建表,可以檢索默認的選擇模型:
TableView<T> table = new TableView<>();
TableViewSelectionModel<T> defaultSelectionModel = table.getSelectionModel();
其中T
是你的表型。 (當然,如果你使用FXML,只是把控制器的initialize()
方法的第二行)。
然後禁用行選擇做
table.setSelectionModel(null);
,並再次啓用它
table.setSelectionModel(defaultSelectionModel);
這裏是一個SSCCE:
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableWithDisabledSelection extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Person> table = new TableView<>();
TableViewSelectionModel<Person> defaultSelectionModel = table.getSelectionModel();
table.getColumns().add(column("First Name", Person::firstNameProperty));
table.getColumns().add(column("Last Name", Person::lastNameProperty));
table.getColumns().add(column("Email", Person::emailProperty));
table.getItems().addAll(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
);
CheckBox enableSelection = new CheckBox("Enable selection");
enableSelection.setSelected(true);
enableSelection.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
table.setSelectionModel(defaultSelectionModel);
} else {
table.setSelectionModel(null);
}
});
BorderPane root = new BorderPane(table);
BorderPane.setAlignment(enableSelection, Pos.CENTER);
BorderPane.setMargin(enableSelection, new Insets(5));
root.setBottom(enableSelection);
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
private <S,T> TableColumn<S,T> column(String title, Function<S,Property<T>> prop) {
TableColumn<S,T> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
return col ;
}
public static void main(String[] args) {
launch(args);
}
public static class Person {
private final StringProperty firstName = new SimpleStringProperty();
private final StringProperty lastName = new SimpleStringProperty();
private final StringProperty email = new SimpleStringProperty();
public Person(String firstName, String lastName, String email) {
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final String lastName) {
this.lastNameProperty().set(lastName);
}
public final StringProperty emailProperty() {
return this.email;
}
public final String getEmail() {
return this.emailProperty().get();
}
public final void setEmail(final String email) {
this.emailProperty().set(email);
}
}
}
是的,這是我期望的代碼,但不幸的是它不工作brother.still在禁用模式下 – AchuRockzz
@AchuRockzz它對我來說工作得很好:我添加了一個完整的示例(使用複選框來啓用/禁用行選擇)。如果它不適合你,你在別的地方還有其他問題。 –
哇,對不起,首先我把代碼放在按鈕事件中,現在當我把第二行放在initialize()method.James_D下時,它完美的工作。 – AchuRockzz
我用rowfactory處理了這種情況;
tableView.setRowFactory(param -> new TableRow<Model>()
{
@Override
protected void updateItem(Model item, boolean empty)
{
super.updateItem(item, empty);
if (!empty)
{
disableProperty().bind(item.getFocusable().not());
}
}
});
因此,您可以將disableproperty與您的tableview模型的合適屬性綁定。
嘗試過,但得到錯誤下不() – AchuRockzz
你填表的對象是什麼樣子?我的意思是,你需要一個被定義爲BooleanProperty類型的屬性。在我的示例代碼中,我有一個不是這樣寫的屬性;私人的BooleanProperty可調焦;我也有getters和setter。所以在我使用這個getter的示例代碼中;公共BooleanProperty getFocusable() { return focusable; }請使用這種方法再試一次。 – mtrgn
爲什麼不只是'table.setDisable(true);'和'table.setDisable(false)',如所須? –
我不想禁用表我想要禁用行選擇 – AchuRockzz
而且代碼table.setSelectionModel(null);正在爲禁用行選擇正確工作,但我需要替代代碼來啓用它,這就是所有。 – AchuRockzz