brandin
brandin

Reputation: 99

DefaultTableModel, titles wont show up. How can I fix this?

This is the start of my student panel. I've tried adding JScrollPanes to it which, from what i've read, should make it work but it just wont show the titles at all. I could be adding them in wrong but im not really sure at this point. Any help would be greatly appreciated. Thank you!

    //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

Upvotes: 0

Views: 34

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Start by taking a look at How to Use Tables and How to Use Scroll Panes

The JTable column header is displayed within the header section of a JScrollPane

JTable in a JScrollPane

You need to wrap the JTable in a JScrollPane, something like...

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

for example

Upvotes: 1

Related Questions