6 Replies - 48813 Views - Last Post: 21 October 2011 - 12:20 PM Rate Topic: -----

#1 Rassti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-September 08

display ArrayList string object in JTable

Posted 01 September 2008 - 03:51 AM

i've created class Person:
public class Person
{
	private int age; 
	private String name;
	private String surname;
	private String id;
   
	public Person(String newN, String newS,String i, int a)
	{
	name = newN;
	surname = newS;
	id = i;
	age = a;
	}
	
	public Person(String newN, String newS, String id, int a, )
	{
	this(newN,newS,a,i);
	}
	public int getAge()
	{
	return age;
	}
	public String getName()
	{
	return name;
	}
	public String getSurname()
	{
	return surname;
	}
   
	public String getId()
	{
	return id;
	}
	public void setName(String newN)
	{
	name = newN;
	}
	public void setLastName(String surname)
	{
	surname = newS;
	}
	
	public void setId(String id)
	{
	id = i;
	}
	public String toString()
	{
	String s = "Name: " + getName() + " " + getSurname() + "\nId: " + getId() + "\nAge: " + getAge();
	return s;
	}
}

then to add persons i used ArrayList:

public class PersonAdd
{
	private ArrayList<Person> pr = null;
   
	public Person() 
	{
		pr = new ArrayList<Prisoner>();
	
	}
	public void addPerson(Person person) 
	{
		pr.add(person);
	}


...............
And, now i want to display all persons in table using JTable.
Please, help with it...

Is This A Good Question/Topic? 0
  • +

Replies To: display ArrayList string object in JTable

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: display ArrayList string object in JTable

Posted 01 September 2008 - 06:51 AM

Here is a JTable tutorial to get you started.
And next time please use code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#38 Unknown Hero  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: display ArrayList string object in JTable

Posted 01 September 2008 - 07:15 AM

View PostRassti, on 1 Sep, 2008 - 03:51 AM, said:

i've created class Person:
public class Person
{
	private int age; 
	private String name;
	private String surname;
	private String id;
   
	public Person(String newN, String newS,String i, int a)
	{
	name = newN;
	surname = newS;
	id = i;
	age = a;
	}
	
	public Person(String newN, String newS, String id, int a, )
	{
	this(newN,newS,a,i);
	}
	public int getAge()
	{
	return age;
	}
	public String getName()
	{
	return name;
	}
	public String getSurname()
	{
	return surname;
	}
   
	public String getId()
	{
	return id;
	}
	public void setName(String newN)
	{
	name = newN;
	}
	public void setLastName(String surname)
	{
	surname = newS;
	}
	
	public void setId(String id)
	{
	id = i;
	}
	public String toString()
	{
	String s = "Name: " + getName() + " " + getSurname() + "\nId: " + getId() + "\nAge: " + getAge();
	return s;
	}
}

then to add persons i used ArrayList:

public class PersonAdd
{
	private ArrayList<Person> pr = null;
   
	public Person() 
	{
		pr = new ArrayList<Prisoner>();
	
	}
	public void addPerson(Person person) 
	{
		pr.add(person);
	}


...............
And, now i want to display all persons in table using JTable.
Please, help with 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.


Was this post helpfull? =>

This post has been edited by Unknown Hero: 01 September 2008 - 07:15 AM

Was This Post Helpful? 3

#39 givemedata  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-October 11

Re: display ArrayList string object in JTable

Posted 18 October 2011 - 12:55 AM

Hello Unknown Hero,

First of all: Thank you very much for this post. Was not sure on how to proceed when reading the Java example on Oracle site. With your post I got it working like a charm (almost).

The only problem I have, is with the "setValueAt" method. How can I get this to work with the give Person class? I have tried but am kind of stuck. I also found a few other examples, and am trying to cramp their working into my tiny brain ;-)

Can I replace the original public void setValueAt(Object value, int rowIndex, int columnIndex)
with an alternative public void setValueAt(Person value, int rowIndex, int columnIndex)
or will I need to use the original and do type casting?

Regards,

GiveMeData
Was This Post Helpful? 0
  • +
  • -

#40 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: display ArrayList string object in JTable

Posted 18 October 2011 - 03:53 AM

I think this is exactly what you need

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 0
  • +
  • -

#41 givemedata  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-October 11

Re: display ArrayList string object in JTable

Posted 19 October 2011 - 03:50 AM

Hi pbl,

Thanx for your reply. After posting, I did some other tests and got it working on my own. I might give your solution a try when refining my current code. Got a refresh issue now, but I will do some more searching before posting about that ;-).

Regards,

GiveMeData
Was This Post Helpful? 0
  • +
  • -

#42 givemedata  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-October 11

Re: display ArrayList string object in JTable

Posted 21 October 2011 - 12:20 PM

Found it, using code above refreshes for the last line. The code I now use, refreshes on the required index.

It's probably not of any use to anyone, but I keep reading that people want to see the final code, so if there is anyone out there, here goes ;-)
import java.util.ArrayList;

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

import javax.swing.table.AbstractTableModel;

public class TableModelPerson extends AbstractTableModel
{
	/**
	 * 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>();

	private PersonList iPersonList;

	private String[] iColumnNames =
	{
		"Identifier",
		"Lastname",
		"Firstname"
	};

	public TableModelPerson( PersonList pPersonList )
	{
		this.iPersonList = pPersonList;
	}

	/**
	 * 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;
		//return iColumnNames.size( );
	}

	/**
	 * 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
	 */
	public String getColumnName( int columnIndex )
	{
		return iColumnNames[ columnIndex ];
	}

	/**
	 * 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 String.class;
			case 1: return String.class;
			case 2: return String.class;
		}

		return null;
	}

	/**
	 * 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 this.iPersonList.size( );
	}

	/**
	 * 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 == 0 )
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	/**
	 * 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 row, int col )
	{
		switch(col)
		{
			case 0 : return this.iPersonList.get( row ).getIdentifier( );

			case 1 : return this.iPersonList.get( row ).getLastname( );
			case 2 : return this.iPersonList.get( row ).getFirstname( );

			default : throw new RuntimeException("no such column");
		}
	}

	/**
	 * 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( String pIdentifier, String pLastname, String pFirstname )
	{
		this.iPersonList.add( pIdentifier, pLastname, pFirstname );

		TableModelEvent event = new TableModelEvent( this, this.iPersonList.size() - 1, this.iPersonList.size() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}

	/**
	 * 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 updateRow( int pIndex, String pIdentifier, String pLastname, String pFirstname )
	{
		this.iPersonList.set( pIndex, pIdentifier, pLastname, pFirstname );

		TableModelEvent event = new TableModelEvent( this, pIndex, pIndex, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}

	/**
	 * 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 moveUp( int pIndex )
	{
		this.iPersonList.swap( pIndex, (pIndex - 1) );

		TableModelEvent event = new TableModelEvent( this, pIndex, (pIndex - 1), TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}

	/**
	 * 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 moveDown( int pIndex )
	{
		this.iPersonList.swap( pIndex, (pIndex + 1) );

		TableModelEvent event = new TableModelEvent( this, pIndex, (pIndex + 1), TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}

	/**
	 * 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 removeRow( int pIndex )
	{
		this.iPersonList.remove( pIndex );

		TableModelEvent event = new TableModelEvent( this, pIndex, pIndex, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}

	/**
	 * 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 pTableModelListener )
	{
		if( listeners.contains( pTableModelListener ) )
		{
			return;
		}

		listeners.add( pTableModelListener );
	}

	/**
	 * 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);
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1