tags:

views:

1834

answers:

4

I have an ArrayList of Track objects. Each Track object has the following fields (all Strings):

url, title, creator, album, genre, composer

I want to display these tracks in a JTable, with each row being an instance of a Track object, and each column containing one of the properties of a Track object.

How can I display this data using a JTable? I've used an AbstractTableModel that implements the getValueAt() method correctly. Still, I dont see anything on the screen.

Or is it easier just to use arrays?

A: 

In order to add contents to display on a JTable, one uses the TableModel to add items to display.

One of a way to add a row of data to the DefaultTableModel is by using the addRow method which will take an array of Objects that represents the objects in the row. Since there are no methods to directly add contents from an ArrayList, one can create an array of Objects by accessing the contents of the ArrayList.

The following example uses a KeyValuePair class which is a holder for data (similar to your Track class), which will be used to populate a DefaultTableModel to display a table as a JTable:

class KeyValuePair
{
    public String key;
    public String value;

    public KeyValuePair(String k, String v)
    {
        key = k;
        value = v;
    }
}

// ArrayList containing the data to display in the table.
ArrayList<KeyValuePair> list = new ArrayList<KeyValuePair>();
list.add(new KeyValuePair("Foo1", "Bar1"));
list.add(new KeyValuePair("Foo2", "Bar2"));
list.add(new KeyValuePair("Foo3", "Bar3"));

// Instantiate JTable and DefaultTableModel, and set it as the
// TableModel for the JTable.
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel();
table.setModel(model);
model.setColumnIdentifiers(new String[] {"Key", "Value"});

// Populate the JTable (TableModel) with data from ArrayList
for (KeyValuePair p : list)
{
    model.addRow(new String[] {p.key, p.value});
}
coobird
Awesome!!!!! Thanks a whole bunch.
Penchant
You're welcome :)
coobird
A: 

then, the problem i have is that i cant see the problem at all. here's the code i've used to make the interface:

public Interface(){

 setSize(1024, 768);
 setBackground(new Color(0,0,0));
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 add(new MenuBar());//simple JMenuBar implementation

 table= new JTable();
 table.setPreferredScrollableViewportSize(new Dimension(500, 500));

 JScrollPane jsp = new JScrollPane(table);
 add(jsp);

 pack();
 setVisible(true);
}

after this code is executed, i have code in another class that does this many times:

((DefaultTableModel)mainInterface.table.getModel()).addRow(
new String[] {t.location,t.title,t.creator,t.album,t.genre,t.composer});

t is a Track object by the way.

Penchant
A: 

I forgot adding the column names:

DefaultTableModel model = new DefaultTableModel(); table.setModel(model); model.setColumnIdentifiers(new String[] {"URL", "Title", "Creator", "Album", "Genre", "Composer"});

No columns= cant see anything.

Penchant
A: 

okay I did this:

public class test {
public String key;
public String value;
public JTable table;



public test(String k, String v){
    key = k;
    value = v;
    showtable();

}



public void maketable(){


    ArrayList<test> list = new ArrayList<test>();
    DefaultTableModel model = new DefaultTableModel();

    table.setModel(model);
    model.setColumnIdentifiers(new String[] {"key","value"});

    list.add(new test("test1","test2"));
    list.add(new test("test3","test4"));

    for(test p : list){
        model.addRow(new String[] {p.key,p.value});

}
}

public void showtable(){


    JFrame frame = new JFrame("FrameDemo");
    Container content = frame.getContentPane();
    content.add(table);
    frame.pack();
    frame.setVisible(true);



}


}

In the main class, I run an object of this class:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    test test_1 = new test("jaja","nenen");

    // TODO code application logic here
}

}

It ain't working so what did I do wrong :(

Fabian