File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable question - setting the column title name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable question - setting the column title name?" Watch "JTable question - setting the column title name?" New topic
Author

JTable question - setting the column title name?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi everyone,

can anyone tell me how to change the column (header or title ) name (a,b,c,d) in my code below to my own creation, say: name, age, gpa, address, color, food ???


also, any (design) idea of how to insert/update this "matrics'" content into a database (so later the same information can be retrieved in the right order the user inserted them???)


Thanks
Ppr


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


public class MySimpleJtable extends JFrame
{
JTable table;
DefaultTableModel tableModel;

public MySimpleJtable()
{
table = new JTable(10, 5);
tableModel = (DefaultTableModel)table.getModel();
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);

}

public static void main(String[] args)
{
MySimpleJtable frame = new MySimpleJtable();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
There are a couple of ways:
1. Use constructor JTable(Object[][] rowData, Object[] columnNames) or
JTable(Vector rowData, Vector columnNames)

2. You can create your own table model class by extending AbstrractTableModel
and override all neccessary methods. One of them is getColumnName(int col).


SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
sasi kala
Ranch Hand

Joined: Dec 17, 2004
Posts: 68
Hi peter,

Use this code....

Hope this helps you.
All The Best.
[ March 24, 2005: Message edited by: sasi kala ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable question - setting the column title name?
 
Similar Threads
JTable problem
JTable
Jtable Column Heading Text
JTable ........
Jtable filled with JButtons each having their own ImageIcon from a SQL server