0

I'm new to java and I want to take data that I have stored in a Map and add it to a table. As I was reading the java tutorials on "How to Use Tables" I found myself lost looking at the sample code below:

String[] columnNames = { "blah", "blah" };

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)}
};

I'm supposed to then instantiate a JTable object like this:

JTable table = new JTable(data, columnNames);

I'm confused as to how I would populate data from a map into an array of objects like they did in the example above? The data in the rows from my map will be a String and a Float.

Ideally, I would have two columns (just an example), { "File", "Size" } and then my rows would put the string for the file name and the float for size.

I tried various things which I feel are too silly to put here, but nothing worked.

3
  • 1
    For better help sooner, post an SSCCE. Now I've read a bit further, I'd create a MapTableModel for that. It would accept a Map in the constructor. Column header names would be Key and Value. .. Commented May 13, 2013 at 14:42
  • @AndrewThompson Thanks although I don't know how to that, I'm going to read about it and give it a shot. It sounds like you are definitely leading me into the right direction. Commented May 13, 2013 at 14:46
  • A Map is not a good idea to store you data when you are using a table. A TableModel needs to be able to access the data by a row/column value. So you would probably be better of to use a DefaultTableModel and copy your data from the map to the model. Commented May 13, 2013 at 15:04

2 Answers 2

3
  Object[][] fileList = new Object[fileListVector.size()][2];

for (int i = 0; i < fileListVector.size(); i++) {
    fileList [i][0] = fileListVector.get(i).getFileName();
    fileList [i][1] = fileListVector.get(i).getFileSize();
}

You can populate your data from a vector to an array like this.

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

Comments

3

I recommend to use a List-based custom TableModel instead.

Have a look at the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

Here's a sample of a List-based read-only TableModel I wrote some time ago:

http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

Instead of the NbBundle you can also use a ResourceBundle, if you're not building your application on top of the NetBeans Platform (though I recommend to use a RCP).

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.