1

I try to add header row to my JTable and next put table on panel.
This is my sample code :

Map <String, Float> tmpCart = new HashMap<String , Float>();
    MainPanel.removeAll();
    MainPanel.repaint();
    tmpCart = cart.GetMap();
        DefaultTableModel tab = new DefaultTableModel();
        tab.setColumnIdentifiers(new String[] {"Name", "Price"});
            for (String key : tmpCart.keySet())
            tab.addRow(new Object[] {key, tmpCart.get(key)});
        JTable jTab = new JTable(tab);
        jTab.setBounds(10, 10, 200, 200);
        jTab.setBackground(Color.orange);
        jTab.setRowHeight(25);
        JScrollPane pan =new JScrollPane(jTab);
         MainPanel.add(pan);
        // MainPanel.add(jTab);
       // pan.repaint(); 

How do I write it properly?

//answer

I try create JTable dynamically, after push button, I want get data from Hashtable, create table and put this table on panel.

First step is remove all components from panel, and next create JTable using data from Hashmap`

full function:

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
Map <String, Float> tmpCart = new HashMap<String , Float>();
MainPanel.removeAll();
MainPanel.repaint();
tmpCart = cart.GetMap();
    DefaultTableModel tab = new DefaultTableModel();
    tab.setColumnIdentifiers(new String[] {"Name", "Price"});
        for (String key : tmpCart.keySet())
        tab.addRow(new Object[] {key, tmpCart.get(key)});
    JTable jTab = new JTable(tab);
    jTab.setBounds(10, 10, 200, 200);
    jTab.setBackground(Color.orange);
    jTab.setRowHeight(25);

    // MainPanel.add(pan);
     MainPanel.add(jTab);            

}

This code works, create table and put it on panel, but doesn't set first row with column names ( text : "Names" and "Price").

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
user737065
  • 389
  • 2
  • 6
  • 9

3 Answers3

3

Can't tell exactly what you are doing from the code sample.

If you are trying to dynamically update a table on a visible GUI, then there is not need to create a new table, just reset the TableModel.

table.setModel( model );

If you need more help then post your SSCCE that demonstrates the problem.

Edit:

First step is remove all components from panel,

Why are your removing components from the panel?

If you need to "swap" panels, then use a CardLayout.

If you need to "refresh" existing components, then just reset the model as explained above.

// MainPanel.add(pan);
MainPanel.add(jTab);               

The scrollPane should be added to the GUI, not the table.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

The table header is another component that only gets added automatically when the table is inside a JScrollPane. Your best bet is to create a JScrollPane via

new JScrollPane(table)

and add that to your MainPanel.

The other option is to added the header directly wherever you want it, and for that you can do:

panel.add(table.getTableHeader())
Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
1

Try putting it into a JScrollPane.

keuleJ
  • 3,418
  • 4
  • 30
  • 51