• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable question - setting the column title name?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic