andrea
andrea

Reputation: 1

Names of my JTable columns not showing

I created a JFrame with Java Swing using a BorderLayout. Both east and west layout are fixed with some components, while BorderLayout at the center can change depending on what the user does. At the beginning there is a picture,

panelDinamycCenter = new JPanel ();   
JScrollPane = new JScrollPane (panelDinamycCenter);

panelDinamycCenter.add (new JLabel ( "", new Imagelcon ( "......."),  JLabel.CENTER));

jFrame.getContentPane (). add (JScrollPane, BorderLayout.CENTER);

Then a data entry screen, for which I used a GridBagLayout. In order to remove the picture and to insert the new screen I used this:

panelDinamycCenter.removeAll (), 

then I add the various components. After

setVisible (true);

I have now created a JTable, to be included in panelDinamycCenter,

panelDinamycCenter.removeAll ();

String [] [] matrixValori = new String  [arrayOggCreaJTableDeroghe.length] [arrayJTableNomeColumnDeroghe.length];

for (int i = 0; i <arrayOggCreaJTableDeroghe.length; i ++) {
    matrixValori [i] = arrayOggCreaJTableDeroghe [i] .creaArrayString ();
}

DefaultTableModel model = new DefaultTableModel (matrixValori,    arrayJTableNomeColumnDeroghe);
JTable JTable = new JTable (model);
jTable.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);

panelDinamycCenter.add (JTable);

jFrame.setVisible (true);

All right, but I can not see the names of the columns. Why?

Upvotes: 0

Views: 68

Answers (1)

trashgod
trashgod

Reputation: 205785

As shown in How to Use Tables: Adding a Table to a Container, "The scroll pane automatically places the table header at the top of the viewport." At a minimum, you need to replace the JScrollPane removed by removeAll():

JTable jTable = new JTable(model);
panelDinamycCenter.add(new JScrollPane(jTable));

Among alternatives, consider these:

  • Add the table header to PAGE_START, as suggested here.
  • Instead of removing and restoring, use CardLayout to switch between views.

Upvotes: 1

Related Questions