Page 1 of 1

How to create a simple JTable creating JTable using your own TableModel Rate Topic: -----

#1 Unknown Hero  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 51
  • Joined: 04-September 07

Posted 08 September 2008 - 10:39 AM

I've written this post as one of my answers to user's question. Then I realised it was very good, and so I decided to put it here. Hope you like it and you learn something from it!



It's always good to have your own model (and renderer) which will manage the data in your table.
I would advise you to first create new class that implements TableModel.
Now you will have bunch of unimplemented methods. Method names explain to you what each method does. Here's an example of how you can implement it (note this example will not work for your problem):
package masina.muzika.viktor.fromTAG;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ModelTablice implements TableModel
{
	/**
	 * List of event listeners. These listeners wait for something to happen
	 * with the table so that they can react. This is a must!
	 */
	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	
	/**
	 * Some random data I've added, so that you can more easy understand how
	 * this thing works.
	 */
	private ArrayList<Object[]> data = new ArrayList<Object[]>();
	
	/**
	 * Name is self explanatory :)/>
	 */
	private String[] columnNames = new String[] {" ", "Old name", "New name"};
	
	/**
	 * Adds a listener to the list that is notified each time a change to the
	 * data model occurs.
	 * 
	 * @param l
	 *			the TableModelListener
	 */
	@Override
	public void addTableModelListener(TableModelListener l)
	{
		if (listeners.contains(l))
			return;
		listeners.add(l);
	}
	
	/**
	 * Returns the most specific superclass for all the cell values in the
	 * column. This is used by the JTable to set up a default renderer and
	 * editor for the column.
	 * 
	 * @param columnIndex
	 *			the index of the column
	 * @return the common ancestor class of the object values in the model.
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex)
	{
		switch (columnIndex)
		{
			case 0:
				return Boolean.class;
			case 1:
				return String.class;
			case 2:
				return String.class;
		}
		return null;
	}
	
	/**
	 * Returns the number of columns in the model. A JTable uses this method to
	 * determine how many columns it should create and display by default.
	 * 
	 * @return the number of columns in the model
	 * @see #getRowCount
	 */
	@Override
	public int getColumnCount()
	{
		return 3;
	}
	
	/**
	 * Returns the name of the column at columnIndex. This is used to initialize
	 * the table's column header name. Note: this name does not need to be
	 * unique; two columns in a table can have the same name.
	 * 
	 * @param columnIndex
	 *			the index of the column
	 * @return the name of the column
	 */
	@Override
	public String getColumnName(int columnIndex)
	{
		return columnNames[columnIndex];
	}
	
	/**
	 * Returns the number of rows in the model. A JTable uses this method to
	 * determine how many rows it should display. This method should be quick,
	 * as it is called frequently during rendering.
	 * 
	 * @return the number of rows in the model
	 * @see #getColumnCount
	 */
	@Override
	public int getRowCount()
	{
		return data.size();
	}
	
	/**
	 * Returns the value for the cell at columnIndex and rowIndex.
	 * 
	 * @param rowIndex
	 *			the row whose value is to be queried
	 * @param columnIndex
	 *			the column whose value is to be queried
	 * @return the value Object at the specified cell
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		return data.get(rowIndex)[columnIndex];
	}
	
	/**
	 * Returns true if the cell at rowIndex and columnIndex is editable.
	 * Otherwise, setValueAt on the cell will not change the value of that cell.
	 * 
	 * @param rowIndex
	 *			the row whose value to be queried
	 * @param columnIndex
	 *			the column whose value to be queried
	 * @return true if the cell is editable
	 * @see #setValueAt
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		if (columnIndex == 1)
			return false;
		
		return true;
	}
	
	/**
	 * Removes a listener from the list that is notified each time a change to
	 * the data model occurs.
	 * 
	 * @param l
	 *			the TableModelListener
	 */
	@Override
	public void removeTableModelListener(TableModelListener l)
	{
		listeners.remove(l);
	}
	
	/**
	 * Sets the value in the cell at columnIndex and rowIndex to value.
	 * 
	 * @param value
	 *			the new value
	 * @param rowIndex
	 *			the row whose value is to be changed
	 * @param columnIndex
	 *			the column whose value is to be changed
	 * @see #getValueAt
	 * @see #isCellEditable
	 */
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex)
	{
		data.get(rowIndex)[columnIndex] = value;
		TableModelEvent dogadjaj = new TableModelEvent(this, rowIndex,
				rowIndex, columnIndex, TableModelEvent.UPDATE);
		/*
		 * Table has been changed, you must inform listeners about that, because
		 * you won't see any change otherwise.
		 */
		for (TableModelListener l: listeners)
			l.tableChanged(dogadjaj);
	}
	
}



NOTE: I copy-pasted many comments from TableModel interface.

After this, you create a new JTable using this model:
ModelTablice model = new ModelTablice();
JTable table = new JTable(model);

Adding and everything you can do by adding to the model (model is the "data keeper", table is only "data viewer").
Just add one more method to your TableModel:
/**
	 * This method is used for adding a new row into your JTable
	 * 
	 * @param checked
	 *						first column value
	 * @param oldName
	 *						second column value
	 * @param newName
	 *						third column value
	 */
	public void addRow(boolean checked, String oldName, String newName)
	{
		data.add(new Object[] {
						checked,
						oldName.substring(oldName
								.lastIndexOf(File.separatorChar) == -1 ? 0
								: oldName.lastIndexOf(File.separatorChar) + 1),
						newName.substring(newName
								.lastIndexOf(File.separatorChar) == -1 ? 0
								: newName.lastIndexOf(File.separatorChar) + 1)});
		TableModelEvent event = new TableModelEvent(this, podaci.size() - 1,
				podaci.size() - 1, TableModelEvent.ALL_COLUMNS,
				TableModelEvent.INSERT);
		for (TableModelListener l: listeners)
			l.tableChanged(event);
	}


Examine the codes I've written, because those are very simple and yet very usefull.

Is This A Good Question/Topic? 2
  • +

Replies To: How to create a simple JTable

#2 lucianoefe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 08

Posted 09 November 2008 - 08:53 PM

package masina.muzika.viktor.fromTAG;

this package was the only error my Netbeans 6.0 notifies me with. any ideas as to how i can surpass this? wanna c ur program run.... pls reply.
Was This Post Helpful? 0
  • +
  • -

#3 dkheartsag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-February 09

Posted 18 March 2009 - 11:19 PM

View Postlucianoefe, on 9 Nov, 2008 - 07:53 PM, said:

package masina.muzika.viktor.fromTAG;

this package was the only error my Netbeans 6.0 notifies me with. any ideas as to how i can surpass this? wanna c ur program run.... pls reply.

Hii,
i want to use this table to set score of my slot game bored application,besically i want to know how i can print data from system.out.println(data).i want to print this data to table how i can do this can u suggest me r give me sample code for that .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1