【问题标题】:DefaultTableModel, titles wont show up. How can I fix this?DefaultTableModel,标题不会显示。我怎样才能解决这个问题?
【发布时间】:2015-10-29 21:48:18
【问题描述】:

这是我的学生小组的开始。我已经尝试将 JScrollPanes 添加到它,从我读过的内容来看,它应该可以使它工作,但它根本不会显示标题。我可能把它们加错了,但我现在还不确定。任何帮助将不胜感激。谢谢!

    //Start of student panel
    JPanel stuPanel = new JPanel();
    tabbedPane.addTab("Student", stuPanel);
    JButton stuButton = new JButton("Add Student");
    Object[] stuColumnNames = {"First Name", "Last Name", "Major", "Minor", "GPA"};

    JTable stuTable = new JTable(new DefaultTableModel(data, stuColumnNames)){
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        //Making it so the cells I want are editable.
        @Override
        public boolean isCellEditable(int row, int column) {
            if (column == 0){
                return false;
            }
            else    
                return true;
        }
    };


    DefaultTableModel stuModel = (DefaultTableModel)stuTable.getModel();
    for(Student tmpS : stuList){
        stuModel.addRow(new Object[]{tmpS.getFirstName(),tmpS.getLastName(), tmpS.getMajor(), tmpS.getMinor(), tmpS.getGpa()});
    }

    stuButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String fName,lName,major,minor;
            float gpa;
            System.out.println("Please enter a first name:");
            fName = in.nextLine();
            System.out.println("Please enter a last name:");
            lName = in.nextLine();
            System.out.println("Please enter a major:");
            major = in.nextLine();
            System.out.println("Please enter a minor:");
            minor = in.nextLine();
            System.out.println("Please enter a GPA:");
            gpa = in.nextFloat();
            in.nextLine();
            stuList.add(new Student(fName, lName, major, minor, gpa));
            stuModel.addRow(new Object[]{fName, lName, major, minor, gpa});
            stuModel.fireTableDataChanged();
        }
    });

    GroupLayout gl_stuPanel = new GroupLayout(stuPanel);
    gl_stuPanel.setHorizontalGroup(
        gl_stuPanel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_stuPanel.createSequentialGroup()
                .addGroup(gl_stuPanel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_stuPanel.createSequentialGroup()
                        .addGap(155)
                        .addComponent(stuButton))
                    .addGroup(gl_stuPanel.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(stuTable, GroupLayout.PREFERRED_SIZE, 408, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    gl_stuPanel.setVerticalGroup(
        gl_stuPanel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_stuPanel.createSequentialGroup()
                .addContainerGap()
                .addComponent(stuButton)
                .addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(stuTable, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
    );
    stuPanel.setLayout(gl_stuPanel);
    //End of student panel

【问题讨论】:

    标签: java swing jtable columnheader


    【解决方案1】:

    首先查看How to Use TablesHow to Use Scroll Panes

    JTable 列标题显示在 JScrollPane 的标题部分中

    您需要将JTable 包装在JScrollPane 中,例如...

    JScrollPane scrollPane = new JScrollPane(stuTable);
    gl_stuPanel.setVerticalGroup(
        gl_stuPanel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_stuPanel.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane)
                .addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
    );
    

    例如

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2015-03-31
      相关资源
      最近更新 更多