...
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellRenderer(new MyComboBoxRenderer(values));
...
public 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());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
/*
* ComboDemo.java
*/
package table;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class ComboDemo extends JFrame {
private JTable table;
private DefaultTableModel model;
public ComboDemo() {
setSize(400,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
table = new JTable();
model = (DefaultTableModel)table.getModel();
model.addColumn("A", new Object[]{"item1"});
model.addColumn("B", new Object[]{"item2"});
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));
add(new JScrollPane(table));
}
public static void main(final String args[]) {new ComboDemo().setVisible(true);}
}
class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(final String[] items) {
super(items);
}
public Component getTableCellRendererComponent(final JTable table, final Object value,
final boolean isSelected, final boolean hasFocus, final int row, final int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(final String[] items) {
super(new JComboBox(items));
}
}
setValueAt(int, int)
Tolpan hat gesagt.:..wie ich per methode Werte in das entsprechende Feld schreiben kann und diese dann auch in der Combobox angezeigt werden..
TableCellEditor ed = table.getCellEditor(0, 0);
JComboBox box = (JComboBox) ed.getTableCellEditorComponent(table, ed.getCellEditorValue(), true, 0, 0);
String[] items = {"a", "b", "c", "d", "e"};
box.setModel(new DefaultComboBoxModel(items));
TableCellRenderer renderer = table.getCellRenderer(0, 0);
box = (JComboBox)renderer.getTableCellRendererComponent(table, table.getValueAt(0, 0), true, true, 0, 0);
box.setModel(new DefaultComboBoxModel(items));
table.updateUI();
public void tableChanged(TableModelEvent e)
{
System.out.println(((JComboBox)table.getCellRenderer(table.getSelectedRow(), table.getSelectedColumn())).getSelectedItem());
}
In actionPerformed vom ActionListener kommst du mit e.getSource() an die Komponente, die die AktionK-Man hat gesagt.:..Wie kann ich da aber auf die JComboBox zugreifen oder auf den Button wie in diesem Beispiel?
Mit getCellRenderer komm ich ja auf JComponentCellRenderer, kann da aber nicht auf JComboBox bzw JButton casten...
for (int row = 0; row < table.getRowCount(); row++) {
for (int column = 0; column < table.getColumnCount(); column++) {
Object value = table.getValueAt(row, column);
if(value instanceof JComboBox){
JComboBox box = (JComboBox)value;
String item = box.getSelectedItem().toString();
System.out.println("Value of JComboBox: "+item);
}
}
}