4 Replies Latest reply: Jun 18, 2013 7:05 PM by KonradZuse RSS

    how add gridPane within tableview

    rakeshvrm5

      it is possible to add gridpane in tableview?

        • 1. Re: how add gridPane within tableview
          James_D

          I assume you want a GridPane as part of the display in each cell. If so, you can do that using a custom cell factory.

           

          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.List;
          
          import javafx.application.Application;
          import javafx.beans.property.SimpleStringProperty;
          import javafx.beans.property.StringProperty;
          import javafx.collections.FXCollections;
          import javafx.collections.ObservableList;
          import javafx.geometry.HPos;
          import javafx.geometry.Insets;
          import javafx.scene.Scene;
          import javafx.scene.control.ContentDisplay;
          import javafx.scene.control.Label;
          import javafx.scene.control.TableCell;
          import javafx.scene.control.TableColumn;
          import javafx.scene.control.TableView;
          import javafx.scene.control.cell.PropertyValueFactory;
          import javafx.scene.control.cell.TextFieldTableCell;
          import javafx.scene.layout.ColumnConstraints;
          import javafx.scene.layout.GridPane;
          import javafx.scene.layout.VBox;
          import javafx.scene.text.Font;
          import javafx.stage.Stage;
          import javafx.util.Callback;
          
          public class TableViewSample extends Application {
          
              private TableView<Person> table = new TableView<>();
          
              @Override
              public void start(Stage stage) {
                  stage.setTitle("Table View Sample");
                  stage.setWidth(600);
                  stage.setHeight(500);
          
                  final Label label = new Label("Address Book");
                  label.setFont(new Font("Arial", 20));
          
          
                  TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
                  TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
                  TableColumn<Person, ObservableList<String>> emailCol = new TableColumn<>("Email");
                  emailCol.setMinWidth(200);
                  firstNameCol.setEditable(true);
                  lastNameCol.setEditable(true);
                  emailCol.setEditable(true);
                  table.setEditable(true);
                  table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));
          
                  final ObservableList<Person> data = FXCollections.observableArrayList(
                          new Person("Jacob", "Smith", "jacob.smith@example.com", "jsmith@example.com", "jacob@home.email.com"),
                          new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                          new Person("Ethan", "Williams", "ethan.williams@example.com", "ewilliams@example.com"),
                          new Person("Emma", "Jones", "emma.jones@example.com", "emma@home.com"),
                          new Person("Michael", "Brown", "michael.brown@example.com", "mike.brown@example.com", "mbrown@home.email.com", "mbrown@home.com"));
          
                  firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
                  lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
                  emailCol.setCellValueFactory(new PropertyValueFactory<Person, ObservableList<String>>("allEmails"));
                  firstNameCol.setMinWidth(100);
                  lastNameCol.setMinWidth(100);
                  emailCol.setMinWidth(350);
                  table.setItems(data);
                  
                  firstNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
                  lastNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
                  emailCol.setCellFactory(new Callback<TableColumn<Person,ObservableList<String>>, TableCell<Person,ObservableList<String>>>() {
                    @Override
                    public TableCell<Person, ObservableList<String>> call(TableColumn<Person, ObservableList<String>> param) {
                      return new TableCell<Person, ObservableList<String>>() {
                        private final GridPane grid ;
                        {
                          setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                          grid = new GridPane();
                          ColumnConstraints leftColumn = new ColumnConstraints();
                          ColumnConstraints rightColumn = new ColumnConstraints();
                          leftColumn.setHalignment(HPos.RIGHT);
                          leftColumn.setPrefWidth(150);
                          rightColumn.setHalignment(HPos.LEFT);
                          rightColumn.setMinWidth(200);
                          grid.getColumnConstraints().addAll(leftColumn, rightColumn);
                          grid.setHgap(10);
                          grid.setVgap(5);
                          setText(null);
                        }
                        @Override
                        public void updateItem(ObservableList<String> emails, boolean empty) {
                          super.updateItem(emails, empty);
                          if (empty) {
                            setGraphic(null);
                          } else {
                            grid.getChildren().clear();
                            grid.addRow(0, new Label("Primary email:"), new Label(emails.get(0)));
                            for (int i=1; i<emails.size(); i++) {
                              grid.addRow(i, new Label("Alternative email "+i+":"), new Label(emails.get(i)));
                            }
                            setGraphic(grid);
                          }
                        }
                      };
                    }
                  });
          
                  final VBox vbox = new VBox();
                  vbox.setSpacing(5);
                  vbox.setPadding(new Insets(10, 0, 0, 10));
                  vbox.getChildren().addAll(label, table);;//, hb);
          
                  Scene scene = new Scene(vbox);
                  stage.setScene(scene);
                  stage.show();
          
              }
          
              public static void main(String[] args) {
                  launch(args);
              }
              
              public static class Person {
                private final StringProperty firstName;
                private final StringProperty lastName;
                private final StringProperty email ;
                private final ObservableList<String> alternativeEmails ;
          
                Person(String firstName, String lastName, String email, String... alternativeEmails) {
                  this.firstName = new SimpleStringProperty(this, "firstName", firstName);
                  this.lastName = new SimpleStringProperty(this, "lastName", lastName);
                  this.email = new SimpleStringProperty(this, "email", email);
                  this.alternativeEmails = FXCollections.observableArrayList(alternativeEmails);
                }
          
                public String getFirstName() {
                  return firstName.get();
                }
          
                public void setFirstName(String firstName) {
                  this.firstName.set(firstName);
                }
          
                public StringProperty firstNameProperty() {
                  return firstName;
                }
          
                public String getEmail() {
                  return email.get();
                }
          
                public void setEmail(String email) {
                  this.email.set(email);
                }
          
                public StringProperty emailProperty() {
                  return email;
                }
                
                public String getLastName() {
                  return lastName.get();
                }
          
                public void setLastName(String lastName) {
                  this.lastName.set(lastName);
                }
          
                public StringProperty lastNameProperty() {
                  return lastName;
                }
                
                public ObservableList<String> alternativeEmailsProperty() {
                  return alternativeEmails ;
                }
                
                public ObservableList<String> getAllEmails() {
                  List<String> allEmails = new ArrayList<String>(alternativeEmails.size()+1);
                  allEmails.add(email.get());
                  allEmails.addAll(alternativeEmails);
                  return FXCollections.observableArrayList(allEmails);
                }
              }    
          }
          
          • 2. Re: how add gridPane within tableview
            rakeshvrm5

            how can i set  different background color for each grid (Like grid(0,0) is red color and grid(0,1) is blue) of GridPane. ?

            • 3. Re: how add gridPane within tableview
              James_D

              Use css on each of the items placed into the grid.

              E.g.

              Label primEmailLabel = new Label("Primary Email:");
              Label primEmail = new Label(emails.get(0));
              primEmailLabel.setStyle("-fx-background-color: red;");
              primEmail.setStyle("-fx-background-color: blue;");
              grid.addRow(0, primEmailLabel, primEmail);
              

              It's probably better to set style classes on the labels and define the actual styles in an external style sheet.

              • 4. Re: how add gridPane within tableview
                KonradZuse

                http://www.jonathangiles.net/javafx/controlsfx/javadoc/org/controlsfx/control/GridView.html

                it looks like you want something like this.  This requires Java 8 and I believe at least build 91.  I really love the GridView .