javafx表中的自动行编号 [英] Automatic row numbering in javafx table

查看:127
本文介绍了javafx表中的自动行编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,用于动态行号 Java Swing 表即的JTable 。我是 JavaFX 的新手,并希望在 JavaFX 中使用相同的内容。有没有办法在中设置自动行号?JavaFX

I have a sample code that we use to dynamic row numbers in Java Swing Table i.e JTable. I new to JavaFX and would like to the same in JavaFX. Is there is any way to set automatic row numbers in JavaFX Table

 class LineNumberTable extends JTable {

            private JTable mainTable;

            public LineNumberTable(JTable table) {
                super();
                mainTable = table;
                setAutoCreateColumnsFromModel(false);
                setModel(mainTable.getModel());
                setAutoscrolls(false);
                addColumn(new TableColumn());
                getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
                getColumnModel().getColumn(0).setPreferredWidth(40);
                setPreferredScrollableViewportSize(getPreferredSize());

            }

            @Override
            public boolean isCellEditable(int row, int col) {
                if (col == uneditableColumn) {
                    return false;
                }
                return bEdit;
            }

            @Override
            public Object getValueAt(int row, int column) {
                return Integer.valueOf(row + 1);
            }

            @Override
            public int getRowHeight(int row) {
                return mainTable.getRowHeight();
            }
        }


推荐答案

In JavaFX,您使用 TableColumn 使用CellFactories和CellValueFactories填充 TableView

In JavaFX, you use TableColumns with CellFactories and CellValueFactories to populate your TableView.

JavaFX教程有一个文章,可能会帮助你入门。

The JavaFX tutorials have an article that might get you started.

在我使用的一种方法中,我将业务对象转换为显示为表示对象,并将所有必要的属性(如您的情况,数字)添加到它们。

In one approach I have used I convert the business objects to display into presentation objects and add all necessary properties (like in your case, the number) to them.

编辑:在第二种更清洁的方法中,您可以将CellFactory设置为创建 TableCell ,以显示其自己的 index 中的属性TableCell#updateItem(S,boolean)

In a second, cleaner approach, you could set your CellFactory to create a TableCell that shows its own index property in TableCell#updateItem(S, boolean):

public class NumberedCell extends TableCell{

  protected void updateItem(Object object, boolean selected){
    setText(String.valueOf(getIndex());
  }
}

这篇关于javafx表中的自动行编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆