Solved

populate a jtable from a map

Posted on 2011-09-14
6
374 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 156
use lov values 2 72
collection output issue 9 66
American Express @Work site and Java 4 68
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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