Solved

populate a jtable from a map

Posted on 2011-09-14
6
372 Views
Last Modified: 2012-06-27
I am creating a jtable in a dialog.  Right now I am just putting in static values as you will see in the code.  I would like to learn how to populate the table with a map.

 
private JPanel selectedTable(){
		
    JLabel tLabel = new JLabel("Selections");
		
	JPanel p = new JPanel();
    p.setBorder(BorderFactory.createLineBorder(Color.blue));
    GridBagLayout gridbag = new GridBagLayout();
    p.setLayout(gridbag);
    
    
 // Create columns names
	String columnNames[] = { "Item ID", "Rev ID", "PRL", "Dataset", "EC Markup Included" };
    
// Create some data
	String dataValues[][] =
	{
		{ "1", "2", "3", "4", "5" },
		{ "6", "7", "8", "9", "10" },
		{ "93", "89.2", "109", "11", "12"}
	};   
	
	// Create a new table instance
	selectTable = new JTable( dataValues, columnNames );
	selectTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
	selectTable.setCellSelectionEnabled(false);
	selectTable.setRowSelectionAllowed(true);
	selectTable.setColumnSelectionAllowed(false);
	//selectTable.setMinimumSize(table.getPreferredSize());

	// Add the table to a scrolling pane
	 JScrollPane ScrollPane = new JScrollPane(selectTable);
	 
	 
	   
	gbc.insets = new Insets(5,10,10,5);      
    set_gbc(0, 0, 1, 1,GridBagConstraints.BOTH );
    gridbag.setConstraints(tLabel, gbc);
    p.add(tLabel);
	
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH; 
    //set_gbc(0, 1, 1, 1,GridBagConstraints.BOTH );
    //gridbag.setConstraints(ScrollPane, gbc);
    
    p.add(ScrollPane,gbc);
    
    return p;
}

Open in new window


my map is called newMap .  I would like for my key to be column ItemId and the value be column RevId.

Thanks
0
Comment
Question by:jkteater
  • 4
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36538146


You can create a variable of DaefaulTableModel type and populate it with your data;
This is an example:
http://www.java2s.com/Code/JavaAPI/javax.swing.table/DefaultTableModeladdRowObjectrowData.htm
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36538187

I assume that you have some RevType object which has all these fields
and you newMap has ID maped to objects of RevType type
Then this is how you populate your model:

 private DefaultTableModel model;
 model = new DefaultTableModel();

for (String s: columnNmaes){
model.addColumn(s);

}

Set set = newMap.keySet();

for (String s : set){

RevType r = newMap.get(s);
Vector<String> v = new Vector<String>();
v.addElement(r.getItemID());
v.addElement(r.getRevID();
...

model.addRow(v);



}

jtable.sertModel(model);

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 36538862
>>I would like to learn how to populate the table with a map.

For your Map<K, V> , what type is K and what type is V?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 47

Accepted Solution

by:
for_yan earned 75 total points
ID: 36539006

As far as I rememeber
you actually have ArrayList <String>userObjects which has the set of keys (TYPEs)
and HashMap<String,ItemRev> which has the data mapped to the Keys as ItemRev objects


So then this is how you create table model very siimilar to waht I posted above but with more familiar names:
 private DefaultTableModel model;
 model = new DefaultTableModel();

for (String s: columnNmaes){
model.addColumn(s);

}



for (String s : userObjects){

ItemRev r = map.get(s);



Vector<String> v = new Vector<String>();
v.addElement(r.getItemID());
v.addElement(r.getRevID();
...


model.addRow(v);
}


}

jtable.sertModel(model);

Open in new window

0
 

Author Comment

by:jkteater
ID: 36539730
I have just found out that from my team lead that I can not use a map.  I have to use a list because my map is object, object and the actual data is more object, array.  So I will adjust the points and close this question.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36539801
What means object,object and object,array and why you cannot use map?
Map can also be String-Array if you need (with String being the key and array of Strings being the value).
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question