且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 12:32:28

听起来有些行可能具有不同的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.