Question

I am using the following TableCellRenderer in my JTable. When I click on JComboBox, I see a list of values as a single String (like this: [Text1, Text2]), but not as separate items. Where is the error?

    TableColumn columnComboBox = table.getColumnModel().getColumn(5);
        columnComboBox.setCellRenderer(getRendererComboBox());

    private TableCellRenderer getRendererComboBox() {
        return new TableCellRenderer() {

            private JComboBox<String> box = new JComboBox<String>();

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) 
            {   
                box.removeAllItems();
                for (String q : employees[row].getQualification())
                    box.addItem(q);
                box.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
                box.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
                return box;
            }
        };
    }
Was it helpful?

Solution

If you want a dropdown field in your table, you could do it like it is done here. Then, this will be the part where you create the JComboBox:

public void setUpSportColumn(JTable table,
                                 TableColumn sportColumn) {
        //Set up the editor for the sport cells.
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("Snowboarding");
        comboBox.addItem("Rowing");
        comboBox.addItem("Knitting");
        comboBox.addItem("Speed reading");
        comboBox.addItem("Pool");
        comboBox.addItem("None of the above");
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }

And if that is not what you want, but just the content of the dropdown-cell when you hover your mouse over the cell, and all the elements nice in a column, you just have to use html-tags in the string you are passing to setToolTipText-method. Like this:

 //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        String newLine = "<br>";
        String toolTipText = "<html>Snowboarding"+newLine+"Rowing"+newLine+"Knitting"+newLine+"Speed reading"+newLine+"Pool"+newLine+"None of the above</html>";
        renderer.setToolTipText(toolTipText);
        sportColumn.setCellRenderer(renderer);

I found that answer here: Multi-line tooltips in Java?

OTHER TIPS

public class tablet extends JFrame { /** * */ private static final long serialVersionUID = 1L;

public tablet() {

    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(new Object[][] { { "item1", "item2" } }, new Object[] { "A", "B" });
    table.setModel(model);
    table.setPreferredScrollableViewportSize(new Dimension(400, 250));
    table.setRowHeight(25);

    JScrollPane scrollPane = new JScrollPane(table);

    // These are the combobox values
    String[] values = new String[] { "item1", "item2", "item3" };

    // Set the combobox editor on the 1st visible column

    //combo editor
    JComboBox box = new JComboBox(values);
    TableColumn column1 = table.getColumnModel().getColumn(0);
    column1.setCellEditor(new DefaultCellEditor(box));

    //combo renderer
    column1.setCellRenderer(new MyComboBoxRenderer(values));

    getContentPane().add(scrollPane);
}

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public MyComboBoxRenderer(String[] items) {
        super(items);
    }

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

        // Select the current value
        setSelectedItem(value);
        return this;
    }
}

public static void main(String args[]) {
    tablet aTable = new tablet();
    aTable.pack();
    aTable.setVisible(true);
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top