开发者

JTable 3 fields arrayList

I am trying to add values to a Jtable, the values are fetched fr开发者_如何转开发om arrayList, How do you do that

I tried making Object[][] data; and the populate it inside a loop, but it does not work, How do you fix this?

String[] columns = {"Field String","Field Double"," Field Double"};
Object[][] data;

Iterator<Node> itr = arrayList.iterator();
while (itr.hasNext()) {
   Node el    = itr.next();
   double a   = el.getval();
   data[i][1] = el.getstring();
   data[i][2] = a;
   data[i][3] = a*4;
   i++;
}


JFrame frame = new JFrame("Title ");
JTable tablE = new JTable(data, columnas);
JPanel panel = new JPanel();

panel.add(table);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);

How do you populate "data" inside a while loop?


Use a DefaultTableModel and add rows of data using its addRow(Object[]) or addRow(Vector) method. Set this as your JTable's model. The API and the JTable tutorial can get you started.

For e.g.,

  ArrayList arrayList = new ArrayList();

  String[] columns = {"Field String","Field Double"," Field Double"};
  DefaultTableModel model = new DefaultTableModel(columns, 0);

  for (Object item : arrayList) {
     Object[] row = new Object[3];
     //... fill in row with info from item

     model.addRow(row);
  }

  JTable table = new JTable(model);

This demonstrates doing it with a for loop, but a while loop would be similar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜