Java JTable列标题未显示 [英] Java JTable Column headers not showing

查看:112
本文介绍了Java JTable列标题未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好希望你能帮到我,我的代码(逻辑方面)很好唯一的问题是列标题没有显示在2个表格中(表格和表格,一个用于教师和一个表格)对于学生的详细信息)我如何让它们显示?

Hi guys was hoping you could help me out here, 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);

    }

}


推荐答案

这是问题所在。

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

不要直接添加表格。使用 JScrollPane 显示表格标题。

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

f.add(new JScrollPane(table))

为什么需要将表放在中JScrollPane

来自 @Dan post:


当你将组件放在 JScrollPane 的构造函数中时,你提到哪个是要应用滚动的视图。

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.

另一方面,使用add方法,只需将一个组件添加到容器中,例如将其添加到 JPanel 。这样,您就不会指定要添加滚动条的组件。

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.

编辑:
来自 javadoc (由trashgod引用评论部分)

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




此代码段中的两行执行以下操作:
使用引用
表对象的参数调用JScrollPane构造函数。这会创建一个滚动窗格作为
表的容器;该表自动添加到容器中。
调用JTable.setFillsViewportHeight来设置
fillsViewportHeight属性。当此属性为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);

这篇关于Java JTable列标题未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆