您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關在JTable單元格中如何實現可選擇的JComboBox,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
在JTable單元格中實現可選擇的JComboBox,經典例子源碼(如設置性別)[@more@]性別的例子,使用方法如下:
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(genderColumn);// genderColumn為你的性別的例序號。
tc.setCellRenderer(new GenderRenderer());
tc.setCellEditor(new GenderEditor());
限制如下,該列類型為boolean。
/**
* GenderEditor.java
*/
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;
}
}
/**
* GenderRender.java
*/
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,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。