Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

populate a jtable from a map

Posted on 2011-09-14
6
Medium Priority
?
384 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
Industry Leaders: 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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