简体   繁体   English

使用ComboBox更新JTable中的特定单元格

[英]Updating a specific cell in a JTable using a ComboBox

I am reading in data using DOM Parser to update a JTable . 我正在使用DOM解析器读取数据以更新JTable I have a Column (ValidValues) that may not necessarily be located in the XML. 我有一个不一定位于XML中的列(ValidValues)。

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. 但是,如果从XML读取时位于此标记中,那么我将获取值并将其用于SQL查询以返回可用记录的向量。

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. 然后,我希望用一个特定的组合框填充JTable,该组合框包含在读取标签的正确行上返回的值。 EG I may not read a tag until the 17th row has been read in from the XML document. EG I可能直到从XML文档中读取第17行才读取标签。

I have already completed two similar JCombo boxes in the same code but they remain constant, so there is no issue with them. 我已经用相同的代码完成了两个类似的JCombo框,但是它们保持不变,因此它们没有问题。

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. 由于不确定单元格之间的变化,我不确定如何进行操作,我浏览了Oracle教程,但它们似乎仅演示了如何更改一列。 Further research has found nothing relating to this area either. 进一步的研究也没有发现与此领域有关的任何东西。

Code for constant JComboBox updated through vector: 通过向量更新常量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());    
    }
}

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. 我还简要阅读了TableColumn的API,我仍然在其中,以查看是否可以找到一种方法来引用列的行。

Thanks in advance 提前致谢

It sounds like some rows may have different JComboBox values. 听起来有些行可能具有不同的JComboBox值。 You may be able to leverage the approach shown in TableComboBoxByRow , which overrides getCellEditor() to supply the desired editor for certain rows. 您可能可以利用TableComboBoxByRow显示的方法,该方法将覆盖getCellEditor()以为某些行提供所需的编辑器

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

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