Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JComboBox Table Cell Renderer selected values

800458Jul 7 2009 — edited Jul 8 2009
Hi,
I have written a combobox cell editor and renderer for a jtable cell, which actually works.
But when i select en entry in the box, the value is not populated to the table cell. Can anyone guess why?

Thank you for any suggestions!

Regards,

Andreas
public class CellRendererWithMPComboBox extends mpv5.ui.beans.LightMPComboBox implements TableCellRenderer {

    private final Context c;
    private final JTable table;

    /**
     * Create a new CellRenderer which holds a MPComboBox. Will not assign itself to any column.
     * @param c
     * @param table
     */
    public CellRendererWithMPComboBox(Context c, JTable table) {
        this.c = c;
        this.table = table;
    }

    /**
     * Set this renderer to the given column
     * @param column
     */
    public void setRendererTo(int column) {
        TableColumn col = table.getColumnModel().getColumn(column);
        col.setCellEditor(new MPComboBoxEditor(c));
        col.setCellRenderer(this);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (hasFocus) {
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                super.setBackground(table.getSelectionBackground());
            } else {
                setForeground(table.getForeground());
                setBackground(table.getBackground());
            }

//            Select the current value
            try {
//                getComboBox().setSelectedIndex(1);
                  setSelectedItem(((MPComboBoxModelItem)value).getValue());

            } catch (Exception e) {
                Log.Debug(e);
            }
            return this;

        } else {
            return new DefaultCellRenderer();
        }
    }

    class MPComboBoxEditor extends DefaultCellEditor {

        private final Context c;

        public MPComboBoxEditor(Context c) {
            super(new mpv5.ui.beans.MPCombobox(c, table).getComboBox());
            this.c = c;
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 5 2009
Added on Jul 7 2009
8 comments
280 views