0

I am trying to make a row which will contain JCombobox in a class which extends AbstractTableModel.

I have a 2D array which is called data. I also use the method getColumnCount() in order to implement two buttons "Previous" and "Next". I have marked where I think the JCombobox should be, but I don't know how I should implement in the whole row.

public class SwitchTableModel extends AbstractTableModel{

    @Override
    public int getRowCount() {
        // Standard number of rows
        return 3;
    }

    @Override
    public int getColumnCount() {
        // Number of columns change according to the data
        return data [position].length + 1;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (rowIndex)
        {
        case 0:
            return columnIndex == 0 ? "ID" : idArray [data [position][columnIndex - 1]];
        case 1:
            return columnIndex == 0 ? "Company Name" : company_nameArray [data [position][columnIndex - 1]];
        case 2: 
            return columnIndex == 0 ? "Double" : ...???
        default:
            throw new Error ();
        }
    }

    public void previous ()
    {
        position -= 1;
        if (position < 0) position = data.length - 1;

        fireTableStructureChanged();
    }

    public void next ()
    {
        position += 1;
        if (position >= data.length) position = 0;

        fireTableStructureChanged();
    }
}

Any help will be valuable

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Dimitra Micha
  • 2,089
  • 8
  • 27
  • 31
  • have to re_wrote AbstracttableModel too, in my profile are code example including AutoComplete JComboBox as XxxTableCellEditor, use that for edit your question and to post an SSCCE, otherwise everything there are shorts to the dark – mKorbel Feb 14 '13 at 15:08
  • I am sorry, I couldn't find the code example:( – Dimitra Micha Feb 14 '13 at 15:34
  • Read the JTable API and then follow the link to the Swing tutorial titled "How to Use Tables" for an example of a table containing a combo box. – camickr Feb 14 '13 at 16:23
  • @Dimitra Micha `I am sorry, I couldn't find the code example`, sure without reading the tutorial is required to bothering little bit more [for example](http://stackoverflow.com/questions/tagged/java+swing+jtable+jcombobox) or [my posts](http://stackoverflow.com/search?q=user%3A714968+[jtable]+[jcombobox]), again without posting an SSCCE this question isn't answerable at all – mKorbel Feb 14 '13 at 16:56

1 Answers1

2

Adding a row of JCombobox in a AbstractTableModel,

  • this is wrong idea, XxxTableModel hold only last selected (or intialized on 1st use) value in String form (in the case that XxxComboBoxModel contains Item in String form, if is there Number, then model contains only one munber)

  • have to split JComboBox as Editor an XxxTableModel, then this idea works

  • do not put JComboBox to the XxxTableModel, follows code example from Oracle tutorial

mKorbel
  • 109,525
  • 20
  • 134
  • 319