0

I Have a hash Table

hashtable c = new Hashtable();

Employee emp = new Employee("E1001","Sky");
c.put("E1001",emp);

Then I have a JTable

Object[][] data = {

                {"", ""},

        };

String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);

I Cant seem to Figure out how to add the hashtable items into the JTable

Shiv Chand
  • 43
  • 1
  • 6

1 Answers1

1

If I'm reading this right, something like...

Object[][] data = new Object[c.size()][2];
int row = 0;
for (Object key : c.keySet()) {
    data[row][0] = key;
    Employee emp = (Employee)c.get(key);
    data[row][1] = ...; // Get name from Employee object...
    // Personally, I prefer to assign the Employee object to
    // the column of the row and use a TableCellRenderer to
    // renderer it
}

String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);

Should work...

Now, if you want a stronger relationship between the HashMap and TableModel (so you could add content to the table and it would update the HashMap), you're going to need to use an AbstractTableModel and get your hands dirty mapping the content between the requirements of the model and the HashMap

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366