Hello good day.
Here is my problem:

I have retrieved some a couple sets of data from my sql database and saved them in ArrayLists of (custom)types <Student> and <Subject>. I am using a single Table. I want to create a custom AbstractDataModel that displays the data from the ArrayList<Student>. The data that comes from ArrayList<Student> are simply Strings, and this constitutes half of the table. The other half of the table is populated by the user editing the cells via JComboBox editor. These choices are used to carry out calculations in order to populate the ArrayList<Subject>.

Problem is, all of this is just in my head. But I can't seem to get this coded. So far I have created the AbstractTableModel and added a TableCellEditor to a few columns so that they are edited via JComboBox's. But whenever I select a choice it doesn't display within the cell. My code seems to be clashing.

ArrayList<Student> applicants=new ArrayList<Student>(300);//Stores all information from the applicants
ArrayList<Subject> subjects=new ArrayList<Subject>(15);

String[] theSubs={"---","Biology","Additional Math","Geography","History","Spanish","Information Tech.","Technical Drawing","French","Physics","Chemistry","Principles of Accounts","Principles of Business"};
DefaultComboBoxModel subCBXmod=new DefaultComboBoxModel(theSubs);
JComboBox studentComboBox=new JComboBox(subCBXmod);
DefaultCellEditor editor=new DefaultCellEditor(studentComboBox);
...


/**
*An inner class
*/
public class TableDWModel extends AbstractTableModel{



    public TableDWModel()
    {
        fireTableDataChanged();
    }


    @Override
    public int getRowCount()
    {
        return applicants.size();//The student is really the subject of the Table. They determine how many rows there will be.
    }
    @Override
    public int getColumnCount()
    {
        return tableColumns.length;
    }
    @Override
    public String getColumnName(int col)
    {
        return tableColumns[col];
    }


    @Override   
    public Object getValueAt(int row, int col)
    {
        Student st=new Student();        

        st=applicants.get(row);
        switch(col)
        {
            case 0: 
                return (st.getFirstName()+" "+st.getLastName());
            case 1:
                return st.getFormClass().getForm();
            case 2:
                return st.getGender();
            case 3:
                return st.getShift();
            case 4:
                return st.getTotalSubjects();

            case 5: 
                    //Clueless!

        }

        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return (columnIndex >= 3);
}

And this is the design of my table: 5de4af102a87396ef8f3b5050283b723

So how can I accomplish sewing these pieces together please?

I'm sorry but I have read those tutorials, and the are not so explixit so as to make me competent enough to solve this niche question.

  • you have to override setValueAt with proper notifier

  • change current methods

  • nobody knows how Editor and Renderer is used in your code

  • whats wrong with code example from Oracle tutorial

Let me strip down my problem:

I have created a custom TableModel for my table. I have also used JComboBox as the editor for a number of the table's columns. The trouble I am having is that when I select an option from the Combobox's dropdown list, it does not display in the cell; the cell remains blank. Why is this please?

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.