• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

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

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
like

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!
0
Olaf_Bloemer
Asked:
Olaf_Bloemer
  • 2
  • 2
1 Solution
 
António SargentoChief ArchitectCommented:
> 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.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

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() {
        super("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) {
                    printDebugData(table);
                }
            });
        }

        //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) {
                System.exit(0);
            }
        });
    }

    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));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    public static void main(String[] args) {
        SimpleTableDemo frame = new SimpleTableDemo();
        frame.pack();
        frame.setVisible(true);
    }
}
0
 
Olaf_BloemerAuthor Commented:
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?
0
 
António SargentoChief ArchitectCommented:
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.
0
 
Olaf_BloemerAuthor Commented:
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,

Olaf
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now