File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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


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

JScrollPane scrollPane = new JScrollPane(table);


public static void main(String[] args)
MySimpleJtable frame = new MySimpleJtable();
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:
subject: JTable question - setting the column title name?
jQuery in Action, 3rd edition