Solved

populate a jtable from a map

Posted on 2011-09-14
6
367 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
Comment Utility


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
Comment Utility

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
Comment Utility
>>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Accepted Solution

by:
for_yan earned 75 total points
Comment Utility

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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 video teaches viewers about errors in exception handling.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now