Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093156

Misalignment between header and content in tableview

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 8u20
    • 8u5
    • javafx
    • Windows 8

      Hi,

        In the below example ,


      import javafx.application.Application;
      import javafx.beans.property.SimpleBooleanProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.value.ObservableValue;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TableCell;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.Callback;

      import com.sun.prism.impl.Disposer.Record;


      public class TableViewDeleteSample extends Application {
          
          private TableView<Person> tableView = new TableView<Person>();
        

          private final ObservableList<Person> data =
                  FXCollections.observableArrayList(
                  new Person("Eliza", "Smith", "eliza.smith@javafxpro.com"),
                  new Person("Isabella", "Johnson", "isabella.johnson@javafxpro.com"),
                  new Person("Imran", "Williams", "imran.williams@javafxpro.com"),
                  new Person("Emma", "Jones", "emma.jones@javafxpro.com"),
                  new Person("Russel", "Peters", "russel.peters@javafxpro.com"));

          
          public static class Person {
            
              private final SimpleStringProperty firstName;
              private final SimpleStringProperty lastName;
              private final SimpleStringProperty email;

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

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

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

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

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

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

              public void setEmail(String fName) {
                  email.set(fName);
              }
          }
          
          @SuppressWarnings("unchecked")
       @Override
          public void start(Stage primaryStage) {
              primaryStage.setTitle("Table With Delete Row");
              tableView.setEditable(false);
              
              TableColumn<Person, String> firstName = new TableColumn<Person, String>("First Name");
              TableColumn<Person, String> lastName = new TableColumn<Person, String>("Last Name");
              TableColumn<Person, String> email = new TableColumn<Person, String>("Email");
              
              TableColumn<Person, String> firstName1 = new TableColumn<Person, String>("First Name Dup");
              TableColumn<Person, String> lastName1 = new TableColumn<Person, String>("Last Name Dup");
              TableColumn<Person, String> email1 = new TableColumn<Person, String>("Email Dup");
              
              
              firstName.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
              lastName.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
              email.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
              
               firstName1.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
              lastName1.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
              email1.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
              
              tableView.getColumns().add(firstName);
              tableView.getColumns().add(lastName);
              tableView.getColumns().add(email);
              
              tableView.getColumns().add(firstName1);
              tableView.getColumns().add(lastName1);
              tableView.getColumns().add(email1);
              
              //Insert Button
              TableColumn col_action = new TableColumn<>("Action");
              tableView.getColumns().add(col_action);
              
              col_action.setCellValueFactory(
                      new Callback<TableColumn.CellDataFeatures<Record, Boolean>,
                      ObservableValue<Boolean>>() {

                  @Override
                  public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
                      return new SimpleBooleanProperty(p.getValue() != null);
                  }
              });

              //Adding the Button to the cell
              col_action.setCellFactory(
                      new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {

                  @Override
                  public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
                      return new ButtonCell();
                  }
              
              });
              
              
              tableView.setItems(data);
              
              VBox vBox = new VBox();
              vBox.getChildren().addAll(tableView);
              primaryStage.setScene(new Scene(vBox, 440, 400));
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
          
          //Define the button cell
          private class ButtonCell extends TableCell<Record, Boolean> {
              final Button cellButton = new Button("Delete");
              
              ButtonCell(){
                  
               //Action when the button is pressed
                  cellButton.setOnAction(new EventHandler<ActionEvent>(){

                      @Override
                      public void handle(ActionEvent t) {
                          // get Selected Item
                       Person currentPerson = (Person) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
                       //remove selected item from the table list
                       data.remove(currentPerson);
                      }
                  });
              }

              //Display button if the row is not empty
              @Override
              protected void updateItem(Boolean t, boolean empty) {
                  super.updateItem(t, empty);
                  if(!empty){
                      setGraphic(cellButton);
                  }
              }
          }
      }
         I moved the horizontal scrollbar to right and then maximized the window to full.
         After that the column headings are not aligned properly with the column values.
        When I click on any column header then the header aligns with the content.

      If it is a known issue , is there any workaround for this?

            jgiles Jonathan Giles
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: