use*_*928 18 javafx tableview javafx-2 javafx-8
我正在使用此表在表视图中显示数据:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用组合框更改行数时,只更改表行中的数据.表高度不变.
有没有办法删除空行?
Ulu*_*Biy 28
更改tableview的高度和删除"空"行是两回事.请明确点.
要删除行,请参阅本教程.
要更改高度,首先设置fixedCellSizeProperty
表视图,然后在绑定中使用它:
table.setFixedCellSize(25);
table.prefHeightProperty().bind(Bindings.size(table.getItems()).multiply(table.getFixedCellSize()).add(30));
Run Code Online (Sandbox Code Playgroud)
添加30px是tableview的标题.
kle*_*tra 14
不幸的是,TableView不支持visibleRowCount的配置(您可以考虑在fx中提交功能请求'jira - 不需要,已经在几年前完成了).并且根据这样的偏好让视图返回prefHeight并不是完全直截了当的:我们需要测量"真实"细胞的大小要求,并且它以某种方式埋在肠内.
只是为了好玩,尝试扩展整个协作者堆栈:
代码:
/**
* TableView with visibleRowCountProperty.
*
* @author Jeanette Winzenburg, Berlin
*/
public class TableViewWithVisibleRowCount<T> extends TableView<T> {
private IntegerProperty visibleRowCount = new SimpleIntegerProperty(this, "visibleRowCount", 10);
public IntegerProperty visibleRowCountProperty() {
return visibleRowCount;
}
@Override
protected Skin<?> createDefaultSkin() {
return new TableViewSkinX<T>(this);
}
/**
* Skin that respects table's visibleRowCount property.
*/
public static class TableViewSkinX<T> extends TableViewSkin<T> {
public TableViewSkinX(TableViewWithVisibleRowCount<T> tableView) {
super(tableView);
registerChangeListener(tableView.visibleRowCountProperty(), "VISIBLE_ROW_COUNT");
handleControlPropertyChanged("VISIBLE_ROW_COUNT");
}
@Override
protected void handleControlPropertyChanged(String p) {
super.handleControlPropertyChanged(p);
if ("VISIBLE_ROW_COUNT".equals(p)) {
needCellsReconfigured = true;
getSkinnable().requestFocus();
}
}
/**
* Returns the visibleRowCount value of the table.
*/
private int getVisibleRowCount() {
return ((TableViewWithVisibleRowCount<T>) getSkinnable()).visibleRowCountProperty().get();
}
/**
* Calculates and returns the pref height of the
* for the given number of rows.
*
* If flow is of type MyFlow, queries the flow directly
* otherwise invokes the method.
*/
protected double getFlowPrefHeight(int rows) {
double height = 0;
if (flow instanceof MyFlow) {
height = ((MyFlow) flow).getPrefLength(rows);
}
else {
for (int i = 0; i < rows && i < getItemCount(); i++) {
height += invokeFlowCellLength(i);
}
}
return height + snappedTopInset() + snappedBottomInset();
}
/**
* Overridden to compute the sum of the flow height and header prefHeight.
*/
@Override
protected double computePrefHeight(double width, double topInset,
double rightInset, double bottomInset, double leftInset) {
// super hard-codes to 400 .. doooh
double prefHeight = getFlowPrefHeight(getVisibleRowCount());
return prefHeight + getTableHeaderRow().prefHeight(width);
}
/**
* Reflectively invokes protected getCellLength(i) of flow.
* @param index the index of the cell.
* @return the cell height of the cell at index.
*/
protected double invokeFlowCellLength(int index) {
double height = 1.0;
Class<?> clazz = VirtualFlow.class;
try {
Method method = clazz.getDeclaredMethod("getCellLength", Integer.TYPE);
method.setAccessible(true);
return ((double) method.invoke(flow, index));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return height;
}
/**
* Overridden to return custom flow.
*/
@Override
protected VirtualFlow createVirtualFlow() {
return new MyFlow();
}
/**
* Extended to expose length calculation per a given # of rows.
*/
public static class MyFlow extends VirtualFlow {
protected double getPrefLength(int rowsPerPage) {
double sum = 0.0;
int rows = rowsPerPage; //Math.min(rowsPerPage, getCellCount());
for (int i = 0; i < rows; i++) {
sum += getCellLength(i);
}
return sum;
}
}
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger.getLogger(TableViewWithVisibleRowCount.class
.getName());
}
Run Code Online (Sandbox Code Playgroud)
请注意,当具有固定单元格大小时,您可能会使用表格的prefHeight的简单覆盖,但没有尝试 - 没有风险没有乐趣:-)
有没有办法做到这一点...是的,您需要做的是当您创建表格时(因为每次选择新数字时都会重新创建它),您需要计算表格的高度当前的条目数,然后使用setPrefHeight()
TableView 的属性缩小表以仅包含这些行。
我玩了一下,但没有找到任何快速解决方案来正确计算表的大小,所以我没有任何代码给你,但这就是你需要做的。您还可以对表格进行“样式”设置,使其不具有交替配色方案,这将使包含数据的行下方的行看起来“空”,即使会有一些空白。
祝你好运!
归档时间: |
|
查看次数: |
16558 次 |
最近记录: |