Help us understand the problem. What is going on with this article?

Java - JTableの作り方

More than 3 years have passed since last update.

考え方

  1. 初期データ構造を作る(カラムとデータ)
  2. DefaultTableModelを作成
  3. JTableを作成
  4. JScrollPaneに入れる
  5. JFrameに入れる
  6. 描画する

サンプルコード

1, データ構造を作る

String[] columnNames = { "1", "2", "3", "4" };

String[] data1 = { "A", "B", "C", "D" };
String[] data2 = { "E", "F", "G", "H" };
String[] data3 = { "I", "J", "K", "L" };
String[] data4 = { "M", "N", "O", "P" };
String[] data5 = { "Q", "R", "S", "T" };

String[][] data = { data1, data2, data3, data4, data5 };

2, DefaultTableModelを作成

DefaultTableModel model = new DefaultTableModel(data, columnNames);

3, JTableを作成

JTable table = new JTable(model);

4, JScrollPaneに入れる

JScrollPane spane = new JScrollPane(table);

5, JFrameに入れる

JFrame frame = new JFrame("Title");
frame.setSize(500, 500);
frame.add(spane);

6, 描画する

frame.setVisible(true);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away