2016-04-20 9 views

答えて

0

このpostに基づき、ここでJavaFXのテーブルビュー内の同じ列に行によって異なるデータ・クラス・タイプを取得する方法を示す簡単な例である:

enter image description here

package application; 

import java.time.LocalDateTime; 
import javafx.application.Application; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ListChangeListener; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.geometry.Insets; 
import javafx.stage.Stage; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TablePosition; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.VBox; 

public class Main extends Application { 

    private final TableView<Person<?>> tableView = new TableView<>(); 

    private Person<Integer> person1 = new Person<>("Jacob", "Smith", 28, 4); 
    private Person<Integer> person2 = new Person<>("Isabella", "Johnson", 19, 5); 
    private Person<String> person3 = new Person<>("Bob", "The Sponge", 13, "Say Hi!"); 
    private Person<LocalDateTime> person4 = new Person<>("Time", "Is Money", 45, LocalDateTime.now()); 
    private Person<Double> person5 = new Person<>("John", "Doe", 32, 457.89); 

    private final ObservableList<Person<?>> data = FXCollections.observableArrayList(person1, person2, person3, person4, 
      person5); 

    @SuppressWarnings("unchecked") 
    @Override 
    public void start(Stage primaryStage) { 

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

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

     TableColumn<Person<?>, Integer> ageCol = new TableColumn<>("Age"); 
     ageCol.setMinWidth(50); 
     ageCol.setCellValueFactory(new PropertyValueFactory<>("age")); 

     TableColumn<Person<?>, ?> particularValueCol = new TableColumn<>("Particular Value"); 
     particularValueCol.setMinWidth(200); 
     particularValueCol.setCellValueFactory(new PropertyValueFactory<>("particularValue")); 

     tableView.setItems(data); 

     // Type safety: A generic array of Table... is created for a varargs 
     // parameter 
     // -> @SuppressWarnings("unchecked") to start method! 
     tableView.getColumns().addAll(firstNameCol, lastNameCol, ageCol, particularValueCol); 

     // Output in console the selected table view's cell value/class to check 
     // that the data type is correct. 
     SystemOutTableViewSelectedCell.set(tableView); 

     // To check that table view is correctly refreshed on data changed.. 
     final Button agePlusOneButton = new Button("Age +1"); 
     agePlusOneButton.setOnAction((ActionEvent e) -> { 
      Person<?> person = tableView.getSelectionModel().getSelectedItem(); 
      try { 
       person.setAge(person.getAge() + 1); 
      } catch (NullPointerException npe) { 
       // 
      } 
     }); 

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

     Scene scene = new Scene(new Group()); 
     ((Group) scene.getRoot()).getChildren().addAll(vbox); 

     primaryStage.setWidth(600); 
     primaryStage.setHeight(750); 

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

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

    public static class Person<T> { 

     private final StringProperty firstName; 
     private final StringProperty lastName; 
     private final IntegerProperty age; 
     private final ObjectProperty<T> particularValue; 

     private Person(String firstName, String lastName, Integer age, T particularValue) { 
      this.firstName = new SimpleStringProperty(firstName); 
      this.lastName = new SimpleStringProperty(lastName); 
      this.age = new SimpleIntegerProperty(age); 
      this.particularValue = new SimpleObjectProperty<T>(particularValue); 
     } 

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

     public void setFirstName(String firstName) { 
      this.firstName.set(firstName); 
     } 

     public StringProperty firstNameProperty() { 
      return firstName; 
     } 

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

     public void setLastName(String lastName) { 
      this.lastName.set(lastName); 
     } 

     public StringProperty lastNameProperty() { 
      return lastName; 
     } 

     public Integer getAge() { 
      return age.get(); 
     } 

     public void setAge(Integer age) { 
      this.age.set(age); 
     } 

     public IntegerProperty ageProperty() { 
      return age; 
     } 

     public T getParticularValue() { 
      return particularValue.get(); 
     } 

     public void setParticularValue(T particularValue) { 
      this.particularValue.set(particularValue); 
     } 

     public ObjectProperty<T> particularValueProperty() { 
      return particularValue; 
     } 
    } 

    public static final class SystemOutTableViewSelectedCell { 
     @SuppressWarnings({ "rawtypes", "unchecked" }) 
     public static void set(TableView tableView) { 

      tableView.getSelectionModel().setCellSelectionEnabled(true); 

      ObservableList selectedCells = tableView.getSelectionModel().getSelectedCells(); 

      selectedCells.addListener(new ListChangeListener() { 
       @Override 
       public void onChanged(Change c) { 
        TablePosition tablePosition = (TablePosition) selectedCells.get(0); 
        Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow()); 
        System.out.println("Selected Cell (Row: " + tablePosition.getRow() + "/Col: " 
          + tablePosition.getColumn() + ") Value: " + val + "/" + val.getClass()); 
       } 
      }); 
     } 
    } 

} 
関連する問題