1

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nathan
  • 266
  • 4
  • 16

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Brilliant, thanks very much for your help, overriding the getCellEditor provides more freedom to specifically define how you want cells to be set – Nathan Nov 29 '13 at 12:48
  • Excellent; as you are using a DOM parser, consider containing the `Document` in a concrete implementation of `AbstractTableModel`. – trashgod Nov 29 '13 at 14:39
  • Forgive me but what do you mean by that ? I have implemented my own custom table model that extends AbstratDataModel and use this with my JTable, but do not reference it to Document at any point – Nathan Nov 29 '13 at 15:40
  • I was thinking of something like [this](http://stackoverflow.com/a/9134371/230513), in which the `TableModel` accesses a private data structure to fulfill its contract. – trashgod Nov 29 '13 at 16:08
  • I've completed the project itself so may go back to amending it and try to implement it in this way – Nathan Nov 29 '13 at 16:46