本篇文章给大家分享的是有关在JTable单元格中如何实现可选择的JComboBox,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
在JTable单元格中实现可选择的JComboBox,经典例子源码(如设置性别)[@more@]性别的例子,使用方法如下:
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(genderColumn);// genderColumn为你的性别的例序号。
tc.setCellRenderer(new GenderRenderer());
tc.setCellEditor(new GenderEditor());
限制如下,该列类型为boolean。
import java.awt.Component;
import java.util.EventObject;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class GenderEditor extends JComboBox implements TableCellEditor {
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
public GenderEditor() {
super();
addItem("Male");
addItem("Female");
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 new Boolean(getSelectedIndex() == 0 ? true : false);
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
boolean isMale = ((Boolean)value).booleanValue();
setSelectedIndex(isMale ? 0 : 1);
return this;
}
}
import java.awt.Component;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class GenderRenderer extends JComboBox implements TableCellRenderer {
public GenderRenderer() {
super();
addItem("Male");
addItem("Female");
}
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());
}
boolean isMale = ((Boolean)value).booleanValue();
setSelectedIndex(isMale ? 0 : 1);
return this;
}
}
以上就是在JTable单元格中如何实现可选择的JComboBox,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。