1

I am rendering a JTable with certain cells being a normal text input or a ComboBox.

When I try it like this:

package sandbox.jtablegui;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.*;
import javax.swing.table.*;

public class App extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public App()
    {
        String[] colNames = { "Name", "Value" };
        final String[] propertyNames = { "Val1", "Val2" };
        DefaultTableModel model = new DefaultTableModel(colNames, 2) {

            // set setting names
            public Object getValueAt(int row, int col) {
                if (col == 0)
                    return propertyNames[row];
                return super.getValueAt(row, col);
            }

            public boolean isCellEditable(int row, int col) {
                if (col == 0)
                    return false;
                return true;
            }
        };

        JComboBox<String> comboBox = new JComboBox<String>(new String[] { "Option1", "Option2" });
        final DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox)    {
            @Override
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                Object val = value;
                System.out.println(val);
                if (val == null) {
                    val = ((JComboBox<?>) getComponent()).getItemAt(0);
                    System.out.println(val);
                }
                return super.getTableCellEditorComponent(table, val, isSelected, row, column);
            }
        };

        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row == 1)
                    return comboBoxEditor;
                else
                    return super.getCellEditor(row, column);
            }
        };
        RowEditorModel rowEditorModel = new RowEditorModel();



        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        App frame = new App();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

    public class RowEditorModel {
        private Hashtable data;

        public RowEditorModel() {
            data = new Hashtable();
        }

        public void addEditorForRow(int row, TableCellEditor editor) {
            data.put(new Integer(row), editor);
        }

        public void removeEditorForRow(int row) {
            data.remove(new Integer(row));
        }

        public TableCellEditor getEditor(int row) {
            return (TableCellEditor) data.get(new Integer(row));
        }
    }
}

However, the default value is not shown in the JTable as you can see from the screenshot.

enter image description here

poseid
  • 6,986
  • 10
  • 48
  • 78
  • 1
    please whats problem is following officia Oracle tutorial, seems like as RowEditorModel isn't completed, there are bunch of better ways (to storing a value to the XxxTableModel) – mKorbel Apr 29 '16 at 14:34
  • yes, discovering now that the tablemodel should manage the values e.g. http://stackoverflow.com/questions/11858286/how-to-use-custom-jtable-cell-editor-and-cell-renderer ? – poseid May 02 '16 at 14:34

1 Answers1

2

Setting of selected index on combobox creation cannot help. You need to set it each time when it uses as editor. Something like this:

DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox) {
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        Object val = value;
        if (val == null) {
            val = ((JComboBox<?>) getComponent()).getItemAt(0);
        }
        return super.getTableCellEditorComponent(table, val, isSelected, row, column);
    }

}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • thanks, hmm.. when is getTableCellEditorComponent called? I try to see the selected optoin with System.out.println(val); but there is no sign the function is called ... – poseid Apr 29 '16 at 10:04
  • This method is called by JTable each time when table prepares editor to show. If my solution has no effect, please provide a [SSCCE](http://sscce.org), so I can see what's wrong. – Sergiy Medvynskyy Apr 29 '16 at 10:14
  • 1
    @poseid editor is shown only when an editable cell of a table is clicked by the user. If you want to see it in table, you need to return it in `getValueAt()` method of your model. – Sergiy Medvynskyy Apr 29 '16 at 11:55
  • @poseid please also read about [editor/renderer concept in Swing](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) – Sergiy Medvynskyy Apr 29 '16 at 12:22
  • thanks, ok, I must set the default value via the JTable. I thought the JComboBox default value would be responsible for that. but it makes sense via getValueAt – poseid Apr 29 '16 at 14:15