• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Columns for JTable do not display

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JTable within my application and utilizing a class that extends AbstractTableModel to display the columns and data. However, the columns are not being displayed. I simply created a array of Strings and passed that array to getColumnName method do display the columns. But it is not working. Do I need to implment an instance of TableColumnModel as well? If not, any ideas on what can be wrong ? Thanks
Here's the relevant code:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
public class VerbTableModel extends AbstractTableModel {
final String[] columnNames = {"Present", "Present Subjunctive", "Future", "Conditional", "Preterite", "Imperfect"};
String[][] cellData = new String[columnNames.length][6];
public boolean isCellEditable(int row, int col){
return false;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return cellData.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
}
This code goes into my class where I display the GUI and where the table exists:
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new FlowLayout());
tm = new VerbTableModel();
JTable verbTable = new JTable(tm);
tablePanel.add(verbTable);
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to get your data to display you must implement the public Object getValueAt(int row, int column); method in order to tell the table what to render in each cell.
About your column names... Ideally you would create an instance of javax.swing.table.JTableHeader and place it in a North Border of the JTable. As a quick implementation, yuo can add your JTable in a javax.swing.JScrollPane whose default implementation is designed to handles JTables suitably.
So your VerbTableModel would contain the following method...

...and you would add your JTable to the JPanel as so.

THE JDK API also suggests the same treatment!

[ July 29, 2002: Message edited by: saager mhatre ]
 
Jerry McClain
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Saager. Adding the JScrollPane did the trick. Columns now appear. Can you send me the link of the Java API that talks about adding a scrollpane will cause the columns to appear, if u have it.
Also, I had the getValueAt method, but it seems like I forgot to paste it up there.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic