2017-04-13 1 views
0

내 프로젝트의 일부로 일부 시간 (편집 시간이 nb : 테이블 편집하지 않음) 동안 비활성화해야하는 tableview를 표시하고 싶습니다. tableview.This는 작업 코드 JavaFX tableview 행 선택 사용 및 사용 안 함

table.setSelectionModel(null); 

그래서 편집 과정 내가 다시 사용하려면 버튼을 클릭하면 이상하지만, 불행히도 내가 저런거 하나에 대한 대체 코드를 찾을 수 could'nt 후 내 문제입니다 나에게 행 선택을 가능하게하는 코드를 제안 해주십시오. 모든 대답은 상당 할 것입니다.

+0

왜 단지'(참) table.setDisable하지,'및''table.setDisable (false)를, 필요에 따라? –

+0

내가 원하지 않는 테이블을 비활성화하려면 행 선택을 비활성화하십시오 – AchuRockzz

+0

그리고 그 코드 table.setSelectionModel (null); 행 선택을 비활성화하기 위해 올바르게 작동하지만, 다시 사용할 수 있도록 대체 코드가 필요합니다. – AchuRockzz

답변

0

당신은 기본 선택 모델을 검색 할 수 있습니다 T이 테이블의 유형입니다

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

. (당신이 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); 
     } 

    } 
} 
+0

그래,이 코드는 내가 예상했지만 불행히도 작동하지 않는 brother.still 비활성 모드에서. – AchuRockzz

+0

@AchuRockzz 그것은 나를 위해 잘 작동합니다 : 나는 전체 예제 (확인란을 사용하여 행 선택을 사용/사용 안 함)를 추가했습니다. 그것이 당신을 위해 작동하지 않으면, 당신은 어딘가에 다른 뭔가가 있습니다. –

+0

와우 죄송합니다, 먼저 button 이벤트 안에 코드를 넣습니다. 이제 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를 tableview 모델의 적절한 속성으로 바인딩 할 수 있습니다. 테이블을 만들 때

+0

시도했지만 오류가 표시되지 않음() – AchuRockzz

+0

표를 채우는 객체는 어떻게 표시됩니까? 즉 BooleanProperty 유형으로 정의 된 속성이 필요합니다. 내 예제 코드에는 이처럼 작성되지 않은 속성이 있습니다. private BooleanProperty 포커스 가능; 또한 나는 getters와 setters 있습니다. 그래서 예제 코드에서는이 getter를 사용했습니다. public BooleanProperty getFocusable() { return focusable; }이 방법으로 다시 시도하십시오. – mtrgn