JTableのCellEditorにArrowButtonを非表示にしたJComboBoxを設定
Total: 3760
, Today: 1
, Yesterday: 1
Posted by aterai at
Last-modified:
概要
JTable
のCellEditor
としてArrowButton
を非表示にしたJComboBox
を使用します。
Screenshot
Advertisement
サンプルコード
class LocalDateTimeTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
private final JComboBox<LocalDateTime> comboBox;
private LocalDateTime selectedDate;
public LocalDateTimeTableCellEditor() {
super();
UIManager.put("ComboBox.squareButton", Boolean.FALSE);
comboBox = new JComboBox<LocalDateTime>() {
@Override public void updateUI() {
super.updateUI();
setBorder(BorderFactory.createEmptyBorder());
setOpaque(true);
setRenderer(new LocalDateTimeCellRenderer<LocalDateTime>());
setUI(new BasicComboBoxUI() {
@Override protected JButton createArrowButton() {
JButton button = new JButton(); //.createArrowButton();
button.setBorder(BorderFactory.createEmptyBorder());
button.setVisible(false);
return button;
}
});
}
};
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
}
@Override public Component getTableCellEditorComponent(
JTable table, final Object value, boolean isSelected, int row, int column) {
if (value instanceof LocalDateTime) {
comboBox.setModel(new DefaultComboBoxModel<LocalDateTime>() {
@Override public LocalDateTime getElementAt(int index) {
if (index >= 0 && index < getSize()) {
return LocalDateTime.now().plusDays(index);
} else {
return LocalDateTime.now();
}
}
@Override public int getSize() {
return 7;
}
@Override public Object getSelectedItem() {
return selectedDate;
}
@Override public void setSelectedItem(Object anItem) {
selectedDate = (LocalDateTime) anItem;
}
});
selectedDate = (LocalDateTime) value;
}
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
comboBox.setSelectedItem(value);
}
});
return comboBox;
}
@Override public Object getCellEditorValue() {
return comboBox.getSelectedItem();
}
@Override public boolean shouldSelectCell(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
MouseEvent e = (MouseEvent) anEvent;
return e.getID() != MouseEvent.MOUSE_DRAGGED;
}
return true;
}
@Override public boolean stopCellEditing() {
if (comboBox.isEditable()) {
// Commit edited value.
comboBox.actionPerformed(new ActionEvent(this, 0, ""));
}
return super.stopCellEditing();
}
@Override public boolean isCellEditable(EventObject e) {
return true;
}
}
View in GitHub: Java, Kotlin解説
ArrowButton
を非表示にしたJComboBox
を使用するTableCellEditor
を作成- この
JComboBox
で編集する値はLocalDateTime
なのでDefaultCellEditor
は使用しないAbstractCellEditor
を継承するTableCellEditor
を作成
LocalDateTime.now()
で取得した日付から7
日分をJComboBox
で選択可能になるよう、TableCellEditor#getTableCellEditorComponent(...)
内でComboBoxModel
を更新