Some questions about JTable. (row-based design, displaying headers or not, )

Posted on 2003-02-27
Medium Priority
Last Modified: 2008-02-01
I want to display a list of n pairs (x_value, y_value)
in a JTable with 2 rows (and n+1 columns) in a matter

x_value     x1     x2     x3     ...     xn
y_value     y1     y2     y3     ...     yn

Hence what I needed, would be a JTable with specifying
types for the rows rather than columns.
In addition the y-values should be editable while the
x-values are not.

1. Is it possible to specify row headers?

2. What is the standard method to suppress column headers?

3. Why are column headers shown, if adding a JTable to
some Layout wrapping it with a JScrollPane, while
headers are not shown, when not using JScrollPane.
Is it a Java bug?

Thank you very much!
Question by:Olaf_Bloemer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

António Sargento earned 150 total points
ID: 8035045
> 1. Is it possible to specify row headers?

Not exactly. You could simulate row headers by not allowing edit them.

> 2. What is the standard method to suppress column headers?

No. You should use TableModel class.

For further details see.


Here a small example program that do what you require

public class SimpleTableModel extends AbstractTableModel
  Object[][] data = {
      {"X", new Float(1.1), new Float(0.1), new Float(3.4), new Float(1.5)},
      {"Y", new Float(1.1), new Float(0.1), new Float(3.4), new Float(1.5)}};

  public SimpleTableModel()
  public int getColumnCount()
    return 5;
  public Object getValueAt(int rowIndex, int columnIndex)
    return data[rowIndex][columnIndex];
  public int getRowCount()
    return data.length;

  public boolean isCellEditable(int row, int col)
  { return col!=0; }

  public String getColumnName(int col)
  { return null; }

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class SimpleTableDemo extends JFrame {
    private boolean DEBUG = true;

    public SimpleTableDemo() {

        final JTable table = new JTable(new SimpleTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this window.
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));

    public static void main(String[] args) {
        SimpleTableDemo frame = new SimpleTableDemo();

Author Comment

ID: 8040270
Thank you for your answers and the example!
But how can I get, say, cells with right alignment
for the y_values while staying with left alignment
for the x_values?

Expert Comment

by:António Sargento
ID: 8040783
You could do that by defining your own CellRender.
In http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
there was several examples how could you define a cell render.

Author Comment

ID: 8055944
Thank you for your efforts.
It's not really satisfying that there no classes
like TableRow and TableRowModel, but I'll try to find
a systematic work-around...

Thanks again,
best regards,


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question