Swing/NonEditableCellEditor のバックアップ(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/NonEditableCellEditor へ行く。
- 1 (2020-02-10 (月) 16:03:45)
- 2 (2021-07-30 (金) 04:23:23)
- 3 (2023-03-16 (木) 16:33:17)
- 4 (2025-01-03 (金) 08:57:02)
- 5 (2025-01-03 (金) 09:01:23)
- 6 (2025-01-03 (金) 09:02:38)
- 7 (2025-01-03 (金) 09:03:21)
- 8 (2025-01-03 (金) 09:04:02)
- 9 (2025-06-19 (木) 12:41:37)
- 10 (2025-06-19 (木) 12:43:47)
- 11 (2025-10-22 (水) 20:19:30)
- category: swing folder: NonEditableCellEditor title: JTableのCellEditorを編集不可だが選択・コピーを可能に変更する tags: [JTable, TableCellEditor, JPopupMenu] author: aterai pubdate: 2020-02-10T16:01:12+09:00 description: JTableのCellEditorとして編集不可・選択コピー可能なJTextFieldを設定します。 image: https://drive.google.com/uc?id=1REUOW6JqlORjh3rkRnez7K3E-HxCDL0j
Summary
JTableのCellEditorとして編集不可・選択コピー可能なJTextFieldを設定します。
Screenshot

Advertisement
Source Code Examples
JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
// table.setFocusable(false);
// table.setCellSelectionEnabled(false);
table.setSelectionForeground(Color.BLACK);
table.setSelectionBackground(new Color(0xEE_EE_EE));
JTextField field = new JTextField();
field.setEditable(false);
field.setBackground(table.getSelectionBackground());
field.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
field.setComponentPopupMenu(new TextComponentPopupMenu());
DefaultCellEditor cellEditor = new DefaultCellEditor(field);
cellEditor.setClickCountToStart(1);
table.setDefaultEditor(Object.class, cellEditor);
View in GitHub: Java, KotlinDescription
JTable#isCellEditable(...)などをオーバーライドしてJTableを編集不可にするとセル内の文字列を部分選択してコピーできない- デフォルトの
JTableではセルを選択してCtrl+Cを入力しても列がタブ区切りでコピーされる Object.classのDefaultCellEditorとして編集を不可にしたJTextFieldを設定することで編集は不可だが、文字列を選択してのコピーは可能にするJTextField#setEditable(false)で編集不可に設定JTextField#setBackground(JTable#getSelectionBackground())でセルエディタの背景色をJTableの選択時背景色と同じに変更JTextField#setComponentPopupMenu(...)でポップアップメニューからのコピーも可能にするDefaultCellEditor#setClickCountToStart(1)を使用してシングルクリックでセルエディタが起動するよう設定し直接文字列の選択を可能にする