0

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!

1
  • For better help sooner, post an SSCCE. Note that a multi-class SSCCE is possible so long as there is no more than 1 public class in the code. Commented Apr 11, 2011 at 0:51

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

The FYI paragraphy is valid as long the compiler is >= 1.5
Hi, thanks for your help so far. I am still confused as to how I can get the data from the ArrayList in one class to be displayed in a table in another class. Please could you advise me.
0

It seems that you have to use a TableModel.

Your program's flow might look like this:

  1. get the data (columns, cells) from the user. (are you using the command line?)
  2. fill up the ArrayList (or whatever data structure you're holding) with that data.
  3. create a new 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).

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.