Java JTable Column headers not showing
Asked Answered
H

3

5

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);

    }

}
Heck answered 18/12, 2012 at 15:13 Comment(1)
Possible duplicate of JTable won't show column headersLuciferin
M
21

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);
Misspeak answered 18/12, 2012 at 16:5 Comment(1)
See also Adding a Table to a Container.Underwrite
D
4

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

Discouragement answered 18/12, 2012 at 15:22 Comment(3)
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.shtmlDiscouragement
thanks the thing is im using JCreator (no help or pre made graphics) since this is for a projectHeck
It is as easy as fS.add(new JScrollPane(tableS));Heronry
T
0

My problem was I set my JFrame layout to null (f.setLayout(null)). So when I removed it and added

f.add(new JScrollPane(table)). 

It worked.

Trot answered 2/12, 2020 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.