2017-04-13 9 views
0

私のプロジェクトの一環として、しばらくの間無効にする必要があるtableviewを表示したいとします(nb:テーブルを編集しないでください)。 tableview.This、作業コードで編集プロセスは、私は戻ってそれを有効にしたいが、残念ながら私はthat.Anyいずれかの任意の代替コードを見つけるcould'ntボタンをクリックしたときに上になった後JavaFX tableview行の選択を有効または無効にする

table.setSelectionModel(null); 

だから私の問題があります行の選択を可能にするコードを私に提案してください。

+0

「table.setDisable(true);」と「table.setDisable(false)必要に応じて? –

+0

私はテーブルを無効にしたくない行の選択を無効にしたい – AchuRockzz

+0

そのコードtable.setSelectionModel(null);行の選択を無効にするために正しく動作していますが、私はそれをすべて有効にするための代替コードが必要です。 – AchuRockzz

答えて

0

は、デフォルトの選択モデルを取得することができます。Tは、あなたのテーブルのタイプです

TableView<T> table = new TableView<>(); 
TableViewSelectionModel<T> defaultSelectionModel = table.getSelectionModel(); 

。 (あなたはFXMLを使用している場合はもちろん、単にコントローラのinitialize()方法の2行目を入れた。)その後

を行選択を無効にする

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

    } 
} 
+0

はい、これは私が期待していたコードですが、不幸にも動作していないbrother.stillが無効モードです。 – AchuRockzz

+0

@AchuRockzzそれは私のためにうまくいきます:私は完全な例を追加しました(行選択を有効/無効にするチェックボックスを使います)。それがあなたのために働かないなら、あなたはどこか他の何かが間違っています。 –

+0

すごい申し訳ありません、最初にボタンイベントの中にコードを入れて、今度は2番目の行をinitialize()メソッドの下に置くと完全に動作します.James_D rockzz .. – AchuRockzz

0

私はこのような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をテーブルビューモデルの適切なプロパティにバインドすることができます。あなたがテーブルを作成するときに

+0

試行されましたが、エラーが発生していませんでした。 – AchuRockzz

+0

テーブルを塗りつぶすオブジェクトはどのように見えますか?つまり、BooleanProperty型に定義されているプロパティが必要です。私のコード例では、このように書かれていないプロパティがあります。 private BooleanProperty focusable;私はゲッターとセッターも持っています。だから私はこのゲッターを使用しているコード例では、 public BooleanProperty getFocusable() { return focusable; }この方法でもう一度お試しください。 – mtrgn

関連する問題