JTable可以给表格的某一列加入控件,下面方法可以实现
try{
TableColumn column = jTable.getColumn(colName);
column.setCellEditor(new DefaultCellEditor(new JComboBox()));
}catch(IllegalArgumentException ex){
}
或者
TableColumn column = JTable.getColumnModel().getColumn(int column);
column.setCellEditor(new DefaultCellEditor(new JComboBox()));
TableColumn column = jTable.getColumn(colName);
column.setCellEditor(new DefaultCellEditor(new JComboBox()));
}catch(IllegalArgumentException ex){
}
或者
TableColumn column = JTable.getColumnModel().getColumn(int column);
column.setCellEditor(new DefaultCellEditor(new JComboBox()));
下面这个类可以帮助我们在表格的某一个指定的单元格实现下拉列表效果
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;
public class ComboBoxCellEditor extends JComboBox implements TableCellEditor{
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
public ComboBoxCellEditor() {
super();
addItem("日");
addItem("月");
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
fireEditingStopped();
}
});
}
public void addCellEditorListener(CellEditorListener listener) {
listenerList.add(CellEditorListener.class, listener);
}
public void removeCellEditorListener(CellEditorListener listener) {
listenerList.remove(CellEditorListener.class, listener);
}
protected void fireEditingStopped() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
}
public void cancelCellEditing() {
fireEditingCanceled();
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
public boolean isCellEditable(EventObject event) {
return true;
}
public boolean shouldSelectCell(EventObject event) {
return true;
}
public Object getCellEditorValue() {
return getSelectedItem();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
return this;
}
}
客户端调用上述代码,可以实现在表格第一行第二列的单元格内加入下拉列表
JTable table = new JTable(){
@Override
public TableCellEditor getCellEditor(int row, int column) {
// TODO Auto-generated method stub
if(row ==1 && column == 2){
return new ComboBoxCellEditor();
}
return super.getCellEditor(row, column);
}
};
@Override
public TableCellEditor getCellEditor(int row, int column) {
// TODO Auto-generated method stub
if(row ==1 && column == 2){
return new ComboBoxCellEditor();
}
return super.getCellEditor(row, column);
}
};