"(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