3 Replies - 259 Views - Last Post: 01 February 2013 - 02:48 AM Rate Topic: -----

#1 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 199
  • Joined: 28-August 12

JTables, AbstractTableModels and columnNames

Posted 31 January 2013 - 02:56 AM

Hello Java programmers. I have a game that I am working on, and I am at the stage where I am displaying the player's game history. I am using a JTable and an AbstractTableModel. The thing is, however, that I have the AbstractTableModel defined in a different Java file than the JTable is. The JTable is, in fact, defined in its own class. My JTable will display, but it doesn't have the columnNames displayed even though I explicitly defined them in the AbstractTableModel and overrode a method for displaying their names. Is it possible for me to display the column names in the JTable with my code the way it is? I am using
public GameHistoryTable(GameHistoryTableModel m) 
	{
		//Set up the table here.
		table = new JTable(m);
		table.setPreferredScrollableViewportSize(new Dimension(800,120));
	}



as constructor for JTable. Here, I included all of the GameHistoryTable(), so that you can see me giving the AbstractTableModel as parameter.

This post has been edited by IceHot: 31 January 2013 - 02:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JTables, AbstractTableModels and columnNames

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: JTables, AbstractTableModels and columnNames

Posted 31 January 2013 - 03:16 AM

Can you show us the rest of your code?
Was This Post Helpful? 0
  • +
  • -

#3 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 199
  • Joined: 28-August 12

Re: JTables, AbstractTableModels and columnNames

Posted 31 January 2013 - 02:04 PM

The AbstractTableModel is defined like this:
import javax.swing.table.AbstractTableModel;

public class GameHistoryTableModel extends AbstractTableModel
{
	//This will allow us to get the information from LoadData and bring it into the
	//table.
	private LoadData l;
	private TimeToString t;
	//declaring the data as Objects...practically everything is Object
	private Object[][] data;
	//Declaring and defining the difficultyNames (to save the heap). These will get 
	//added to the data.
	private String[] difficultyNames = {"Elementary School", "Middle School", 
			"Big Adder", "Divide and Conquer", "Human Calculator"};
	//declaring and defining the columnNames
	private String[] columnNames = {"Difficulty", "Times Played", "Fastest Time",
			"Slowest Time", "Average Time", "Number Right", "Number Wrong", "Percent"};
	
	public GameHistoryTableModel() 
	{
		data = new Object[5][8];
	}
	public void setLoadData(LoadData d)
	{
		l = d;
	}
	//Defining the necessary methods of an AbstractTableModel. There are three
	//methods you must define EVERY TIME YOU USE AN ABSTRACTTABLEMODEL. They are:
	/*
	 * getRowCount()
	 * getColumnCount()
	 * getValueAt(int, int)
	 */
	public int getRowCount()
	{
		return 5;
	}
	public int getColumnCount()
	{
		return 8;
	}
	@Override
	public String getColumnName(int column)
	{
		return columnNames[column];
	}
	//Absolutely EVERYTHING is an object in Java. The developers of Java took one
	//step further to ensure that this principle would be enforced by making sure
	//that EVEN EVERY FREAKING ITEM IN THE JTABLE is an object, too.
	public Object getValueAt(int row, int column)
	{
		return data[row][column];
	}
	public void setdata()
	{
		for (int i = 0; i < getRowCount(); i++)
		{
			for (int j = 0; j < getColumnCount(); j++)
			{
				if (j == 0)
				{
					//set the data to the difficultyName
					data[i][j] = difficultyNames[i];
				}	//end case 0
				else if (j == 1)
				{
					data[i][j] = (Integer)l.gettimesplayed(i);
				}	//end case 1
				else if (j < 5)
				{
					//setting the instance of TimeToString based on the column #
					if (j == 2)
					{
						t = new TimeToString(l.getfastesttime(i));
					}
					else if (j == 3)
					{
						t = new TimeToString(l.getslowesttime(i));
					}
					else
					{
						t = new TimeToString(l.getavgtime(i));
					}
					data[i][j] = t.getformattedtime();
				}	//end cases 2 through 4
				else if (j == 5)
				{
					data[i][j] = (Integer)l.getnumright(i);
				}	//end case 5
				else if (j == 6)
				{
					data[i][j] = (Integer)l.getnumwrong(i);
				}	//end case 6
				else
				{
					data[i][j] = l.getpercent(i);
				}	//end case 7
			}	//end inner for
		}	//end outer for
	}	//end setdata
}	//end class



Is this enough?

This post has been edited by IceHot: 31 January 2013 - 11:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 199
  • Joined: 28-August 12

Re: JTables, AbstractTableModels and columnNames

Posted 01 February 2013 - 02:48 AM

UPDATE: I found some working code on the Java website: http://docs.oracle.c...ents/table.html At first, I found nothing of help in the code, but I stumbled upon something in the code called a JScrollPane. Curious, I wrote up my own example as I was tinkering with their code. I found out that the JScrollPane usually contains tables, and it is this scroll pane that gets added to the JPanel (my book doesn't cover this :dontgetit: ) I did this and my columnNames show! Now, onward to releasing Stage Two of the beta-test!

This post has been edited by IceHot: 01 February 2013 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1