Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

swing - Java JTable Column headers not showing

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

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...