5 Replies - 908 Views - Last Post: 25 April 2012 - 10:01 PM Rate Topic: -----

#1 Gwythian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-April 12

JTable Column Headers

Posted 25 April 2012 - 07:22 AM

Hi, I'm working on making a GUI table in Java for my class project, and I'm not able to make the columns appear correctly.

My code is:

import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
import java.awt.event.*;



public class GUIInterface extends JFrame
{
  private final static int NUMBER_OF_STUDENTS = Database.NUMBER_OF_STUDENTS;
  
  
  GUIInterface()
  {
     JFrame studentGUI = new JFrame("Student Registration");
     studentGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     String[][] transferArray = Program.studentsData; 
     String[] columnNames = {"First Name", "Last Name", "Address", "Credit Hours"};
     
     JTable studentTable = new JTable(transferArray, columnNames);
     studentTable.setOpaque(true);
     
     studentTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
     studentTable.setFillsViewportHeight(true);
     
     JScrollPane scrollPane = new JScrollPane(studentTable);
     
     add(scrollPane);
     
     studentGUI.setContentPane(studentTable);  
     studentGUI.pack();
     studentGUI.setVisible(true);
  }
}


When it runs, all that shows is in the attached picture. It shows the data I wanted in the table, but ignores the column names completely.

What am I missing?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: JTable Column Headers

#2 Gwythian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-April 12

Re: JTable Column Headers

Posted 25 April 2012 - 08:54 AM

I figured it out. Had to do with everything being in the same class. This can be deleted whenever.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JTable Column Headers

Posted 25 April 2012 - 02:10 PM

View PostGwythian, on 25 April 2012 - 11:54 AM, said:

Had to do with everything being in the same class.

I seriously doubt :) Don't see how making 2 class files would have fix your problem, unless you simply fix the actual bug by coincidence.
Was This Post Helpful? 0
  • +
  • -

#4 Gwythian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-April 12

Re: JTable Column Headers

Posted 25 April 2012 - 09:00 PM

I think it had to do with it not being added the the panel or frame correctly. I'm brand spankin new at this GUI stuff, so I'm shooting in the dark. :D
Was This Post Helpful? 0
  • +
  • -

#5 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: JTable Column Headers

Posted 25 April 2012 - 09:02 PM

So just so people who google this have an answer to your problem, you should post how you solved it. I'm guessing you put your JTable inside a JScrollPane?
Was This Post Helpful? 0
  • +
  • -

#6 Gwythian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-April 12

Re: JTable Column Headers

Posted 25 April 2012 - 10:01 PM

Ah, sorry about that ianian112.

What I did was create a JFrame, then a JPanel, created the JTable, then the JScrollPane passing it the JTable, then added the scrollPane to the JPanel, set the panel to Opaque, added the JPanel to the JFrame, then packed it, setting the visibility to True.

I also changed the class to just create a new JFrame rather than extending the JFrame.

I hope that helps. I'm still kinda new and learning, so if there's anything I can do to make it more clear, I'd be happy to do what I can.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1