Using a JComboBox in a Cell in a JTable Component : Table Renderer Editor « Swing JFC « Java






Using a JComboBox in a Cell in a JTable Component

 


import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    DefaultTableModel model = (DefaultTableModel) table.getModel();

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };

    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyComboBoxEditor(values));
    col.setCellRenderer(new MyComboBoxRenderer(values));
  }
}

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
  public MyComboBoxRenderer(String[] items) {
    super(items);
  }

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

class MyComboBoxEditor extends DefaultCellEditor {
  public MyComboBoxEditor(String[] items) {
    super(new JComboBox(items));
  }
}

   
  








Related examples in the same category

1.Shading Rows and Columns in a JTable Component
2.Shades every other column yellow
3.Using Built-In Cell Renderers and Editors in a JTable Component
4.Get Default cell Renderer
5.Get Default Cell Editor
6.Creating a Custom Cell Renderer in a JTable Component
7.Creating a Class-Based Custom Cell Renderer in a JTable Component
8.A slider renderer for volume values in a tableA slider renderer for volume values in a table
9.Table Cell Editor: ComboBoxCellEditor
10.Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
11.Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
12.StockTable 3: CellRendererStockTable 3: CellRenderer
13.Colored Table CellRendererColored Table CellRenderer
14.Change Table cell background with column renderer Change Table cell background with column renderer
15.Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
16.A table that allows the user to pick a color from a pulldown listA table that allows the user to pick a color from a pulldown list
17.MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
18.Table Editor: Editable Color ColumnTable Editor: Editable Color Column
19.Tab in and out JComboBox in JTable
20.Creating a Custom Table Cell Editor in a JTable Component
21.Preventing Invalid Values in a Cell in a JTable Component
22.Setting the Activation Click Count for a Table Cell Editor in a JTable Component
23.Using a List JSpinner as a Cell Editor in a JTable Component
24.Rendering an image in a JTable column
25.This program demonstrates cell rendering and editing in a table.This program demonstrates cell rendering and editing in a table.