Java JTable和类Excel公式

Java JTable和类Excel公式,java,swing,jtable,excel-formula,Java,Swing,Jtable,Excel Formula,经过一些研究,我发现了一种在JTable中实现公式的方法(如果我理解正确的话,这是一种漫长而乏味的方法) 下面是这个表的样子(您可能知道,它是用netbeans编辑的,不要介意大量的null部分,我们稍后会讨论) 因此,问题是,在name1/2/etc行和列中应该有数字(而不是netbeans自动生成的空垃圾邮件,我在制作自定义tableModel的应用程序中有一个“重置”按钮,但这是一个旧版本,在这里没有用处)在总计行/列中,应包含相应列/行中的数字总和 从我读到的许多线程中,有一个getVa

经过一些研究,我发现了一种在JTable中实现公式的方法(如果我理解正确的话,这是一种漫长而乏味的方法)

下面是这个表的样子(您可能知道,它是用netbeans编辑的,不要介意大量的null部分,我们稍后会讨论)

因此,问题是,在name1/2/etc行和列中应该有数字(而不是netbeans自动生成的空垃圾邮件,我在制作自定义tableModel的应用程序中有一个“重置”按钮,但这是一个旧版本,在这里没有用处)在总计行/列中,应包含相应列/行中的数字总和

从我读到的许多线程中,有一个getValueAt方法,我可以使用它对相应行/列中的单元格求和。不过,我不太确定这是一个好的选择,因为它每次都需要读取大量的单元格(当用户在表中输入数字时,它应该以数字方式更新总数,而不是计数,我也不知道如何在特定单元格中实现它)

以下是问题:

a)是否可以制作一个类似excel的公式来添加单元格?(类似于添加单元格c2:c6,因为我确实在excel文件中导出了该表,所以我认为这样做是可能的。)

2a)我是否必须将列类型更改为object,或者所述公式仍将被视为整数?(除了作为字符串类型实现的名称列之外,所有其他列都是整数类型,最终用户也应该只能插入数字,而不会弄乱公式,但将类型更改为Object实际上也可以插入字母)

b)是否可以使用表上的eventListeners生成一个非excel格式的公式(在案例a中,这是不可能的)?(类似于“行(x)中的数据,列(y)已更改,使用总和更新了总单元格!”)

c)由于表中的数据导出到一个xls文件(在另一个线程中获得几个指针后已正确实现),是否可以使用该文件再次填充表,或者每次都必须重新绘制

我很抱歉同时问了所有这些问题(特别是c-因为它与标题无关…),但我没有在jTable类教程中找到oracle提供的任何内容,也没有在其他类似的ish线程中找到任何建设性的建议

请耐心等待,正如在另一篇文章中所说的,我仍然是一个初学者(如果我可以被认为是初学者的话!)另外,提前感谢你的帮助,另一篇文章中的指针确实帮助了我

PS:我希望我能以一种可以理解的方式解释它,作为编程界的新手,当你不得不解释一些东西时,事情并不会变得容易

再次感谢您的帮助~

编辑:

下面是我为在表中实现公式而编写的代码(实际上,get/setValueAt()的用法很奇怪,但它似乎工作得相当好(有点笨拙,但有改进的余地只会让我学到更多东西,这在一天结束时总是很好的)

class按钮{
公共总计按钮(JTable JTable){
//获取表格模型
TableModel model=jTable.getModel();
//为变量声明
int x;
int-y;
//初始化表占位符
int totalValue=0;
int rowCount=model.getRowCount();
int columnCount=model.getColumnCount();
//第一行的for循环
对于(x=0;x
基本上,我添加了一个按钮来计算和填充表的总行/列(在某个点添加了一个keyListener,但正如我所说的,它感觉有点笨重..并不是说使用按钮感觉有什么不同,而是很好..)

可以在代码片段中读取的循环是为所有行/列中的每个单元格添加的(只发布了一部分,因为每个单元格的内容几乎相同)

还必须将jTable模型中的null-s改为“-s”,以便将列类型设置为String(后来用于Integer.parseInt()和Integer.toString()

另一方面,虽然keyListener感觉有点笨拙,但它会“实时”更新表(说“实时”,因为在表中插入新值在下一次击键之前不会算作keyPressed())

再次感谢您的指点,该程序似乎运行得相当好,即使它真的需要一些代码/逻辑润色(我目前正在处理)

PS:我知道这远不是最好的解决方案,但我有点高兴,因为我自己做了一些研究(当然,如前所述,如果没有指针,我将无法编写代码)


PPS:再次感谢您:p

对于数字,让您的
表格模型
将表达式存储为
字符串
,并让您的
getValueAt()
实现返回表达式求值的结果。有几种方法可用。

实际上,对于
    contractFlaminiaTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"name1", null, null, null, null, null, null, null, null, null, null},
            {"name2", null, null, null, null, null, null, null, null, null, null},
            {"name3", null, null, null, null, null, null, null, null, null, null},
            {"name4", null, null, null, null, null, null, null, null, null, null},
            {"name5", null, null, null, null, null, null, null, null, null, null},
            {"name6", null, null, null, null, null, null, null, null, null, null},
            {"name7", null, null, null, null, null, null, null, null, null, null},
            {"name8", null, null, null, null, null, null, null, null, null, null},
            {"TOTAL", null, null, null, null, null, null, null, null, null, null}
        },
        new String [] {
            "  ", "col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8", "col9", "TOTAL"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class
        };
        boolean[] canEdit = new boolean [] {
            false, true, true, true, true, true, true, true, true, true, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    contractFlaminiaTable.setRowHeight(30);
    contractFlaminiaTable.getTableHeader().setReorderingAllowed(false);
    flaminiaScrollPane2.setViewportView(contractFlaminiaTable);
    contractFlaminiaTable.getColumnModel().getColumn(0).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(1).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(2).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(3).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(4).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(5).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(6).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(7).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(8).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(9).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(10).setResizable(false);
    contractFlaminiaTable.getColumnModel().getColumn(10).setPreferredWidth(48);
class TotalButton {

public TotalButton(JTable jTable) {
    // get table model
    TableModel model = jTable.getModel();

    // declare for variables
    int x;
    int y;

    // initialize table placeholder
    int totalValue = 0;

    int rowCount = model.getRowCount();
    int columnCount = model.getColumnCount();

    // for loop for the first row
    for (x = 0; x < rowCount; x++) {
        for (y = 1; y < columnCount; y++) {
            if (y == 10) {
                break;           // break when the loop reaches the total column
            }
            if (x == 0) {

                String value = (String) model.getValueAt(x, y);
                if ("".equals(value)) {
                    value = "0";            // avoiding empty string to integer error
                }
                int tempVal = Integer.parseInt(value);           // turn the string into a number

                totalValue = totalValue + tempVal;          // adds the numbers to the total
            }
        }

        String finalValue = Integer.toString(totalValue);           // turn the number to a string
        model.setValueAt(finalValue, 0, 10);            // set total column value
    }