DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. How to copy a static HashMap into a JTable

How to copy a static HashMap into a JTable

A. Programmer user avatar by
A. Programmer
·
Mar. 21, 11 · Interview
Like (0)
Save
Tweet
Share
11.25K Views

Join the DZone community and get the full member experience.

Join For Free

It was a sunny day with easy code and birds singing. But, suddenly a static HashMap (it could be non-static, also) wants to be copied into a two column JTable, keys on first column, and values on the second one. Well, the clouds start to press and before I notice it starts to rain … "with questions":

  • How to delete all rows of a JTable (I need this because sometimes I have to refresh the JTable for 0) ?
  • How to obtain the keys from the JTable by index, or similar?
  • How to add or insert the HashMap elements into JTable ?
Oh, sorry, I forgot to introduce you my static HashMap, nothing special, just a trivial one – the elements are irrelevant, therefore I’ll skip it:
public class MyMapHostClass {
...
public static Map myMap = new HashMap();
...
}

Afterwards, it proves to be a summer rain, warm and refreshing, because the answers jump up in a few minutes: delete all rows using a backward loop (or a nice trick) – both here, get all keys with the keySet method, and insert/add them into JTable with insertRow/addRow method. Well, I glue all these answers into lines code, and get this:
...
DefaultTableModel  model = (DefaultTableModel) jTable.getModel();
...
//remove all rows
model.getDataVector().removeAllElements();
...
//get all keys – notice the static effect, you can use it for non-static HashMap either
Set keys = MyMapHostClass.myMap.keySet();

//get an Iterator over keys
Iterator iteratorKeys = keys.iterator();

//put them into jTable
while (iteratorKeys.hasNext()) {
     String key = (String) iteratorKeys.next();
     //insert on first position by pushing down the existing rows
     model.insertRow(0, new Object[]{key, MyMapHostClass.myMap.get(key)});
     //append at the end
     model.addRow(new Object[]{key, MyMapHostClass.myMap.get(key)});
     }


Done! Now, is still raining, but … I’m singing in the rain!

From http://e-blog-java.blogspot.com/2011/03/how-to-copy-static-hashmap-into-jtable.html

Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Stop Using Spring Profiles Per Environment
  • Introduction to Spring Cloud Kubernetes
  • Tracking Software Architecture Decisions
  • Microservices Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: