简体   繁体   English

Java JTable 列标题未显示

[英]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).我的代码(逻辑方面)都很好,唯一的问题是列标题没有出现在 2 个表(表和表 S,一个用于教师,一个用于学生详细信息)中。 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);

    }

}

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.使用JScrollPane显示表格标题。

f.add(new JScrollPane(table))

Why do you need to put the table in the JScrollPane ?为什么需要将表放在JScrollPane

From @Dan post:来自@Dan帖子:

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.当您将组件放在JScrollPane的构造函数中时,您会提到哪个是滚动应用到的视图。

On the other side, using the add method, you just add a component to a container, like adding it to a JPanel .另一方面,使用 add 方法,您只需将组件添加到容器中,就像将其添加到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)编辑:来自javadoc (在评论部分被trashgod引用)

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.此代码段中的两行执行以下操作: 使用引用表对象的参数调用 JScrollPane 构造函数。 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.调用 JTable.setFillsViewportHeight 来设置 fillsViewportHeight 属性。 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.当此属性为 true 时,表格会使用容器的整个高度,即使表格没有足够的行来使用整个垂直空间。 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);

如果我没记错的话,你应该在它周围放一个容器(例如JScrollPane )。

My problem was I set my JFrame layout to null (f.setLayout(null)) .我的问题是我将JFrame布局设置为null (f.setLayout(null)) So when I removed it and added所以当我删除它并添加

f.add(new JScrollPane(table)). 

It worked.有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM