Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Column headings do not line up after scrolling to the right and maximizing in TableView

RathuJul 16 2014

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);
            }
        }
    }
}

Step 1 : run the code

Step 2 :Move the scrollbar all the way to the right.

Step 3: Maximize the screen

You can see the column headings are not aligned properly to the respective column values.

Anyone else is facing this issue?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2014
Added on Jul 16 2014
0 comments
694 views