我在我的程序中使用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;
}

单击并自动插入行的JavaFX TableView编辑?的更多相关文章

  1. canvas中普通动效与粒子动效的实现代码示例

    canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。本文详细的介绍了粒子特效,和普通动效进行对比,非常具有实用价值,需要的朋友可以参考下

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. canvas学习和滤镜实现代码

    这篇文章主要介绍了canvas学习和滤镜实现代码,利用 canvas,前端人员可以很轻松地、进行图像处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. localStorage的过期时间设置的方法详解

    这篇文章主要介绍了localStorage的过期时间设置的方法详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. 详解HTML5 data-* 自定义属性

    这篇文章主要介绍了详解HTML5 data-* 自定义属性的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 教你使用Canvas处理图片的方法

    本篇文章主要介绍了教你使用Canvas处理图片的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. ios – Swift语言:如何调用SecRandomCopyBytes

    从Objective-C,我可以这样做:在Swift中尝试这个时,我有以下内容:但我得到这个编译器错误:data.mutableBytes参数被拒绝,因为类型不匹配,但我无法弄清楚如何强制参数.解决方法这似乎有效:

  9. 使用Firebase iOS Swift将特定设备的通知推送到特定设备

    我非常感谢PushNotifications的帮助.我的应用聊天,用户可以直接向对方发送短信.但是如果没有PushNotifications,它就没有多大意义.它全部设置在Firebase上.如何将推送通知从特定设备发送到特定设备?

  10. ios – NSData to Data swift 3

    如何将此代码转换为使用Swift3数据?

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部