Dinesh
Dinesh

Reputation: 16428

Dynamic jcombobox rendering inside jtable

I have a JTable with JCheckboxes in 2 columns of the JTable. Lets say column A and Column B are the ones with JComboBox in it. The items of Column A's JCheckbox are static which can be hard-coded. But, the items of Column B's JComboBox values are Dynamic. What I wanted to do is, when the user launched the application, the column A's JcomboBox should have the static values and it should display the default selected value in the ComboBox as "---Select---"

i.e. I will set the combox items as ["--Select","X","Y","Z"]

Also, in the column B's JComboBox should display the default value as "Please Wait", which means, after some time it is going to be getting updated dynamically. Until that time, it has to show "Please Wait".

After some set of user actions, I want to update the Column B's JComboBox with the dynamic values. Even in that also, the column B's JcomboBox values are dependent on Column A's JcomboBox selected Item.

I can able to update the values dynamically, but the rendering of that Cell as JComboBox is not working as expected.

I have tried the following,

Enumeration<TableColumn> c = myTable.getColumnModel().getColumns();
        int i = 0;
        while (c.hasMoreElements()) {
            TableColumn column = c.nextElement();
            column.setCellRenderer(new ComboBoxCellRenderer(i));
            i++;
        }



   private static class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {

        public ComboBoxCellRenderer(int column) {
            // for (int i = 0; i < elements.length; i++) {
            if (column == 0) {

                addItem("--Select--");
                addItem("X");
                addItem("Y");
                addItem("Z");

            }
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setSelectedItem(value);
            return this;
        }
    }

If I use this code, then I am unable to select the items from the JComboBo. Even if I select, it is not getting selected.

I am currently using the code mentioned in the link below.

Stackoverflow

It would be much helpful to me, if someone give an example code for this.

Thanks in advance.

Upvotes: 1

Views: 1189

Answers (1)

trashgod
trashgod

Reputation: 205785

DependentColumn may offer some guidance. If the dependent column is also editable, see these related examples.

image

Upvotes: 2

Related Questions