简体   繁体   English

以编程方式编辑JTable值

[英]Editing JTable Values Programmatically

I'm currently working on a sort-of IDE for Lua, and I'm currently making the New Project wizard. 我目前正在为Lua开发某种IDE,并且正在制作“新建项目”向导。

Once the user has input the project details, I'd like them to be shown to the user as a confirmation. 用户输入项目详细信息后,我希望将其显示给用户作为确认。 This information should be displayed in a JTable, to make the UI cleaner. 此信息应显示在JTable中,以使UI更加整洁。

Project Details: 项目详情:
在此处输入图片说明

Detail Confirmation: 详细确认:
在此处输入图片说明

I'm still working on the UI, but I'd like to have this stuff finished, before I start working on the actual syntax-highlighting and the editor in general. 我仍在使用UI,但是在开始实际的语法高亮显示和一般的编辑器工作之前,我想先完成这些工作。

At the moment, this is the code I'm trying to get to work (and failing miserably): 目前,这是我正在尝试使用的代码(并且失败了):

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

How can I edit the table contents using this or similar code? 如何使用此或类似代码编辑表内容?

Solution by OP: OP解决方案:

I had a look in the constructor and after a few minutes of searching, I found what i was looking for. 我看了一下构造函数,经过几分钟的搜索,我找到了想要的东西。 Just in case anybody else needs this: 万一其他人需要此:

// 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];
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM