【问题标题】:Editing JTable Values Programmatically以编程方式编辑 JTable 值
【发布时间】:2014-01-16 21:08:16
【问题描述】:

我目前正在开发一种用于 Lua 的 IDE,并且正在制作新项目向导。

一旦用户输入了项目详细信息,我希望将它们显示给用户作为确认。此信息应显示在 JTable 中,以使 UI 更简洁。

项目详情:

详细确认:

我仍在处理 UI,但我想先完成这些工作,然后再开始处理实际的语法高亮和一般编辑器。

目前,这是我试图开始工作的代码(并且失败得很惨):

if (jTabbedPane2.getSelectedIndex() == 1) {
        jLabel2.setIcon(_wizard3);
        // Enter values into table
        jTable1.setValueAt(jTextField1.getText(), 1, 1);
        jTabbedPane2.setSelectedIndex(2);
        return;
    }

如何使用此代码或类似代码编辑表格内容?

【问题讨论】:

  • and failing miserably - 也没有提出问题。我不知道代码片段代表什么。您的问题标题的简单答案是,是的,您将使用 setValueAt(...) 方法以编程方式更新 TableModel。
  • 啊,是的。对不起。我有点着急。
  • 您的编辑是答案吗?如果是这样,您应该发布自己问题的答案,以明确区分问题和答案。

标签: java swing ide lua jtable


【解决方案1】:

OP 的解决方案:

我查看了构造函数,经过几分钟的搜索,我找到了我要找的东西。 以防万一其他人需要这个:

// Set the new model
jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            // This is where your (new) values go.
            {jTextField1.getText(), jTextField2.getText()}
        },
        new String [] {
            // These are the headers for each column.
            "Project Language", "Project Type"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            // Set these for each column. 
            // If false, the end-user cannot edit the contents
            // If true, the end-user is free to play with the data inside.
            false, false
        };

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

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多