views:

3288

answers:

4

Before you answer: Yes I have read the jtable tutorial over at Sun. No, it did not help me. Yes, I am a dolt. Please don't answer with a reference to that document. What I am specifically interested in is how to dynamically add rows and columns to my Jtable via the Netbeans IDE. I already have an object that contains a hashmap with my data. I can't figure out where or what object I should be passing that object to. Thanks for your time!

I have a vector that contains a series (of length l) of objects (each one corresponding to a row). How do I get that vector object to display on the JTable?

+2  A: 

A JTable uses a TableModel to hold its data. Your hash/vector of data will need to be adapted to be used; you can write a TableModel implementation, using the hash/vector as backing data, or, if you won't be dynamically updating the hash/vector and needing it to show automatically, you can simply copy everything into an instance of DefaultTableModel, and use that.

If you do use an adapter, and dynamically update the hash/vector, remember that all updates must be done in the event dispatch thread. :-)

Chris Jester-Young
And make sure if you modify columns/rows that the model properly fires model change events, otherwise you won't see anything update in the table.
John Gardner
Good point---sorry I took that for granted. :-)
Chris Jester-Young
A: 

To add to my previous answer, for what it's worth, I've actually written a table model that uses (essentially) an ArrayList<Row> as backing data, where Row is a HashMap<String, Object>, mapping column names to values.

The whole thing is about 1500 lines of code, although my code may be overkill for your purposes, and you probably don't have to write nearly as much code. All the best!

Chris Jester-Young
A: 

Just to illustrate, the following are examples of how to use the DefaultTableModel to show your data from HashMaps and Vectors.

The following is an example of dumping data from a HashMap onto a DefaultTableModel which is used as the TableModel of a JTable.

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableExample extends JFrame
{
    private void makeGUI()
    {
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     // HashMap with some data.
     HashMap<String, String> map = new HashMap<String, String>();
     map.put("key1", "value1");
     map.put("key2", "value2");

     // Create a DefaultTableModel, which will be used as the
     // model for the JTable.
     DefaultTableModel model = new DefaultTableModel();

     // Populate the model with data from HashMap.
     model.setColumnIdentifiers(new String[] {"key", "value"});

     for (String key : map.keySet())
      model.addRow(new Object[] {key, map.get(key)});

     // Make a JTable, using the DefaultTableModel we just made
     // as its model.
     JTable table = new JTable(model);

     this.getContentPane().add(table);
     this.setSize(200,200);
     this.setLocation(200,200);
     this.validate();
     this.setVisible(true);
    }

    public static void main(String[] args)
    {
     new JTableExample().makeGUI();
    }
}

For using a Vector to include a column of data into a JTable:

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableExample extends JFrame
{
    private void makeGUI()
    {
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     // Vector with data.
     Vector<String> v = new Vector<String>();
     v.add("first");
     v.add("second");

     // Create a DefaultTableModel, which will be used as the
     // model for the JTable.
     DefaultTableModel model = new DefaultTableModel();

     // Add a column of data from Vector into the model.
     model.addColumn("data", v);

     // Make a JTable, using the DefaultTableModel we just made
     // as its model.
     JTable table = new JTable(model);

     this.getContentPane().add(table);
     this.setSize(200,200);
     this.setLocation(200,200);
     this.validate();
     this.setVisible(true);
    }

    public static void main(String[] args)
    {
     new JTableExample().makeGUI();
    }
}

I have to admit that the column names don't appear when using the above examples (I usually use the DefaultTableModel's setDataVector method), so if anyone has any suggestions on how to make the column names appear, please do :)

coobird
A: 

Just an addition to coobirds post, to get the header to appear i did this:

import java.util.; import javax.swing.; import javax.swing.table.*;

public class JTableExample extends JFrame { private void makeGUI() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // HashMap with some data.
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");

    // Create a DefaultTableModel, which will be used as the
    // model for the JTable.
    DefaultTableModel model = new DefaultTableModel();

    // Populate the model with data from HashMap.
    model.setColumnIdentifiers(new String[] {"key", "value"});

    for (String key : map.keySet())
            model.addRow(new Object[] {key, map.get(key)});

    // Make a JTable, using the DefaultTableModel we just made
    // as its model.
    JTable table = new JTable(model);

    this.getContentPane().add(new JScrollPane(table));
    this.setSize(200,200);
    this.setLocation(200,200);
    this.validate();
    this.setVisible(true);
}

public static void main(String[] args)
{
    new JTableExample().makeGUI();
}

}

By the way, your post was very helpful for me coobird, you have no idea how thankful I am!