使用ComboBox更新JTable中的特定单元格 [英] Updating a specific cell in a JTable using a ComboBox

查看:113
本文介绍了使用ComboBox更新JTable中的特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DOM分析器读取数据以更新JTable.我有一个不一定要位于XML中的列(ValidValues).

但是,如果从XML读取时位于此标记中,那么我将获取该值并将其用于SQL查询以返回可用记录的向量.

然后,我希望用一个特定的组合框填充JTable,该组合框包含在读取标签的正确行上返回的值.例如,在从XML文档中读取第17行之前,我可能不会读取标签.

我已经用相同的代码完成了两个相似的JCombo框,但是它们保持不变,因此它们没有问题.

由于在单元格之间会发生这种变化,我不确定如何进行操作,我浏览了Oracle教程,但它们似乎仅演示了如何更改一列.进一步的研究也没有发现与该领域有关的东西.

通过向量更新常量JComboBox的代码:

        propColumn = table.getColumnModel().getColumn(ENV_PROPERTIES_COLUMN);
        propComboBox = new JComboBox();
        propComboBox.addItem("");
        constructEnvProperties();
        propColumn.setCellEditor(new DefaultCellEditor(propComboBox));

public void constructEnvProperties(){

    IWM781EnvProfilePropertiesCtl ctl = new IWM781EnvProfilePropertiesCtl();

    Vector<IWM781EnvProfileProperties> recordSet = ctl.getRecordSet("TestEnvXXX", con);

    for(int i = 0; i < recordSet.size(); i++){
        logger.debug(recordSet.get(i).getProp781Property());
        propComboBox.addItem(recordSet.get(i).getProp781Property());    
    }
}

尝试使用变体组合框:

if(tableEntryElement.getElementsByTagName("ValidValues").item(0) != null){

     // Build combo box based on <SystemCode> tag
    logger.debug(tableEntryElement.getElementsByTagName("ValidValues").item(0).getTextContent());

        TableColumn optionColumn = table.getColumnModel().getColumn(OPTION_COLUMN);

        JComboBox optionComboBox = new JComboBox();
        optionComboBox.addItem("");
        constructOptions(tableEntryElement);
        optionColumn.setCellEditor(new DefaultCellEditor(optionComboBox));  
    }

我知道这里的问题将是:

     TableColumn optionColumn =  table.getColumnModel().getColumn(OPTION_COLUMN);

,因为它引用了整个专栏,但是任何想法都将不胜感激.

我还简要阅读了TableColumn的API,我仍然在其中,以了解是否可以找到一种方法来引用列的行.

预先感谢

解决方案

听起来有些行可能具有不同的JComboBox值.您也许可以利用 TableComboBoxByRow 中显示的方法,该方法将覆盖getCellEditor()以提供所需的编辑器用于某些行.

I am reading in data using DOM Parser to update a JTable. I have a Column (ValidValues) that may not necessarily be located in the XML.

However, if this tag is located when reading in from the XML, I take the value and use this to from an SQL query to return a vector of the records available.

I then wish to populate the JTable with a specific combo box of the values returned on the correct row that the tag was read. E.G I may not read a tag until the 17th row has been read in from the XML document.

I have already completed two similar JCombo boxes in the same code but they remain constant, so there is no issue with them.

As this changes between cells I'm unsure of how to proceed, I looked through Oracle tutorials but they only seem to demonstrate how one column can be changed. Further research has found nothing relating to this area either.

Code for constant JComboBox updated through vector:

        propColumn = table.getColumnModel().getColumn(ENV_PROPERTIES_COLUMN);
        propComboBox = new JComboBox();
        propComboBox.addItem("");
        constructEnvProperties();
        propColumn.setCellEditor(new DefaultCellEditor(propComboBox));

public void constructEnvProperties(){

    IWM781EnvProfilePropertiesCtl ctl = new IWM781EnvProfilePropertiesCtl();

    Vector<IWM781EnvProfileProperties> recordSet = ctl.getRecordSet("TestEnvXXX", con);

    for(int i = 0; i < recordSet.size(); i++){
        logger.debug(recordSet.get(i).getProp781Property());
        propComboBox.addItem(recordSet.get(i).getProp781Property());    
    }
}

Attempt at a variant combo box:

if(tableEntryElement.getElementsByTagName("ValidValues").item(0) != null){

     // Build combo box based on <SystemCode> tag
    logger.debug(tableEntryElement.getElementsByTagName("ValidValues").item(0).getTextContent());

        TableColumn optionColumn = table.getColumnModel().getColumn(OPTION_COLUMN);

        JComboBox optionComboBox = new JComboBox();
        optionComboBox.addItem("");
        constructOptions(tableEntryElement);
        optionColumn.setCellEditor(new DefaultCellEditor(optionComboBox));  
    }

I know the issue here will be:

     TableColumn optionColumn =  table.getColumnModel().getColumn(OPTION_COLUMN);

as it's referencing the entire column, but any ideas would be greatly appreciated.

I've also briefly read the API for TableColumn which I'm still in the middle of to see if I can find a way to reference the row of the column.

Thanks in advance

解决方案

It sounds like some rows may have different JComboBox values. You may be able to leverage the approach shown in TableComboBoxByRow, which overrides getCellEditor() to supply the desired editor for certain rows.

这篇关于使用ComboBox更新JTable中的特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆