2017-10-23 20 views
0

tableView의 첫 번째 열 또는 마지막 열에 대한 재정렬을 비활성화하려고합니다. 두 번째 방법으로 첫 번째 열 또는 마지막 열을 비활성화 할 수 있으므로 처음/마지막 열을 이동할 수는 없지만 두 가지 방법으로 다른 열을 첫 번째/마지막 위치로 끌 수 있습니다. 이 문제를 어떻게 방지 할 수 있습니까? 나는 그곳에 어떻게 든 "장벽"을두고 모든 시간 고정 된 칼럼을 갖고 싶다.JavaFx : 첫 번째/마지막 열에 대한 재정렬 해제

firstColumn.impl_setReorderable(false); // I know the method is @Deprecated but it works :) 

다른 솔루션은 다음 재정렬을 해제 TableHeaderRow의 참조를 얻기 위해, 반사와 같습니다

은 어떻게 firstColumn을 사용하지 않도록 설정합니다. 그러나이 솔루션의 문제는 첫 번째 또는 마지막 행을 원할 때 순서 변경을 위해 모든 열을 사용할 수 없도록 설정한다는 것입니다.

간단한 생각 어떻게 이런 식으로 할 수 있습니까?

답변

0

리오 더링을 수행 한 다음 변경 사항을 청취 할 수 있고 이벤트가 끝나면 원하는대로 열을 재정렬 할 수 있습니다. 열의 순서를 바꾸는 것에 대한 매우 유용한 게시물은 this입니다 (잘 살펴 봐야 할 좋은 답변이 많습니다). 그래서 당신은 당신의 테이블의 피부 특성에 리스너를 추가 할 수 있으며, 내부의 다음을 수행 : 또한 firstNameCol.impl_setReorderable(false);phoneCol.impl_setReorderable(false);가 드래그를 방지하기 위해 추가 할 수

import com.sun.javafx.scene.control.skin.TableHeaderRow; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.VBox; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 

public class TestApp extends Application { 

    private TableView<Person> table = new TableView<Person>(); 
    private final ObservableList<Person> data = FXCollections.observableArrayList(
      new Person("Jacob", "Smith", "[email protected]", "845-548-600"), 
      new Person("Isabella", "Johnson", "[email protected]", "455-777-645"), 
      new Person("Ethan", "Williams", "[email protected]", "888-504-254"), 
      new Person("Emma", "Jones", "[email protected]", "123-548-350"), 
      new Person("Michael", "Brown", "[email protected]", "650-120-600")); 

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

    @Override 
    public void start(Stage stage) { 
     Scene scene = new Scene(new Group()); 
     stage.setTitle("Table View Sample"); 
     stage.setWidth(550); 
     stage.setHeight(500); 

     final Label label = new Label("Address Book"); 
     label.setFont(new Font("Arial", 20)); 

     table.setEditable(true); 

     TableColumn firstNameCol = new TableColumn("First Name"); 
     firstNameCol.setMinWidth(100); 
     firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); 

     TableColumn lastNameCol = new TableColumn("Last Name"); 
     lastNameCol.setMinWidth(100); 
     lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName")); 

     TableColumn emailCol = new TableColumn("Email"); 
     emailCol.setMinWidth(200); 
     emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email")); 

     TableColumn phoneCol = new TableColumn("Phone"); 
     phoneCol.setMinWidth(100); 
     phoneCol.setCellValueFactory(new PropertyValueFactory<Person, String>("phone")); 

     table.setItems(data); 
     table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, phoneCol); 

     table.skinProperty().addListener((obs, oldSkin, newSkin) -> { 
      final TableHeaderRow header = (TableHeaderRow) table.lookup("TableHeaderRow"); 
      header.reorderingProperty().addListener((o, oldVal, newVal) -> { 
       ObservableList columns = table.getColumns(); 

       // If the first columns is not in the first index change it 
       if (columns.indexOf(firstNameCol) != 0) { 
        columns.remove(firstNameCol); 
        columns.add(0, firstNameCol); 
       } 
       // Use the same logic for the last column 
       if (columns.indexOf(phoneCol) != columns.size() - 1) { 
        columns.remove(phoneCol); 
        columns.add(columns.size(), phoneCol); 
       } 
      }); 
     }); 

     final VBox vbox = new VBox(); 
     vbox.setSpacing(5); 
     vbox.setPadding(new Insets(10, 0, 0, 10)); 
     vbox.getChildren().addAll(label, table); 

     ((Group) scene.getRoot()).getChildren().addAll(vbox); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    public static class Person { 

     private final SimpleStringProperty firstName; 
     private final SimpleStringProperty lastName; 
     private final SimpleStringProperty email; 
     private final SimpleStringProperty phone; 

     private Person(String fName, String lName, String email, String phone) { 
      this.firstName = new SimpleStringProperty(fName); 
      this.lastName = new SimpleStringProperty(lName); 
      this.email = new SimpleStringProperty(email); 
      this.phone = new SimpleStringProperty(phone); 
     } 

     public String getPhone() { 
      return phone.get(); 
     } 

     public String getFirstName() { 
      return firstName.get(); 
     } 

     public String getLastName() { 
      return lastName.get(); 
     } 

     public String getEmail() { 
      return email.get(); 
     } 

    } 
} 

: 여기

table.skinProperty().addListener((obs, oldSkin, newSkin) -> { 
    final TableHeaderRow header = (TableHeaderRow) table.lookup("TableHeaderRow"); 
    header.reorderingProperty().addListener((o, oldVal, newVal) -> { 
     ObservableList columns = table.getColumns(); 

     // If the first columns is not in the first index change it 
     if (columns.indexOf(firstNameCol) != 0) { 
       columns.remove(firstNameCol); 
       columns.add(0, firstNameCol); 
      } 
     // Use the same logic for the last column 
     if (columns.indexOf(phoneCol) != columns.size() - 1) { 
      columns.remove(phoneCol); 
      columns.add(columns.size() , phoneCol); 
     } 
    }); 
}); 

전체 작업 예입니다 첫 번째 열과 마지막 열에 대해서도 마찬가지입니다.

+0

네,이 방법이 효과가 있습니다 만, 내가 옮기더라도 그 곳을 바꿀 수있는 방법이 있다고 생각했습니다. 가장 좋은 해결책은 가능한 경우 첫 번째/마지막 열로 드래그하지 못하게하는 것입니다. 그렇지 않으면이 방법으로 이동하십시오. – Sunflame

+0

@Sunflame 드래그 할 수 없도록하려는 컬럼에'impl_setReorderable (false); '를 사용할 수 있지만, 시각적으로 보이지 않게하는 방법 (최소한 쉬운 방법)은 없다고 생각합니다. 열의 재 배열이 일어나지 않는 경우에도 드래그 할 수없는 다른 열을 드래그 할 수 있습니다. – JKostikiadis

+0

네가 맞을지도 모르지만, 첫 번째 부품을 옮긴다면 수표와 함께 부품을 사용했다. 가장 우아한 솔루션은 아니지만 작동합니다. 제안 솔루션을 주셔서 감사합니다. – Sunflame