添加Component后JTable为空

发布于 2024-11-07 15:06:00 字数 948 浏览 0 评论 0原文

我的 JTable 有问题,我试图放入每一行 JComboBoxes 和 JTextFields,但是当我加载应用程序时,我的表是空的。函数 getCmb* 和 getTxt* 返回 JComboBox 和 JTextField 并且工作正常,我检查过。

        JTable tblCommands;
        String[] columnTitles=new String[]{"Command","Offset","Type","Value","Units","Value Type","R/W"};
        Object[][] data=new Object[20][7];
        int row=0;
        for(MessageCSVView message:messageContainer.getRows()){
            data[row][0]=message.getCmbName();//this works 
            data[row][1]=message.getCmbOffset();//this works 
            data[row][2]=message.getTxtType();//this works 
            data[row][3]=message.getTxtValue();//this works 
            data[row][4]=message.getTxtUnit();//this works 

            data[row][5]=message.getTxtValueType();//this works 
            data[row][6]=message.getCmbRW();//this works 
            row++;
        }
        tblCommands=new JTable(data,columnTitles);

谁能告诉我我做错了什么?

I have problem with JTable, I am trying to put in each row JComboBoxes and JTextFields but when I load app, my table is empty. Functions getCmb* and getTxt* return JComboBox and JTextField and that works fine, I checked.

        JTable tblCommands;
        String[] columnTitles=new String[]{"Command","Offset","Type","Value","Units","Value Type","R/W"};
        Object[][] data=new Object[20][7];
        int row=0;
        for(MessageCSVView message:messageContainer.getRows()){
            data[row][0]=message.getCmbName();//this works 
            data[row][1]=message.getCmbOffset();//this works 
            data[row][2]=message.getTxtType();//this works 
            data[row][3]=message.getTxtValue();//this works 
            data[row][4]=message.getTxtUnit();//this works 

            data[row][5]=message.getTxtValueType();//this works 
            data[row][6]=message.getCmbRW();//this works 
            row++;
        }
        tblCommands=new JTable(data,columnTitles);

Can anybody tell me what I did wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-11-14 15:06:00

您误解了 JTable 输入的方式。

您需要创建 TableCellEditor 实现并将它们添加到表格的每一列。

请参阅 JTable 上的 Swing 教程以获取更多信息。

You misunderstand the way input works with JTable.

You'll need to create TableCellEditor implementations and add them to each column of your table.

Take a look at Swing tutorial on JTable for further information.

美人迟暮 2024-11-14 15:06:00

示例片段

public class JComboBoxCellEditor extends DefaultCellEditor {    
    JComboBox comboBox;    
    public JComboBoxCellEditor() {
        super(new JComboBox());  
        comboBox = (JComboBox) getComponent();
    }
}

然后将其包含如下,

TableColumn column = myTable.getColumnModel().getColumn(0);
column.setCellEditor(new JComboBoxCellEditor());

进一步阅读:

这是您最好的选择,JTable 的 Swing 教程

Example Snippet

public class JComboBoxCellEditor extends DefaultCellEditor {    
    JComboBox comboBox;    
    public JComboBoxCellEditor() {
        super(new JComboBox());  
        comboBox = (JComboBox) getComponent();
    }
}

Then include it like below,

TableColumn column = myTable.getColumnModel().getColumn(0);
column.setCellEditor(new JComboBoxCellEditor());

Further reading:

Here is your best bet, Swing tutorial for JTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文