Reputation: 199
I can't manage to display the JTable in a JFrame. Actually I have extracted the data from an ArrayList and filled the JTable row by row. I have checked that the JTable is filled, the number of rows is equal to the rows in the original ArrayList. Yet running the function shows a blank GUI. I really do not see the problem in my code:
public Graphicalinterface4() {
//super (new GridLayout(1,0));
//panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
getdata2 in=new getdata2();
try {
ArrayList<Piece> test=in.getList();
ArrayList<Piece> classified=in.classify(test);
JTable table=getlocaltable(classified);
table.setFillsViewportHeight(true);
JScrollPane scrPane=new JScrollPane(table);
//scrPane.setSize(800,690);
//scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scrPane);
//scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public JTable getlocaltable(ArrayList<Piece> in) {
//new Object[]{"Type","Company","reference","description","price","To order"}));
//DefaultListModel<Piece> testlst=new DefaultListModel<Piece>();
int sz=in.size();
String[] columns=new String[] {
"Type","Company","reference","description","price","To order"
};
DefaultTableModel model = new DefaultTableModel();
//JTable table=new JTable(null, in.toArray());
for (int i=0;i<sz;i++) {
Piece p=in.get(i);
String type=p.gettype();
String company=p.getasc();
String reference=p.getref();
String description=p.getdesc();
String price=p.getprice();
String image=p.getimage();
System.out.println(type);
//DefaultTableModel model=(DefaultTableModel) table.getModel();
model.addRow(new Object[]{type,company,reference,description,price,Integer.toString(0)});
}
JTable table=new JTable(model);
System.out.println(table.getRowCount());
return table;
}
static Graphicalinterface4 ssp;
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {ssp=new Graphicalinterface4();}
});
}
Upvotes: 0
Views: 60
Reputation: 199
Thanks! I had not initialized the DefaultTableModel correctly. In the API the String[] and Object[][]{} are inverted(DefaultTableModel(String, Object[][]{}), but this works for me.
public JTable getlocaltable(ArrayList<Piece> in) {
//new Object[]{"Type","Company","reference","description","price","To order"}));
int sz=in.size();
String[] columns=new String[] {
"Type","Company","reference","description","price","To order"
};
DefaultTableModel model = new DefaultTableModel(new Object[][]{}, columns);
//JTable table=new JTable(model);
for (int i=0;i<sz;i++) {
Piece p=in.get(i);
String type=p.gettype();
String company=p.getasc();
String reference=p.getref();
String description=p.getdesc();
String price=p.getprice();
String image=p.getimage();
System.out.println(type);
//DefaultTableModel model=(DefaultTableModel) table.getModel();
model.addRow(new Object[]{type,company,reference,description,price,Integer.toString(0)});
}
JTable table=new JTable(model);
System.out.println(table.getRowCount());
return table;
}
Upvotes: 0
Reputation: 324108
DefaultTableModel model = new DefaultTableModel();
You have 0 columns in the TableModel
. The columns displayed is determined by the columns in the TableModel
, not the number of items in each row. So adding rows of data has no effect.
Read the DefaultTableModel
API for the proper constructor to use that will accept the "columns" variable as a parameter.
Upvotes: 1