0

I ahve a JTable that is supposed to be 2 columns (String, JComboBox). When i initialize the table everything looks good. As soon as a I select a value in the table the JComboBox cell aquires the data type of the selected item.

I want to keep the JCOmboBox there and have it fire the events of data change and the Table ignore data changes in that column and keep the ComboBox populated.

My table has this as an override

@Override
public TableCellEditor getCellEditor(int row, int column) {
    Object value = super.getValueAt(row, column);
    if (value != null) {
        if (value instanceof JComboBox) {
            return new DefaultCellEditor((JComboBox) value);
        }
        return getDefaultEditor(value.getClass());
    }
    return super.getCellEditor(row, column);
}

Implementation

    JComboBox uploadBox = new JComboBox();
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.PROMPT);
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.UPLOAD);
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.DONT_UPLOAD);

    Object[][] tableData = new Object[][]{
        {"Upload data on save", uploadBox}
    };



    table.setModel(
            new DefaultTableModel(tableData, new String[]{"Description", "Options"}) {
        Class[] types = new Class[]{String.class, JComboBox.class};
        boolean[] canEdit = new boolean[]{false, true};

        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }


    });

    table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
            return (Component)o;
        }
    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
meriley
  • 1,831
  • 3
  • 20
  • 33

1 Answers1

3
  • answer is quite simple don't put JComboBox to the XxxTableModel or to set getColumClass for JComboBox.class, this is wrong (sure is possible but with bunch of side effects), XxxTableModel (is designated for) can hold directly only standard Java data types (String, Date, Icon/ImageIcon, Integer, Double etc... )

  • XxxTableModel should be store (if you don't want to parsing between Java data types) the same data type like as is stored in DefaultComboBoxModel (noting clear what constans are MyPanel.XXX), e.g in XxxTableModel is stored String value when DefaultComboBoxModel has the same data types, similair logics for Date, Icon/ImageIcon, Integer or Double

  • for more info to read Oracle tutorial How to use Tables - Using a Combo Box as an Editor

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @meriley I have here a few posts with code in SSCCE form, tagged by java + swing + jtable + jcombobox – mKorbel Dec 13 '13 at 19:59
  • My issue is that each row has a different JComboBox (they store different Data) while the provided example is a unified set of data for all rows. – meriley Dec 13 '13 at 20:42
  • @meriley each row has a different JComboBox - this isn't any issue, there are three ways how to hold different value for separated DefaultComboBoxModel (not required to define is created always), but any from them is about to put JComboBox to model, renderer is designated for columns and rows coordinates, or to store in Map or to strore in Vector, – mKorbel Dec 13 '13 at 20:51
  • @meriley you need to accept that model is designated to hold only initial (or last selected) value for JComboBox as cell editor (painted in JTables view by TableCellRenderer), TableCellEditor is about temporary JComponents, that to store vlaue to model and then renderer is about to paint value from model to JTable – mKorbel Dec 13 '13 at 20:54
  • +1 for distinguishing models; @meriley: [`TableComboBoxByRow`](http://stackoverflow.com/a/3256602/230513) may be of interest. – trashgod Dec 14 '13 at 01:15