单击并自动插入行的JavaFX TableView编辑?

我在我的程序中使用tableview来显示它很好的几行.
我的程序用于会计,你知道有很多数据插入,我使用表视图,我遇到了很多问题:.

问题1:当用户想要修改一个值时,他会双击以编辑单元格,我希望我的用户选择任何行并开始输入.

问题2:在我的程序中,用户可以插入100行一天的工作我需要一个策略来自动添加行,例如当他插入行时,它应该在当前行下添加一个空行以轻松完成他的数据输入.

请帮我解决这些问题,因为我的用户每天都会输入大量数据.
谢谢.

解决方法

这是一个例子

>在键入的单元格上输入后立即进入编辑模式
>在最后一行按Enter键时创建一个新行(仅作为示例).或者,您可以单击按钮添加新行.如果您希望在最后一行使用tab来创建新行,则必须相应地更改代码.

我建议不要使用空行,因为它们出现在你的模型中.

InlineEditingWithDynamicRowAdding.java

import javafx.application.Application;
import javafx.scene.control.TableView;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableCell;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.DoubleProperty;
import javafx.collections.FXCollections;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.KeyCode;
import javafx.scene.control.TablePosition;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.util.StringConverter;
import javafx.scene.Scene;
import javafx.scene.control.cell.TextFieldTableCell;

public class InlineEditingWithDynamicRowAdding extends Application {

    private final ObservableList<Data> data =
        FXCollections.observableArrayList(
                new Data(1.,5.),new Data(2.,6.),new Data(3.,7.),new Data(4.,8.)
        );

    private TableView<Data> table;

    @Override
    public void start(Stage stage) {

        // create edtiable table
        table = new TableView<Data>();
        table.setEditable(true);

        // column 1 contains numbers
        TableColumn<Data,Number> number1Col = new TableColumn<>("Number 1");
        number1Col.setMinWidth(100);
        number1Col.setCellValueFactory( cellData -> cellData.getValue().number1Property());
        number1Col.setCellFactory( createNumberCellFactory());

        // column 2 contains numbers
        TableColumn<Data,Number> number2Col = new TableColumn<>("Number 2");
        number2Col.setMinWidth(100);
        number2Col.setCellValueFactory( cellData -> cellData.getValue().number2Property());
        number2Col.setCellFactory( createNumberCellFactory());

        // add columns & data to table
        table.setItems(data);
        table.getColumns().addAll( number1Col,number2Col);




        // switch to edit mode on keypress
        // this must be KeyEvent.KEY_PRESSED so that the key gets forwarded to the editing cell; it wouldn't be forwarded on KEY_RELEASED
        table.addEventFilter(KeyEvent.KEY_PRESSED,new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {

                if( event.getCode() == KeyCode.ENTER) {
//                  event.consume(); // don't consume the event or else the values won't be updated;
                    return;
                }

                // switch to edit mode on keypress,but only if we aren't already in edit mode
                if( table.getEditingCell() == null) {
                    if( event.getCode().isLetterKey() || event.getCode().isDigitKey()) {  

                        TablePosition focusedCellPosition = table.getFocusModel().getFocusedCell();
                        table.edit(focusedCellPosition.getRow(),focusedCellPosition.getTableColumn());

                    }
                }

            }
        });

        table.addEventFilter(KeyEvent.KEY_RELEASED,new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {

                if( event.getCode() == KeyCode.ENTER) {


                    // move focus & selection
                    // we need to clear the current selection first or else the selection would be added to the current selection since we are in multi selection mode 
                    TablePosition pos = table.getFocusModel().getFocusedCell();

                    if (pos.getRow() == -1) {
                        table.getSelectionModel().select(0);
                    } 
                    // add new row when we are at the last row
                    else if (pos.getRow() == table.getItems().size() -1) {
                        addRow();
                    } 
                    // select next row,but same column as the current selection
                    else if (pos.getRow() < table.getItems().size() -1) {
                        table.getSelectionModel().clearAndSelect( pos.getRow() + 1,pos.getTableColumn());
                    }


                }

            }
        });     

        // single cell selection mode
        table.getSelectionModel().setCellSelectionEnabled(true);

        // add row index column as 1st column
        // -------------------------------------
        TableColumn<Data,Data> indexCol = new TableColumn<Data,Data>("#");

        indexCol.setCellFactory(new Callback<TableColumn<Data,Data>,TableCell<Data,Data>>() {
            @Override public TableCell<Data,Data> call(TableColumn<Data,Data> param) {
                return new TableCell<Data,Data>() {
                    @Override protected void updateItem(Data item,boolean empty) {
                        super.updateItem(item,empty);

                        if (this.getTableRow() != null) {

                            int index = this.getTableRow().getIndex();

                            if( index < table.getItems().size()) {
                                int rowNum = index + 1;
                                setText( String.valueOf(rowNum));
                            } else {
                                setText("");
                            }

                        } else {
                            setText("");
                        }

                    }
                };
            }
        });

        table.getColumns().add( 0,indexCol); // number column is at index 0

        // allow multi selection
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        // buttons
        // --------------------------------------------
        FlowPane buttonBar = new FlowPane();

        // add new row button
        Button addButton = new Button( "Add");
        addButton.setOnAction(e -> {
            addRow();
        });
        addButton.setFocusTraversable(false);// don't let it get the focus or else the table would lose it when we click the button and we's have to request the focus on the table in the event handler

        // remove selected rows button
        Button removeButton = new Button( "Remove");
        removeButton.setOnAction(e -> {
            removeSelectedRows();
        });
        removeButton.setFocusTraversable(false);// don't let it get the focus or else the table would lose it when we click the button and we's have to request the focus on the table in the event handler

        buttonBar.getChildren().addAll( addButton,removeButton);

        // add nodes to stage
        BorderPane root = new BorderPane();
        root.setCenter(table);
        root.setBottom(buttonBar);

        Scene scene = new Scene( root,800,600);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

        stage.setScene(scene);
        stage.show();


        // select first cell
        // TODO: why isn't this selecting the 1st cell in the index column?
        table.getSelectionModel().selectFirst();

    }

    /**
     * Insert a new default row to the table,select a cell of it and scroll to it. 
     */
    public void addRow() {

        // get current position
        TablePosition pos = table.getFocusModel().getFocusedCell();

        // clear current selection
        table.getSelectionModel().clearSelection();

        // create new record and add it to the model
        Data data = new Data(0d,0d);
        table.getItems().add( data);

        // get last row
        int row = table.getItems().size() - 1;
        table.getSelectionModel().select( row,pos.getTableColumn());

        // scroll to new row
        table.scrollTo( data);

    }

    /**
     * Remove all selected rows.
     */
    public void removeSelectedRows() {

        table.getItems().removeAll(table.getSelectionModel().getSelectedItems());

        // table selects by index,so we have to clear the selection or else items with that index would be selected 
        table.getSelectionModel().clearSelection();


    }

    /**
     * Number cell factory which converts strings to numbers and vice versa.
     * @return
     */
    private Callback<TableColumn<Data,Number>,Number>>  createNumberCellFactory() {

        Callback<TableColumn<Data,Number>> factory = TextFieldTableCell.forTableColumn( new StringConverter<Number>() {

            @Override
            public Number fromString(String string) {
                return Double.parseDouble(string);
            }

            @Override
            public String toString(Number object) {
                return object.toString();
            }
        });

        return factory;
    }

    /**
     * Table data container
     */
    public static class Data {

        private final SimpleDoubleProperty number1;
        private final SimpleDoubleProperty number2;

        private Data( Double number1,Double number2) {
            this.number1 = new SimpleDoubleProperty(number1);
            this.number2 = new SimpleDoubleProperty(number2);
        }

        public final DoubleProperty number1Property() {
            return this.number1;
        }

        public final double getNumber1() {
            return this.number1Property().get();
        }

        public final void setNumber1(final double number1) {
            this.number1Property().set(number1);
        }

        public final DoubleProperty number2Property() {
            return this.number2;
        }

        public final double getNumber2() {
            return this.number2Property().get();
        }

        public final void setNumber2(final double number2) {
            this.number2Property().set(number2);
        }


    }

    public static void main(String[] args) {
        launch(args);
    }


}

application.css

/* edit cell appearance */

/* If the padding is 0,then there's a grey area. If you set it to 2,then there won't be a great area.
 * However if you use a border color,then the border requires its size,so the padding is set to 2 - 1 = 1.
 * You have to toy around with the border. If necessary give it a width of 2 and make it the same color as the background color.
 */
.text-field-table-cell .text-field {
   -fx-padding: 1; 
   -fx-border-color:red; 
   -fx-border-width:1; 
   -fx-background-color:yellow;
 }
.table-cell:focused {
   -fx-padding: 0;
}

/* right-align the cell content in view mode */
.table-cell {
    -fx-alignment: CENTER-RIGHT;
}
/* right-align the cell content in edit mode */
.text-field {
    -fx-alignment: CENTER-RIGHT;
}

/* colorize background only of rows which have data */
.table-row-cell:empty {
    -fx-background-color: white;
}

.table-row-cell:empty .table-cell {
    -fx-border-width: 0px;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrlʺlt+L 格式化代码Ctrlʺlt+O 优化导入的类和包Alt+Insert 生成代码(如get,set方法,构造函数等)Ctrlʾ或者Alt+Shiftʼ 最近更改的代码Ctrl+R 替换文本Ct
运行程序出现下面错误:HTTP Status 500 ---------------------------------------------------------------------------------type Exception reportmessagedescription Th
1、建立DM的profile,使用的模版在install_root/profileTemplates/dmgr下句法为:manageprofile.sh -create -templatePath install_root/profileTemplates/dmgr调用参数为:-create 建立一
使用dom4j解析XML时,要快速获取某个节点的数据,使用XPath是个不错的方法,dom4j的快速手册里也建议使 用这种方式,标题都写的这么阔气:Powerful Navigation with XPath。 方法是使用Document的selectNodes(String XPath)方法,代码
英文操作系统导致 Debug 下的变量查看时显示乱码,可通过改变字体解决此问题。
eclipse中javascript报错问题处理:三个地方:&lt;1&gt;&quot;eclipse设置 &quot;:Java代码window-&gt;preference-&gt;Validator-&gt;Errors/Warnings-&gt;Enable Javascript Sema
打开eclipse中文字体很小,简直难以辨认。在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了。解决方式有两种:一、把字体设置为Courier New操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“
如果不加密码,默认只能本机访问,加密码也是为了安全考虑 1.进入Redis&#160;的安装目录,找到redis.conf文件。用vi命令打开文件 输入 / requirepass 进行查找,输入n查找下一个。 (最好复制一个新的conf文件) 在红背景处设置密码 2.重启 Redis &amp;
设置LINUX 自启动: 在/etc/rc.d/rc.local中加入: conf 目录下一个文件&#160;server.xml
ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢,Vector由于使用了synchronized方法(线程安全)所以性能上比ArrayList要差,
在实现设计模式之前,首先来复习以下UML中的五种关系图 依赖&lt;关联&lt;聚合&lt;组合 &lt;1&gt;依赖 依赖关系用虚线加箭头表示,如图所示: 上图表示:Animal类依赖于Water类(动物依赖于水)。 依赖是类的五种关系中耦合最小的一种关系。因为依赖关系在生成代码的时候,这两个关
第一步:准备包:日志相关包jcl-over-slf4j-1.6.1.jarlogback-classic-0.9.29.jarlogback-core-0.9.29.jarslf4j-api-1.6.1.jarjstl包jstl-1.2.jarspring 相关包org.springframewor
当运行这个web程序时,无法运行,提示错误如下: 当时安装的tomcat是tomcat7版本,安装的jdk版本是1.6。 配置的tomcat如下:window-Preferences-Server-Runtime Environment,添加tomcat。如下: 检查多次,tomcat安装,环境配置
代码中 会让补全,否则会报&#160;diamond operator is not supported in -source 1.5 需要在POM中指定 source 版本号
原因:这是由于jdk的版本与项目的要求不一致造成的,如果是maven项目,首先查看一下pom.xml,以我的项目为例: 从其中可以看出要求的编译插件为1.8版本,而我本机上安装的jdk为1.7版本,因此需要首先下载安装1.8版本的jdk下载链接为 jdk下载链接 然后在intellij idea中点
照着教程弄的第一个 DEMO,结果启不来。 解决办法:在Controller 上面加上&#160;@EnableAutoConfiguration 成功启动 Demo的其它内容及配置如下图,新建一个 空的 Maven 项目 Pom.xml 主界面: Control.java 运行报错 :: Spri
如下图所示,我的是 2018,不同版本,Schema 可能要 Save As一下
Ant Design Pro Vue 打包发布到Tomcat后,刷新报错404解决方法 在应用下面加 WEB-INF&#160;建&#160;web.xml&#160;内容如下 &lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&qu
效果如图: JAVA&#160;代码 public static void main(String[] args) throws Exception { String str = &quot;&lt;row PTID=\&quot;80268175\&quot; ZYH=\&quot;2002868
HTTP Status 500 - Handler processing failed; nested exception is java.lang.AbstractMethodError: org.apache.xerces.dom.ElementNSImpl.setUserData(Ljava/