Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable question - setting the column title name?

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi peter,

Use this code....

Hope this helps you.
All The Best.
[ March 24, 2005: Message edited by: sasi kala ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic