1

I want to add listbox for every row of a Jtable. and every listbox should be populated with different values that is retrieved from a file or a database. Here is the code for the same.

combo=new JComboBox(model1);
FileInputStream fstream= new FileInputStream("scbdata.txt");
DataInputStream in=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
str=br.readLine();
s = str.split(",");
for(int i=0;i<15;i++)   
model1.addElement(s[i]);
TableColumn col=m_table.getColumnModel().getColumn(3);
col.setCellEditor(new DefaultCellEditor(combo));`   

When I am using the above code the same combo box is being added to the entire rows of a column but i want to add combo box with different content in each row of a particular column. Is there any way out to add a ComboBox or something in a single cell?

Nidhi
  • 217
  • 1
  • 4
  • 14

1 Answers1

0

Check this

public class JTableWithComboBox {

    private static class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {

        public ComboBoxCellRenderer(Object[] items) {
            super(items);
        }

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

    protected void initUI() {
        JFrame frame = new JFrame("test");
        frame.add(getTable());
        frame.pack();
        frame.setVisible(true);
    }

    private Component getTable() {
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 1; i++) {
            Vector<String> row = new Vector<String>();
            for (int j = 0; j < 1; j++) {
                row.add("some value");
            }
            data.add(row);
        }
        Vector<String> columns = new Vector<String>();
        columns.add("Column 1");
        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        table.setRowHeight(20);
        int i = 0;
        Enumeration<TableColumn> c = table.getColumnModel().getColumns();
        String[] items = {"Item1", "Item2", "Item3"};
        JComboBox combo = new JComboBox(items);
        while (c.hasMoreElements()) {
            TableColumn column = c.nextElement();
            column.setCellRenderer(new ComboBoxCellRenderer(items));
            column.setCellEditor(new DefaultCellEditor(combo));
            combo.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        System.out.println(e.getItem() + " selected");
                    }
                }
            });
            i++;
        }
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scroll;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableWithComboBox().initUI();
            }
        });
    }
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • By using the TableCellRenderer and tablecelleditor the combobox with same items is being added to each row of a column..Is it possible to add combobox with different items to each row of a column?? – Nidhi Dec 04 '12 at 05:41
  • Can you tell me how can it be done? any example?. I have gone through many examples but not getting the correct solution. please help – Nidhi Dec 04 '12 at 06:36
  • sure what is your actual need ? – vels4j Dec 04 '12 at 07:39
  • I have a table, there are some columns in table. two columns has the combo box now what i want to do is,when user selects some value from column1 combobox, based on the user selected value column2 combobox should be populated. for example if user selects value1 from column1 combobox then column2 combobox will show values corresponding to value1 only. – Nidhi Dec 04 '12 at 08:22