JavaFX

Table, Tabellen mit Checkboxen (CheckBoxTableCell)

javafx.scene.control.cell.CheckBoxTableCell

Die Verwendung von Tabellen mit Checkboxen in JavaFX zeigt folgendes Bespiel:



package com.sowas.javawiki.javafx;
 
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
 
public class MyFxApplication extends Application {
 
   @Override
   public void start(Stage mainStage) {
      mainStage.setTitle("JavaFX-Table");
 
      ObservableList<RowData> data =
              FXCollections.observableArrayList(new RowData("col 1/row 1", "col 2/row1", "col 3/row1", true),
                                                new RowData("col 1/row 2", "col 2/row2", "col 3/row2", false),
                                                new RowData("col 1/row 3", "col 2/row3", "col 3/row3", true));
      TableView<RowData> table = new TableView<RowData>();
      table.setEditable(true);  // Tabelle wird grundsätzlich editierbar gemacht
 
      TableColumn col1 = new TableColumn("Col 1");
      col1.setCellValueFactory(new PropertyValueFactory<RowData, String>("colValue1"));
 
      TableColumn col2 = new TableColumn("Col 2");
      col2.setCellValueFactory(new PropertyValueFactory<RowData, String>("colValue2"));
 
      TableColumn col3 = new TableColumn("Col 3");
      col3.setCellValueFactory(new PropertyValueFactory<RowData, String>("colValue3"));
 
      TableColumn<RowData, Boolean> col4 = new TableColumn<>("Checked");
      col4.setCellValueFactory(new PropertyValueFactory<>("checked"));
      col4.setCellFactory(CheckBoxTableCell.forTableColumn(col4));
      col4.setEditable(true);  // Checkboxen werden editierbar gemacht
 
      table.getColumns().addAll(col1, col2, col3, col4);
      table.setItems(data);
 
      Scene scene = new Scene(table);
      mainStage.setScene(scene);
      mainStage.show();
   }
 
   public static void main(String[] args) {
      launch(args);
   }
}


Wichtig! Da JavaFX mit Reflection arbeitet, ist der richtige Schreibweise sämtlicher Methoden mit „checked“ im Namen wichtig!

package com.sowas.javawiki.javafx;
 
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
 
public class RowData {
 
   private SimpleStringProperty colValue1;
   private SimpleStringProperty colValue2;
   private SimpleStringProperty colValue3;
   private SimpleBooleanProperty checked;
 
   public RowData(String colValue1, String colValue2, String colValue3, boolean checked) {
      this.colValue1 = new SimpleStringProperty(colValue1);
      this.colValue2 = new SimpleStringProperty(colValue2);
      this.colValue3 = new SimpleStringProperty(colValue3);
      this.checked = new SimpleBooleanProperty(checked);
   }
 
   @SuppressWarnings("unused")
   public String getColValue1() {
      return colValue1.get();
   }
 
   @SuppressWarnings("unused")
   public String getColValue2() {
      return colValue2.get();
   }
 
   @SuppressWarnings("unused")
   public String getColValue3() {
      return colValue3.get();
   }
 
   @SuppressWarnings("unused")
   public boolean getChecked() {
      return checked.get();
   }
 
   @SuppressWarnings("unused")
   public SimpleBooleanProperty checkedProperty() {
      return checked;
   }
}



Stichworte:

JavaFX Table, Tabellen, Beispiel

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007