<java><JTable>使用AbstractTableModel实现更新、删除、插入数据

本文详细介绍使用Java Swing中的JTable进行数据的添加、删除及更新操作。通过AbstractTableModel实现数据模型,确保表格数据准确更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继上次JTable实现号码分布的博文之后,这次写一下关于JTable数据的更新、删除、插入吧。

同样的,本文将以abstractTableModel为例,其他数据导入方式可以参考。主界面如下:

数据从文件导入,首先是添加。添加的时候会弹出一个对话框,提示输入插入的相关信息,这里不做叙述。添加时,会判断期数对不对(即有无重复的期数),判断方式如下:

                                for(int i=0; i<histroyDataTableMode.getRowCount(); i++) {
                                    if(histroyDataTableMode.getValueAt(i, 0).equals(stageValue)) {
                                        JOptionPane.showMessageDialog(null, "期数不对:\n所插入的期数已经存在!");
                                        validInput = false;
                                        break;
                                    }
                                }
使用getValueAt获取第一列的值,与输入的stageValue作比较。然后showDialog即可。判断正确后,即写入文件,写入文件部分,,,以后再写吧。现在不高兴写,太累了, 大中午的想睡觉 - -! 文件写完后更新table,更新table如下:


 
// 在表中显示
AnalyseDataInfo dataToInsert = new AnalyseDataInfo();                                     dataToInsert.SetStage(stageValue);                                     for(int i=0; i<6; i++) {                                         dataToInsert.SetRedBall(i, redBallVale[i]);                                     }                                     dataToInsert.SetBlueBall(blueBallValue);                                     histroyDataTableMode.add(dataToInsert);
其中add方法为:

    public void add(AnalyseDataInfo data) {
        int index = stageDatas.size();
        stageDatas.add(data);
        fireTableRowsInserted(index, index);
    }
stageDatas为插入数据的一个全部变量链表,此处直接添加,然后fireTableRowsInserted即可。这是添加部分。


接下来是删除部分。删除部分大同小异,由于我是用AbstractTableModel,因此很简单的,在删除部分直接:

    public void delete(int stage) {
        for(int i=0; i<stageDatas.size(); i++) {
            int stageInList = stageDatas.get(i).GetStage();
            if(stage == stageInList) {
                stageDatas.remove(i);
                fireTableDataChanged();
                break;
            }
        }
    }
就大功告成。比较简单,嘿嘿。


最后便是更新数据了。更新数据也差不多,这里我提一下,在更新数据的过程中,一开始我直接使用的是AbstractTableModel的API:setValueAt更新,结果Table里面的数据死活不变,搞的我一开始是以为刷新不及时,然后尝遍各种刷新方法,什么repiant、updateUI、validate;也试过修改AbstractTableModel的各种fire,等等等等,各种蛋疼。结果到最后才发现原来是setValueAt这个API压根没起作用。气的我快疯了。setValueAt手册里描述如下:

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
由于提供了此空实现,因此,如果用户的数据模型是不可编辑的,则他们不必实现此方法。 
哎,不靠谱啊不靠谱。后来尝试修改链表值才发现原来可以如此简单,也使我后面的添加操作,删除操作一气呵成,由此也发现AbstractTableModel的好处,嘿嘿。

总结,JTable只是一个显示内容的框体,正常幕后操作数据的,是实现的TableModel。


OK,本文到此结束。




使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值