5 Replies - 8729 Views - Last Post: 29 June 2010 - 06:06 PM Rate Topic: -----

#1 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

JTable Column Names Not Displaying

Posted 29 June 2010 - 03:06 PM

I followed the JTable tutorial on Sun but my column name is not displaying, only the column data is displaying. Anyone know how to fix this?

import java.lang.Math;
import java.awt.Color;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TeacherGUI extends JFrame implements ActionListener
{
  private int pWW;
  private int pWH;
  private int MAINFRAME_WINDOW_WIDTH = 800;
  private int MAINFRAME_WINDOW_HEIGHT = 600;
  
  private JTable mainTable;
  
  private JInternalFrame mainInternalFrame = new JInternalFrame("Student Profiles",false,true,false,true);
  
  private JPanel profilePanel = new JPanel();
  
  private final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  private Dimension w;
  
  public TeacherGUI()
  {
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    
    JMenu file = new JMenu("File");
    menuBar.add(file);
    
    JMenuItem quit = new JMenuItem("Quit");
    quit.setActionCommand("Quit");
    quit.addActionListener(this);
    file.add(quit);
    
    setTitle("EZ Ed");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(MAINFRAME_WINDOW_WIDTH,MAINFRAME_WINDOW_HEIGHT);
    setLocation(d.width / 2 - (int) this.getSize().width / 2, d.height / 2 - (int) this.getSize().height / 2);
    
    w = getSize();
    pWW = w.width;
    pWH = w.height;

    JDesktopPane desktop = new JDesktopPane();
    setContentPane(desktop);
    
    setLayout(null);
    
    mainTable = new JTable(new TableModel());
    
    mainTable.setSize(600,300);

    profilePanel.setSize(600,80);
    profilePanel.setBackground(Color.LIGHT_GRAY);
    
    mainInternalFrame.add(profilePanel);
    mainInternalFrame.add(mainTable);
    
    mainInternalFrame.setSize(600,380);
    mainInternalFrame.setLayout(null);
    
    mainTable.setLocation(0,100);
    
    desktop.add(mainInternalFrame);
    
    Insets insets = getInsets();
    Insets insets2 = mainInternalFrame.getInsets();
    int cF = (int)Math.sqrt(Math.pow(w.width - insets.left - insets.right,2)+Math.pow(w.height - insets.top - insets.bottom,2));
    int cI = (int)Math.sqrt(Math.pow(600 - insets2.left - insets2.right,2)+Math.pow(380 - insets2.top - insets2.bottom,2));
    int p = (cF - cI) / 2;
    mainInternalFrame.setLocation( (int) (p * Math.sin(45)), (int) (p * Math.cos(45)));
    
    setDefaultLookAndFeelDecorated(true);
    
    mainInternalFrame.setVisible(true);
    setVisible(true);
    
    ActionListener componentLocationModifier = new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        w = getSize();
        if(pWW != w.width || pWH != w.height)
        {
          
          pWW = w.width;
          pWH = w.height;
        } 
      }
    };
    
    Timer timer = new Timer(1,componentLocationModifier);
    timer.start();
    
  }
  
  private class TableModel extends AbstractTableModel
  {
    private String[] columnNames = {"Number","Student","Average","","Assignment"};
    private Object[][] data = {{1,"Test",new Integer(95),"A","IDK"}};
    
    public int getColumnCount()
    {
      return columnNames.length;
    }
    
    public int getRowCount()
    {
      return data.length;
    }
    
    public String getColumnName(int col)
    {
      return columnNames[col];
    }
    
    public Object getValueAt(int row, int col)
    {
      return data[row][col];
    }
    
    public void setValueAt(Object newValue, int row, int col)
    {
      data[row][col] = newValue;
      fireTableCellUpdated(row,col);
    }
    
    public boolean isCellEditable(int row, int col) 
    {
      if (col < 2) 
        return false;
      else 
        return true;
    }
  }
  
  public void actionPerformed(ActionEvent e)
  {
    String c = e.getActionCommand();
    
    if("Quit".equals(c))
    {
      if(JOptionPane.showConfirmDialog(this,"Do you really want to quit?","Quit",JOptionPane.YES_NO_OPTION) == 0)
        System.exit(0);
    }
  }
  
}



Is This A Good Question/Topic? 0
  • +

Replies To: JTable Column Names Not Displaying

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2778
  • View blog
  • Posts: 11,761
  • Joined: 20-September 08

Re: JTable Column Names Not Displaying

Posted 29 June 2010 - 03:14 PM

Try

mainInternalFrame.add(new JScrollPane(mainTable));

Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: JTable Column Names Not Displaying

Posted 29 June 2010 - 03:19 PM

I tried that but then the table itself disappears which is very odd.
Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: JTable Column Names Not Displaying

Posted 29 June 2010 - 03:25 PM

Have a look at DefaultTableModel or the following

Quote

JTable(Vector rowData, Vector columnNames)
Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames.


or you could perhaps try something like
JTable myTabel = new JTable(3,10); // 3 columns by 6 rows

myTable.getColumnModel().getColumn(1).setHeaderValue("New Column Name"); // change column 1's name //


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2778
  • View blog
  • Posts: 11,761
  • Joined: 20-September 08

Re: JTable Column Names Not Displaying

Posted 29 June 2010 - 03:27 PM

I suggest you only use a null layout when you really know what you're doing
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JTable Column Names Not Displaying

Posted 29 June 2010 - 06:06 PM

View Postg00se, on 29 June 2010 - 04:27 PM, said:

I suggest you only use a null layout when you really know what you're doing

That's a very good point.
Null layout if you draw yourself all your graphics if you use JComponent avoid null layout. You will have to setBounds() on all of them and for a JScrollPane will need a lot of calculations.

As far as your initial question is concerned: JTable header will not be automatically displayed if the JTable is not in a JScrollPane.
There are accessor to get them if you want to draw them "by hand".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1