I have an object Person
. This object is in a List. Now I want to insert the data vorname , nachname and alter into the jTable
.
How can I do this?
public MainForm() {
eintragenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
personList = personAnlegen.PersonAnlegen(
textFieldVorname.getText(),
textFieldNachname.getText(),
textFieldAlter.getText()
);
String[] spaltenueberschrift = {"Vorname", "Nachname","Alter"};
String[][] inhalt;
for (int i = 0; i < personList.size(); i++) {
inhalt[i][i] = personList.get(0).getVorname(), personList.get(0).getNachname(), personList.get(0).getAlter();
}
tablePersonen = new JTable();
scrollPane.setViewportView(tablePersonen);
DefaultTableModel tableModel = new DefaultTableModel(
inhalt,
spaltenueberschrift
);
tablePersonen.setModel(tableModel);
}
});
}
I have I a object "Person" this object is in a List
Basically the List
needs to be part of a custom TableModel
.
You need to create a custom TableModel
for your Person
object.
Check out Row Table Model for a step by step approach on how you can do this.
The link also shows how to make a more generic TableModel
so you can reuse common code.
"Versuch dies" try this
JTable tablePersonen = new JTable(inhalt, spaltenueberschrift);
JTable(Object[][] rowData, Object[] columnNames)
openbooks(in german): http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_19_019.htm
You should make new AbstractTableModel() http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
personList = personAnlegen.PersonAnlegen(
textFieldVorname.getText(),
textFieldNachname.getText(),
textFieldAlter.getText()
);
String[] spaltenueberschrift = {"Vorname", "Nachname","Alter"};
String[][] inhalt;
for (int i = 0; i < personList.size(); i++) {
inhalt[i][i] = personList.get(0).getVorname(), personList.get(0).getNachname(), personList.get(0).getAlter();
}
tablePersonen = new JTable();
scrollPane.setViewportView(tablePersonen);
tablePersonen.setModel(new AbstractTableModel() {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return inhalt[rowIndex][columnIndex];
}
@Override
public int getRowCount() {
return inhalt.length;
}
@Override
public int getColumnCount() {
return spaltenueberschrift.length;
}
public String getColumnName(int col) {
return spaltenueberschrift[col];
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.