Displaying a Map in a JTable

G

Guest

I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George
 
S

steve

I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

write your own table model, it is real easy. 10-20 minutes of work.
then just pick the values out of the map.

Steve
 
G

Guest

steve said:
write your own table model, it is real easy. 10-20 minutes of work.
then just pick the values out of the map.

Steve

Aw, gee, Steve. Could you give a little example
or a link?

George
 
G

Guest

"(e-mail address removed)"
I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

Here's what I wound up doing: codifying the map
in a 2D array and then passing it to a JTable constructor:

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

public class ShowMap {
public static void main(String[] args)
throws ClassNotFoundException, IOException {
//S: The map is serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}
 
S

steve

"(e-mail address removed)"
I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

Here's what I wound up doing: codifying the map
in a 2D array and then passing it to a JTable constructor:

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

public class ShowMap {
public static void main(String[] args)
throws ClassNotFoundException, IOException {
//S: The map is serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}
ewwwww!

I looked at my own vector/map to table stuff ,but it contains all sorts of
editing, and buffering for canceling user changes, way tooo complex
I guess it will be way beyond what you need.



ill give you a rough outline, it look complex but it is not. ( it's a hack,
and not tested)

it is that the table model insist that you supply certain routines.
basically if you replace the "vector" stuff with your "map", you will get
what you want.

the advantage , is speed and space over code length, because in your

getValueAt()
, you can go straight into your map for the values, without using ,
itterators or secondary stores.


//setup the table and the scroll area


MyTableModel Supplierqtbl = new MyTableModel ();
JTable SupplierListTable = new JTable(Supplierqtbl);
JScrollPane SupplierListScroll = new JScrollPane(SupplierListTable);

//add the above "SupplierListScroll " to a frame

JFrame frame = new JFrame();
frame.getContentPane().add(SupplierListScroll);

that gives a table with it's own table model.
you just need to add a routine to the below code, that takes your "map"

//this is the table handler borrowed from "sun examples"

if you are not doing removing, and inserting, then mot of these routines can
just be dummy.


class MyTableModel extends AbstractTableModel {
Vector vectorTableData = new Vector(); // change this to a map
String[] columnNames = { "col1", "zzz" }; // thankyou sun for another
shitty incomplete routine

public int getColumnCount() {
return columnNames.size();
}

public int getRowCount() {
return vectorTableData.size();
}

public String getColumnName(int col) {
return columnNames.elementAt(col).toString();
}

public Object getValueAt(int row, int col) {
try {
Vector rowData = (Vector)
vectorTableData.elementAt(row);
if (rowData != null) {
return rowData.elementAt
(col);
}
return null;
} catch (Exception e) {
return null;
}
}

public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return false;
}

public void setValueAt(Object value, int row, int col) {

((Vector)vectorTableData.elementAt
(row)).setElementAt(value, col);
fireTableCellUpdated(row, col);

}

public void addRow(Vector someVector) {
vectorTableData.addElement(someVector);
fireTableRowsInserted
(vectorTableData.size() - 1, vectorTableData.size() -1);
}

public void removeRow(int row) {
vectorTableData.removeElementAt(row);
fireTableRowsDeleted(vectorTableData.size
(), vectorTableData.size());
}

}

http://www.google.com/search?hl=en&lr=&newwindow=1&q=dynamic+jtables&btnG=Sear
ch
http://forum.java.sun.com/thread.jspa?threadID=121017&messageID=317271
http://javaalmanac.com/cgi-bin/search/find.pl?words=jtable

K that took 10 minutes to pull together from the web, another 10 min for your
debugging and ur done!!

steve
 
G

Guest

steve said:
"(e-mail address removed)"
I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

Here's what I wound up doing: codifying the map
in a 2D array and then passing it to a JTable constructor:

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

public class ShowMap {
public static void main(String[] args)
throws ClassNotFoundException, IOException {
//S: The map is serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}

Meaning?

I looked at my own vector/map to table stuff ,but it contains all sorts
of
editing, and buffering for canceling user changes, way tooo complex
I guess it will be way beyond what you need.

Well, I think I may want to let the user edit the table
and have the edits reflected in the underlying array.
ill give you a rough outline, it look complex but it is not. ( it's a
hack,
and not tested)

We call "hacking" "epistemologically experimental software engineering".
it is that the table model insist that you supply certain routines.
basically if you replace the "vector" stuff with your "map", you will get
what you want.

Since Maps (HashMaps, TreeMaps) are not indexed
(you access a key-value pair by the key--which is not,
in general, an int), it's not straightforward to codify a
TableModel for a Map. You're using Vector, which uses
an int index to retrieve data.

the advantage , is speed and space over code length, because in your

getValueAt()
, you can go straight into your map for the values, without using ,
itterators or secondary stores.


//setup the table and the scroll area


MyTableModel Supplierqtbl = new MyTableModel ();
JTable SupplierListTable = new JTable(Supplierqtbl);
JScrollPane SupplierListScroll = new JScrollPane(SupplierListTable);

//add the above "SupplierListScroll " to a frame

JFrame frame = new JFrame();
frame.getContentPane().add(SupplierListScroll);

that gives a table with it's own table model.
you just need to add a routine to the below code, that takes your "map"

//this is the table handler borrowed from "sun examples"

if you are not doing removing, and inserting, then mot of these routines
can
just be dummy.


class MyTableModel extends AbstractTableModel {
Vector vectorTableData = new Vector(); // change this to a map

Easier said than done.
String[] columnNames = { "col1", "zzz" }; // thankyou sun for another
shitty incomplete routine

public int getColumnCount() {
return columnNames.size();
}

public int getRowCount() {
return vectorTableData.size();
}

public String getColumnName(int col) {
return columnNames.elementAt(col).toString();
}

public Object getValueAt(int row, int col) {

This isn't simple with a Map, since the rows
are accessed by an Object (in my case a
String), not ints.
 
S

steve

steve said:
"(e-mail address removed)"
I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

Here's what I wound up doing: codifying the map
in a 2D array and then passing it to a JTable constructor:

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

public class ShowMap {
public static void main(String[] args)
throws ClassNotFoundException, IOException {
//S: The map is serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}

Meaning?

I looked at my own vector/map to table stuff ,but it contains all sorts
of
editing, and buffering for canceling user changes, way tooo complex
I guess it will be way beyond what you need.

Well, I think I may want to let the user edit the table
and have the edits reflected in the underlying array.
ill give you a rough outline, it look complex but it is not. ( it's a
hack,
and not tested)

We call "hacking" "epistemologically experimental software engineering".
it is that the table model insist that you supply certain routines.
basically if you replace the "vector" stuff with your "map", you will get
what you want.

Since Maps (HashMaps, TreeMaps) are not indexed
(you access a key-value pair by the key--which is not,
in general, an int), it's not straightforward to codify a
TableModel for a Map. You're using Vector, which uses
an int index to retrieve data.

the advantage , is speed and space over code length, because in your

getValueAt()
, you can go straight into your map for the values, without using ,
itterators or secondary stores.


//setup the table and the scroll area


MyTableModel Supplierqtbl = new MyTableModel ();
JTable SupplierListTable = new JTable(Supplierqtbl);
JScrollPane SupplierListScroll = new JScrollPane(SupplierListTable);

//add the above "SupplierListScroll " to a frame

JFrame frame = new JFrame();
frame.getContentPane().add(SupplierListScroll);

that gives a table with it's own table model.
you just need to add a routine to the below code, that takes your "map"

//this is the table handler borrowed from "sun examples"

if you are not doing removing, and inserting, then mot of these routines
can
just be dummy.


class MyTableModel extends AbstractTableModel {
Vector vectorTableData = new Vector(); // change this to a map

Easier said than done.
build an array to key map
String[] columnNames = { "col1", "zzz" }; // thankyou sun for another
shitty incomplete routine

public int getColumnCount() {
return columnNames.size();
}

public int getRowCount() {
return vectorTableData.size();
}

public String getColumnName(int col) {
return columnNames.elementAt(col).toString();
}

public Object getValueAt(int row, int col) {

This isn't simple with a Map, since the rows
are accessed by an Object (in my case a
String), not ints.
write an access function!! to the array

i don't see what the problem is, you can iterate a map, and you could easily
build an array, and store the keys in it.
 
G

Guest

steve said:
steve said:
On Wed, 20 Apr 2005 02:27:34 +0800,
(e-mail address removed) wrote
(in article <[email protected]>):


"(e-mail address removed)"
I have a Map<String, String> which I would like to
display in a scrollable JTable. One solution I've
thought of is transforming the Map to a 2D array,
which I could feed to JTable constructor.
But maybe a TableModel offers a better solution.
Any hints from someone who has done this?

George

Here's what I wound up doing: codifying the map
in a 2D array and then passing it to a JTable constructor:

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

public class ShowMap {
public static void main(String[] args)
throws ClassNotFoundException, IOException {
//S: The map is serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}

Meaning?

I looked at my own vector/map to table stuff ,but it contains all
sorts
of
editing, and buffering for canceling user changes, way tooo complex
I guess it will be way beyond what you need.

Well, I think I may want to let the user edit the table
and have the edits reflected in the underlying array.
ill give you a rough outline, it look complex but it is not. ( it's a
hack,
and not tested)

We call "hacking" "epistemologically experimental software engineering".
it is that the table model insist that you supply certain routines.
basically if you replace the "vector" stuff with your "map", you will
get
what you want.

Since Maps (HashMaps, TreeMaps) are not indexed
(you access a key-value pair by the key--which is not,
in general, an int), it's not straightforward to codify a
TableModel for a Map. You're using Vector, which uses
an int index to retrieve data.

the advantage , is speed and space over code length, because in your

getValueAt()
, you can go straight into your map for the values, without using ,
itterators or secondary stores.


//setup the table and the scroll area


MyTableModel Supplierqtbl = new MyTableModel ();
JTable SupplierListTable = new JTable(Supplierqtbl);
JScrollPane SupplierListScroll = new JScrollPane(SupplierListTable);

//add the above "SupplierListScroll " to a frame

JFrame frame = new JFrame();
frame.getContentPane().add(SupplierListScroll);

that gives a table with it's own table model.
you just need to add a routine to the below code, that takes your "map"

//this is the table handler borrowed from "sun examples"

if you are not doing removing, and inserting, then mot of these routines
can
just be dummy.


class MyTableModel extends AbstractTableModel {
Vector vectorTableData = new Vector(); // change this to a map

Easier said than done.
build an array to key map
String[] columnNames = { "col1", "zzz" }; // thankyou sun for another
shitty incomplete routine

public int getColumnCount() {
return columnNames.size();
}

public int getRowCount() {
return vectorTableData.size();
}

public String getColumnName(int col) {
return columnNames.elementAt(col).toString();
}

public Object getValueAt(int row, int col) {

This isn't simple with a Map, since the rows
are accessed by an Object (in my case a
String), not ints.
write an access function!! to the array

i don't see what the problem is, you can iterate a map, and you could
easily
build an array, and store the keys in it.

I thought there might be a more elegant way to
display a Map<String, String> in a JTable than
the way I did it below. But I've grown to like what
I did, and have no problem with it. Do you see
anything wrong with it? [It has the advantage of
working! : o ) ]

George

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

public class ShowMap {
public static void main(String[] args)
throws IOException, ClassNotFoundException { //See why below.
//S: The map should be serialized in file args[0].
if (args.length < 1) {
System.out.println("Supply args[0] = file name.");
System.exit(0);
}
//A: Read map from the designated file:
//S: ObjectIO.read(args[0]) may throw an IOException.
//S: The (Map) cast may throw a ClassNotFoundException.
Map map = (Map)ObjectIO.read(args[0]);
//A: Codify map in a two dimensional array:
String[] columnNames = {"Key", "Value"};
//S: The number of rows of data is map.size().
Object[][] data = new Object[map.size()][2];
Iterator iterator = map.keySet().iterator();
int row = 0;
while ( iterator.hasNext() ) {
Object key = iterator.next();
data[row][0] = key;
data[row][1] = map.get(key);
row = row + 1;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollpane = new JScrollPane(table);

JFrame frame = new JFrame();
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top