Java JTable Column headers not showing

29,835

Solution 1

Here is the problem.

JTable table = new JTable (data, columnNames);
f.add(table);

Do not add the table directly. Use JScrollPane to show the table headers.

f.add(new JScrollPane(table))

Why do you need to put the table in the JScrollPane?

From @Dan post:

When you put the component inside the constructor of a JScrollPane, you mention which is the view to which the scroll to be applied for.

On the other side, using the add method, you just add a component to a container, like adding it to a JPanel. This way, you don't specify the component to add the scroll bars to.

EDIT: From javadoc (cited by trashgod in the comment section)

Here is typical code for creating a scroll pane that serves as a container for a table:

JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true); 

The two lines in this snippet do the following: The JScrollPane constructor is invoked with an argument that refers to the table object. This creates a scroll pane as a container for the table; the table is automatically added to the container. JTable.setFillsViewportHeight is invoked to set the fillsViewportHeight property. When this property is true the table uses the entire height of the container, even if the table doesn't have enough rows to use the whole vertical space. This makes it easier to use the table as a drag-and-drop target. The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled.

If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:

container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);

Solution 2

If I remember correctly, you should put a container (e.g. a JScrollPane) around it.

Share:
29,835
Matthew Cassar
Author by

Matthew Cassar

Updated on February 02, 2021

Comments

  • Matthew Cassar
    Matthew Cassar about 3 years

    My code (Logic-wise) is all good, the only problem is that the column headers do not show up in the 2 tables (table and tableS, one for teacher and one for student details). How do I make them show?

            import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    
    public class Display
    {
        ArrayList<Student> stud1 = new ArrayList<Student>();
        ArrayList<SubjectTeacher> sTeach1 = new ArrayList<SubjectTeacher>();
    
        //For Display Teachers
        private  JTable table;
        private  JFrame f;
        private int i;
    
        //For DisplayStudents
        private JTable tableS;
        private JFrame fS;
        private int iS;
    
    
        //Displays Teachers
        public void displayTeachers()
        {
    
                f = new JFrame("Teachers");
    
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500,400);
                f.setVisible(true);
    
    
    
            String[] columnNames =  {"Name", "Surname", "ID", "Pay", "Subject"};
    
            sTeach1 = Stores.getTeach();
            i = sTeach1.size();
    
    
            Object[][] data = new Object[i+1][5];
    
    
    
                for (int j = 0; j<sTeach1.size(); j++)
                {
                    {
                        SubjectTeacher myTeach = sTeach1.get(j);
                         data[j][0] = myTeach.getName();
                         data[j][1] = myTeach.getSurname();
                         data[j][2] = myTeach.getID();
                         data[j][3] = myTeach.getPay();
                         data[j][4] = myTeach.getSubjectID();
                    };
                }
    
    
    
    
        JTable table = new JTable (data, columnNames);
        f.add(table);
    
        }
    
    
    
        //Displays Students
        public void displayStudents()
        {
    
            System.out.println(stud1.size());
    
                fS = new JFrame("Students");
    
                fS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fS.setSize(700,600);
                fS.setVisible(true);
    
    
    
            String[] colNames =  {"Name", "Surname", "ID", "Computer", "Physics", "Maths", "Tests", "Passes", "Fails"};
    
    
            stud1 = Stores.getStud();
            iS = stud1.size();
    
            Object[][] info = new Object[iS+1][9];
    
    
    
    
            for (int j = 0; j<stud1.size(); j++)
                {
                    {
    
    
                        Student myStud = stud1.get(j);
    
                     info[j][0] = myStud.getName();
                     info[j][1] = myStud.getSurname();
                     info[j][2] = myStud.getID();
                     info[j][3] = myStud.getSubject(1);
                     info[j][4] = myStud.getSubject(2);
                     info[j][5] = myStud.getSubject(3);
                     info[j][6] = myStud.getTestsTaken();
                     info[j][7] = myStud.getPasses();
                     info[j][8] = myStud.getFails();
                    };
                }
    
    
    
    
        JTable tableS = new JTable (info, colNames);
        fS.add(tableS);
    
        }
    
    }
    
  • Jeroen Vannevel
    Jeroen Vannevel over 11 years
    I'm not sure if this is Eclipse or the plugin I use (Jigloo), but I can just rightclick the table and choose for "Surround by container". This link should set you on your way regardless: roseindia.net/java/example/java/swing/ScrollableJTable.shtml
  • Matthew Cassar
    Matthew Cassar over 11 years
    thanks the thing is im using JCreator (no help or pre made graphics) since this is for a project
  • mavroprovato
    mavroprovato over 11 years
    It is as easy as fS.add(new JScrollPane(tableS));
  • trashgod
    trashgod over 11 years