Swing/AddRow のバックアップ差分(No.27)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/AddRow へ行く。
- 1 (2006-04-04 (火) 19:35:45)
- 2 (2006-04-12 (水) 19:32:34)
- 3 (2006-11-01 (水) 20:43:35)
- 4 (2006-11-10 (金) 03:37:20)
- 5 (2007-05-10 (木) 10:50:55)
- 6 (2007-09-25 (火) 20:57:28)
- 7 (2007-10-24 (水) 18:08:26)
- 8 (2007-10-27 (土) 15:26:41)
- 9 (2008-01-24 (木) 12:45:16)
- 10 (2008-06-11 (水) 17:00:16)
- 11 (2008-07-02 (水) 18:45:22)
- 12 (2008-11-26 (水) 18:57:05)
- 13 (2010-03-08 (月) 12:24:31)
- 14 (2010-03-08 (月) 13:42:24)
- 15 (2010-07-16 (金) 15:32:29)
- 16 (2010-08-04 (水) 19:16:18)
- 17 (2010-12-12 (日) 23:16:11)
- 18 (2012-06-17 (日) 15:31:04)
- 19 (2013-02-20 (水) 15:33:13)
- 20 (2013-04-09 (火) 21:59:42)
- 21 (2014-11-13 (木) 01:21:04)
- 22 (2014-11-25 (火) 07:10:26)
- 23 (2015-03-17 (火) 18:15:09)
- 24 (2015-08-05 (水) 15:12:00)
- 25 (2016-06-23 (木) 12:30:31)
- 26 (2016-08-16 (火) 13:40:47)
- 27 (2017-10-06 (金) 14:50:29)
- 28 (2019-04-17 (水) 18:52:03)
- 29 (2021-01-26 (火) 11:23:01)
- 30 (2023-11-24 (金) 16:03:10)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: AddRow title: JTableの行を追加、削除 tags: [JTable, JPopupMenu] author: aterai pubdate: 2004-05-03 pubdate: 2004-05-03T19:35:45+09:00 description: JTableへの行追加と、選択行の削除をJPopupMenuから実行します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTHaSWCCMI/AAAAAAAAARI/UM476TJWRdM/s800/AddRow.png --- * 概要 [#summary] `JTable`への行追加と、選択行の削除を`JPopupMenu`から実行します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTHaSWCCMI/AAAAAAAAARI/UM476TJWRdM/s800/AddRow.png) * サンプルコード [#sourcecode] #code(link){{ private final DefaultTableModel model = new DefaultTableModel(); private final JTable table; private void createActionPerformed(ActionEvent e) { int rc = model.getRowCount(); model.addRow(new Object[] {rc, "New name", ""}); //追加された最終行までスクロール table.scrollRectToVisible(table.getCellRect(rc, 0, true)); } private void deleteActionPerformed(ActionEvent e) { int[] selection = table.getSelectedRows(); for (int i = selection.length - 1; i >= 0; i--) { model.removeRow(table.convertRowIndexToModel(selection[i])); } } }} * 解説 [#explanation] 上記のサンプルでは、ポップアップメニューを使って、行の追加と削除を行っています。 - 追加 -- `DefaultTableModel`の`addRow`メソッドを使用し、オブジェクトの配列を行として追加 -- 追加された行が表示されるように、`JTable#getCellRect(...)`で行の領域を取得し、`JTable#scrollRectToVisible(...)`でスクロール - 削除 -- 複数行の削除に対応するために、`index`の大きい方から削除する -- 行のソートを行っている可能性があるので、`JTable#convertRowIndexToModel(int)`で、`viewIndex`を`modelIndex`に変換してから`DefaultTableModel#removeRow(int modelIndex)`を使って削除 * 参考リンク [#reference] - [[JTableのセルにJButtonを追加して行削除>Swing/DeleteButtonInCell]] - [[JTableの行を全削除>Swing/ClearTable]] - [[JTableで行の追加、削除アニメーション>Swing/SlideTableRows]] * コメント [#comment] #comment #comment