We help IT Professionals succeed at work.

Java: why aren't my headers not showing?

epifanio67
epifanio67 asked
on
Hello experts,

Why aren't my headers not showing?

thanks so much for all of your help...

regarsd
package com.tablemodeldemo.ui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TestTableModelDemo {

	public static void main(String[] args) {
		JFrame frame = new JFrame("TestTableModelDemo");
		String headers[] = { "English", "Japenese" };
		TableModelDemo model = new TableModelDemo(10, headers);
		JTable table = new JTable(model);
		
		model.setValueAt("one", 0, 0);
		model.setValueAt("two", 0, 1);
		model.setValueAt("five", 1, 0);
		model.setValueAt("roku - \u516D", 1, 1);
		model.setValueAt("seven", 2, 0);
		model.setValueAt("hachi - \u516B", 2, 1);
		
		JScrollPane scrollPane = new JScrollPane(table);
	    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
	    frame.setSize(300, 150);
	    frame.setVisible(true);

	}

}


//**************************

package com.tablemodeldemo.ui;

import java.awt.Point;
import java.util.Hashtable;

import javax.swing.table.AbstractTableModel;

public class TableModelDemo extends AbstractTableModel{
	
	private static final long serialVersionUID = 1L;
	private Hashtable lookUp;
	private final int rows;
	private final int columns;
	private final String headers[];
	
	@SuppressWarnings("rawtypes")
	public TableModelDemo(int rows, String columnHeaders[]){
		if ((rows < 0) || (columnHeaders == null)){
			throw new IllegalArgumentException("Invalid row count/columnHeaders...");
		}
		this.rows = rows;
		this.columns = columnHeaders.length;
		headers = columnHeaders;
		lookUp = new Hashtable();
		
	}

	@Override
	public int getColumnCount() {
		return columns;
	}

	@Override
	public int getRowCount() {
		return rows;
	}

	@Override
	public Object getValueAt(int row, int column) {
		return lookUp.get(new Point(row, column));
	}
	
	public void setValueAt(Object value, int row, int column){
		if((rows < 0) || (columns <0)){
			throw new IllegalArgumentException("Invalid row/column setting... ");
		}
		if((row < rows)&&(column < columns)){
			lookUp.put(new Point(row, column), value);
		}
	}

}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
//package com.tablemodeldemo.ui;

import java.awt.*;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TestTableModelDemo {

	public static void main(String[] args) {
		JFrame frame = new JFrame("TestTableModelDemo");
		String headers[] = { "English", "Japenese" };
		TableModelDemo model = new TableModelDemo(10, headers);
		JTable table = new JTable(model);

		model.setValueAt("one", 0, 0);
		model.setValueAt("two", 0, 1);
		model.setValueAt("five", 1, 0);
		model.setValueAt("roku - \u516D", 1, 1);
		model.setValueAt("seven", 2, 0);
		model.setValueAt("hachi - \u516B", 2, 1);

		JScrollPane scrollPane = new JScrollPane(table);
	    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
	    frame.setSize(300, 150);
	    frame.setVisible(true);

	}

}


//**************************
  /*
package com.tablemodeldemo.ui;

import java.awt.Point;
import java.util.Hashtable;

import javax.swing.table.AbstractTableModel;

*/

 class TableModelDemo extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Hashtable lookUp;
	private final int rows;
	private final int columns;
	private final String headers[];

	@SuppressWarnings("rawtypes")
	public TableModelDemo(int rows, String columnHeaders[]){
		if ((rows < 0) || (columnHeaders == null)){
			throw new IllegalArgumentException("Invalid row count/columnHeaders...");
		}
		this.rows = rows;
		this.columns = columnHeaders.length;
		headers = columnHeaders;
		lookUp = new Hashtable();

	}


	public int getColumnCount() {
		return columns;
	}


	public int getRowCount() {
		return rows;
	}

     public String getColumnName(int column) {
         return headers[column];
       }
     

	public Object getValueAt(int row, int column) {
		return lookUp.get(new Point(row, column));
	}

	public void setValueAt(Object value, int row, int column){
		if((rows < 0) || (columns <0)){
			throw new IllegalArgumentException("Invalid row/column setting... ");
		}
		if((row < rows)&&(column < columns)){
			lookUp.put(new Point(row, column), value);
		}
	}

}

Open in new window

Awarded 2011
Awarded 2011
Commented:


You should add this method to class  TableModelDemo :

 public String getColumnName(int column) {
         return headers[column];
       }

Open in new window

Awarded 2011
Awarded 2011

Commented:
The code on ID:37252078
with added method getColumnName
shows the headers OK

Author

Commented:
thank you so much...

so, the get methods get called at runtime?

thanks again...