import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Main implements ActionListener {
private MyModel1 model1;
private MyModel2 model2;
private JTable table;
public Main() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(10, 10, 500, 500);
table = new JTable();
model1 = new MyModel1();
model2 = new MyModel2();
table.setModel(model1);
String[] values = new String[]{"item1", "item2", "item3"};
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values)); // If the cell should appear like a combobox in its non-editing state, also set the combobox renderer
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400, 400));
p.setLayout(new BorderLayout());
JPanel controlPanel = new JPanel();
controlPanel.setPreferredSize(new Dimension(400, 200));
JButton b = new JButton("Model1");
b.setActionCommand("model1");
b.setPreferredSize(new Dimension(140, 40));
b.addActionListener(this);
JButton b1 = new JButton("Model2");
b1.setActionCommand("model2");
b1.addActionListener(this);
b1.setPreferredSize(new Dimension(140, 40));
controlPanel.add(b);
controlPanel.add(b1);
p.add(controlPanel,BorderLayout.SOUTH);
p.add(table, BorderLayout.CENTER);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("model1")) {
table.setModel(model1);
}
else if(e.getActionCommand().equals("model2")) {
table.setModel(model2);
}
}
}
class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
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 MyModel1 extends DefaultTableModel {
public MyModel1() {
this.addColumn("A", new Object[]{"item1"});
this.addColumn("B", new Object[]{"item2"});
}
}
class MyModel2 extends DefaultTableModel {
public MyModel2() {
this.addColumn("A", new Object[]{"item1"});
this.addColumn("B", new Object[]{"item2"});
this.addColumn("A", new Object[]{"item3"});
this.addColumn("B", new Object[]{"item4"});
this.addColumn("A", new Object[]{"item5"});
this.addColumn("B", new Object[]{"item6"});
}
}