I'm using this table to display data in Table View:
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class MainApp extends Application
{
IntegerProperty intP = new SimpleIntegerProperty(5);
AnchorPane anchor = new AnchorPane();
Scene scene;
ObservableList<Integer> options
= FXCollections.observableArrayList(
5,
10,
15,
20);
final ComboBox comboBox = new ComboBox(options);
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("1", "Joe", "Pesci"),
new Person("2", "Audrey", "Hepburn"),
new Person("3", "Gregory", "Peck"),
new Person("4", "Cary", "Grant"),
new Person("5", "De", "Niro"),
new Person("6", "Katharine", "Hepburn"),
new Person("7", "Jack", "Nicholson"),
new Person("8", "Morgan", "Freeman"),
new Person("9", "Elizabeth", "Taylor"),
new Person("10", "Marcello", "Mastroianni"),
new Person("11", "Innokenty", "Smoktunovsky"),
new Person("12", "Sophia", "Loren"),
new Person("13", "Alexander", "Kalyagin"),
new Person("14", "Peter", "OToole"),
new Person("15", "Gene", "Wilder"),
new Person("16", "Evgeny", "Evstegneev"),
new Person("17", "Michael", "Caine"),
new Person("18", "Jean-Paul", "Belmondo"),
new Person("19", " Julia", "Roberts"),
new Person("20", "James", "Stewart"),
new Person("21", "Sandra", "Bullock"),
new Person("22", "Paul", "Newman"),
new Person("23", "Oleg", "Tabakov"),
new Person("24", "Mary", "Steenburgen"),
new Person("25", "Jackie", "Chan"),
new Person("26", "Rodney", "Dangerfield"),
new Person("27", "Betty", "White"),
new Person("28", "Eddie", "Murphy"),
new Person("29", "Amitabh", "Bachchan"),
new Person("30", "Nicole", "Kidman"),
new Person("31", "Adriano", "Celentano"),
new Person("32", "Rhonda", " Fleming's"),
new Person("32", "Humphrey", "Bogart"));
private Pagination pagination;
public static void main(String[] args) throws Exception
{
launch(args);
}
public int itemsPerPage()
{
return 1;
}
public int rowsPerPage()
{
return intP.get();
}
public VBox createPage(int pageIndex)
{
int lastIndex = 0;
int displace = data.size() % rowsPerPage();
if (displace > 0)
{
lastIndex = data.size() / rowsPerPage();
}
else
{
lastIndex = data.size() / rowsPerPage() - 1;
}
VBox box = new VBox();
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++)
{
TableView<Person> table = new TableView<>();
TableColumn numCol = new TableColumn("ID");
numCol.setCellValueFactory(new PropertyValueFactory<>("num"));
numCol.setMinWidth(20);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
firstNameCol.setMinWidth(160);
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
lastNameCol.setMinWidth(160);
table.getColumns().addAll(numCol, firstNameCol, lastNameCol);
if (lastIndex == pageIndex)
{
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + displace)));
}
else
{
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
}
box.getChildren().addAll(table);
}
return box;
}
@Override
public void start(final Stage stage) throws Exception
{
scene = new Scene(anchor, 450, 450);
comboBox.valueProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue o, Number oldVal, Number newVal)
{
//System.out.println(newVal.intValue());
intP.set(newVal.intValue());
paginate();
}
});
paginate();
stage.setScene(scene);
stage.setTitle("Table pager");
stage.show();
}
public void paginate()
{
pagination = new Pagination((data.size() / rowsPerPage() + 1), 0);
// pagination = new Pagination(20 , 0);
// pagination.setStyle("-fx-border-color:red;");
pagination.setPageFactory(new Callback<Integer, Node>()
{
@Override
public Node call(Integer pageIndex)
{
if (pageIndex > data.size() / rowsPerPage() + 1)
{
return null;
}
else
{
return createPage(pageIndex);
}
}
});
AnchorPane.setTopAnchor(pagination, 10.0);
AnchorPane.setRightAnchor(pagination, 10.0);
AnchorPane.setBottomAnchor(pagination, 10.0);
AnchorPane.setLeftAnchor(pagination, 10.0);
AnchorPane.setBottomAnchor(comboBox, 40.0);
AnchorPane.setLeftAnchor(comboBox, 12.0);
anchor.getChildren().clear();
anchor.getChildren().addAll(pagination, comboBox);
}
public static class Person
{
private final SimpleStringProperty num;
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String id, String fName, String lName)
{
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.num = new SimpleStringProperty(id);
}
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 getNum()
{
return num.get();
}
public void setNum(String id)
{
num.set(id);
}
}
}
When I change the number of rows using Combo box only data in table rows is changed. Table height is not changed.
Is there a way to remove empty rows?
Here's my solution, in order not to be deeply dependant on
table.getFixedCellSize
(we yet depend on it during FX initialization, while CSS is not yet computed/applied).Note that we also need to add some pixels (don't understand why).
Just change background color of empty rows using css
and modify number of rows on the basis of combobox.
If you're not wedded to bindings, a simple way to do this is to calculate the desired height based on the fixed cell size (cf. Fred Danna's answer) and update it with a listener on the table data.
In
start(Stage)
, we create the table and add aListChangeListener
:Note that the table header row doesn't exist till after
stage.show()
, so the simplest thing to do is to wait to set the table data till then. Alternatively, we could set the data at table construction time, and then callsetTableHeightByRowCount()
explicitly:Unfortunately, configuration of the visibleRowCount isn't supported in TableView (you might consider filing a feature request in fx' jira - no need, already done years ago). And it's not entirely straightforward to let the view return a prefHeight based on a such a preference: we'll need to measure the size requirements of the "real" cell and that's somehow buried inside the bowels.
Just for fun, experimented with extending the whole stack of collaborators:
The code:
Note that you might get away with a plain override of table's prefHeight when having a fixed-cell-size, didn't try that - no risk no fun :-)
Is there a way to do it... yes, what you need to do is when you create the table (since you re-create it every time you select a new number) you need to compute what the height of the table is with the current number of entries, and then use the
setPrefHeight()
property of TableView to make the table smaller to account for only those rows.I toyed with it a little bit, and I didn't find any quick solutions to this to calculate the size of the table properly, so I don't have any code for you, but that is what you need to do. You could also 'style' the table to not have the alternating color scheme, which would make the rows below the ones that have data look 'empty' even though there would be some white space.
Good luck!
Changing tableview's height and removing "empty" rows are two different things. Be specific.
For removing rows see this tutorial.
For changing height, first set the
fixedCellSizeProperty
of the table view then use it in binding:Adding 30px is for tableview's header.