?
Solved

populate a jtable from a map

Posted on 2011-09-14
6
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Accepted Solution

by:
for_yan earned 300 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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