Reputation: 105
I am having some problems displaying data in an ArrayList in the form of a JTable.
The ArrayList consists of data, but only when the user enters data into JTextField's in a GUI (this GUI is in another class). I have used getText().
My ArrayList is in one class, but the GUI for the JFrame for the JTable is in another class. I can't seem to create a JTable in the GUI and I can't seem to be able to get the data from the ArrayList to be displayed in the JTable.
The ArrayList consists of 12 JTextFields all of which are Strings which should be the Headings for the JTable. When the program is launched, the user is able to enter their own data as many times as they want which is stored in the ArrayList under each Heading. This data is always different as the user enters different data all the time and therefore, I think, I cannot use this:
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
What do you think I should do? And how should I implement this?
Any help is much appreciated!
Upvotes: 0
Views: 3786
Reputation: 1
Not sure if it's a right practice, but make the ArrayList
and columnNames
static.
The data array should be split into individual ArrayLists:
ArrayList<String> firstNameAR
ArrayList<String> lastNameAR
ArrayList<String> sportAR
and so on.
Then in your table model use like this:
(final variables are just numbers from 0 to n)
public Object getValueAt(int row, int column) {
switch (column) {
case Data.FIRSTNAME:
return Data.firstNameAR.get(row);
case Data.LASTNAME:
return Data.lastNameAR.get(row);
case Data.SPORT:
return Data.sportAR.get(row);
}
return null;
}
Upvotes: 0
Reputation: 63054
JTable
uses TableModel
as its backing model, which you supply to the JTable
constructor. Whatever the TableModel
exposes, the JTable
will display. Your backing data source looks simple enough that you can use the built-in DefaultTableModel
. Alternatively you could implement your own TableModel
that wraps your own data source.
Edit: JTable
has a constructor that directly accepts simple array data, which you might be able to use.
public JTable(Object[][] rowData, Object[] columnNames)
FYI, With Java 1.5 and above, you can replace new Integer(123)
with 123
as the compiler will autobox this to Integer.valueOf(123)
. The same goes for new Boolean(true)
.
Upvotes: 2
Reputation: 2520
It seems that you have to use a TableModel
.
Your program's flow might look like this:
ArrayList
(or whatever data structure you're holding) with that data.TableModel
instance, backed with your data structure, and pass it to the JTable
. (This could be done without replacing the TableModel
, if you use any of the AbstractTableModel.fireTableXXXXX()
methods).Upvotes: 0