xpanta
xpanta

Reputation: 8418

JTable: How to make a JComboBox cell renderer bigger than the rendered cell?

I am using this JTable method to create a Cell with a JComboBox as their rendered appearance.

public void addComboBoxToColumn(String[] options, int column_index){
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox<String> combo = new JComboBox<String>(options);
    TableCellEditor combo_editor  = new DefaultCellEditor(combo);
    TableColumn column = getColumnModel().getColumn(column_index);
    column.setCellRenderer(renderer);
    column.setCellEditor(combo_editor);     
}

...

public class ComboTableCellRenderer implements ListCellRenderer, TableCellRenderer 
{
    DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();
    DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();

    private void configureRenderer(JLabel renderer, Object value)
    {
        if (value != null)
            renderer.setText((String)value);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        listRenderer = (DefaultListCellRenderer)listRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        configureRenderer(listRenderer, value);
        return listRenderer;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        tableRenderer = (DefaultTableCellRenderer)tableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        configureRenderer(tableRenderer, value);
        return tableRenderer;
    }

}

My problem is that the combobox is always the size of the cell. I do not want that. Is it possible to make the combo box bigger? Some options in the combobox are too big and are half-hidden.

Upvotes: 1

Views: 2161

Answers (2)

mKorbel
mKorbel

Reputation: 109813

My problem is that the combobox is always the size of the cell. I do not want that. Is it possible to make the combo box bigger? Some options in the combobox are too big and are half-hidden.

  • not possible without jumping of cell Dimmension on the Screen, don't to confuse the user

  • to avoiding possible side effects, I'd be

  • create popup undecorated JDialog (for editable JComboBox), JWindow, put there JComboBox

  • add ListSelectionListener (have to change ListSelectionMode to SINGLE)

  • change built in KeyBinding in JTable for TableCellEditor (double_click or F2) to showing JDialog/JWindow have to center to the desired Point on the scren, setVisible must be wrapped in invokeLater

  • add ItemListener, test for SELECTED, on selected to store value to (setValueAt()) XxxTableModel, then to hide JDialog/JWindow

  • use only one JDialog (reuse by removeAll from content pane for another action from GUI) for whole JVM instance, only one for JTable

Upvotes: 1

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

Override the JTable.editCellAt. This is the method that positions the table cell editor by calling setBounds on it. Just set the bounds differently in your preferred way, maybe should span over several columns.

Upvotes: 1

Related Questions