How to read the last cell of a one column JTable

Trying to read the last cell from a JTable

Page 1 of 1

6 Replies - 3184 Views - Last Post: 06 March 2009 - 02:16 PM Rate Topic: -----

#1 jnartey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 24-February 09

How to read the last cell of a one column JTable

Posted 05 March 2009 - 06:56 PM

I have a one column interactive JTable. Initally the table has ten empty rows. When the user gets to the last row, the table automatically inserts another row. My problem is this: When the user enters lets say, 5 items on the table, unless the user hits enter or the arrow key to move away from the last cell edited, I am not able to get the last cell that was edited. Is there anyway, I can set the cursor for the JTable that is being edited to know about the last cell the user edited without having the user hit the enter key or the arrow keys?

*Edited to remove the "Advanced" tag

This post has been edited by pbl: 05 March 2009 - 08:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to read the last cell of a one column JTable

#2 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to read the last cell of a one column JTable

Posted 05 March 2009 - 07:01 PM

View Postjnartey, on 5 Mar, 2009 - 05:56 PM, said:

I have a one column interactive JTable. Initally the table has ten empty rows. When the user gets to the last row, the table automatically inserts another row. My problem is this: When the user enters lets say, 5 items on the table, unless the user hits enter or the arrow key to move away from the last cell edited, I am not able to get the last cell that was edited. Is there anyway, I can set the cursor for the JTable that is being edited to know about the last cell the user edited without having the user hit the enter key or the arrow keys?

Wouldn't qualify this topic as "Advanced"
Basic TableModel method: getRowCount()
Was This Post Helpful? 0
  • +
  • -

#3 jnartey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 24-February 09

Re: How to read the last cell of a one column JTable

Posted 05 March 2009 - 07:12 PM

Thanks for the response. getRowCount(), knows about the last data entered only when the user hits enter after the input in the cell, or navigates away from the cell. It misses the data in the last cell, if the user does not hit enter. I was wondering if I could use the cursor to determine the last cell entered.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to read the last cell of a one column JTable

Posted 05 March 2009 - 07:19 PM

View Postjnartey, on 5 Mar, 2009 - 06:12 PM, said:

Thanks for the response. getRowCount(), knows about the last data entered only when the user hits enter after the input in the cell, or navigates away from the cell. It misses the data in the last cell, if the user does not hit enter. I was wondering if I could use the cursor to determine the last cell entered.

The cursor will never go beyond the last cell row/column
So it is your model responsability to update the numbers of row/columns it contains
So getRowCount() should return the CORRECT value.
Was This Post Helpful? 0
  • +
  • -

#5 jnartey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 24-February 09

Re: How to read the last cell of a one column JTable

Posted 05 March 2009 - 07:32 PM

Thanks once again. I will take a look at the model once again:
Was This Post Helpful? 0
  • +
  • -

#6 jnartey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 24-February 09

Re: How to read the last cell of a one column JTable

Posted 06 March 2009 - 11:44 AM

To re-phrase my question, supposing the code below represents your TableModelListener:


public class InteractiveTableModelListener implements TableModelListener {
		 public void tableChanged(TableModelEvent evt) {
			 if (evt.getType() == TableModelEvent.UPDATE) {
				 int row = evt.getFirstRow();
				 System.out.println("Updated row:"+" "+row);
				 jTable1.setRowSelectionInterval(row, row);
			 }
		 }
	 }

[CODE]

If you print updated row, it does not include the last cell unless you hit enter in the last cell or navigate away from it using the arrow. I also use getRowCount() to loop through the rows as indicated in the code below:

[CODE]
public static void fillCodeList(){

	  JTable table = GLPanel.jTable1;
	  int rowcount = table.getModel().getRowCount();
	  for(int i = 0;i<rowcount;i++){
		String tablecode = (String)table.getModel().getValueAt(i, 0);

		if((tablecode != null)&&(!(tablecode.equalsIgnoreCase("")))){
			gtrans.add(new GLCode(tablecode));
		}


Was This Post Helpful? 0
  • +
  • -

#10 jnartey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 24-February 09

Re: How to read the last cell of a one column JTable

Posted 06 March 2009 - 02:16 PM

Thanks everyone who contributed. I finally figured out how to fix it. The code below explains how: This will read and save the last data edited whenever another component gets focus. That means you don't have to hit the enter key or navigate away from the last cell before the table knows of the last data entered.

JTable jTable1 = new JTable();
jTable1.putClientProperty("terminateEditonfocusLost", Boolean.TRUE);


Was This Post Helpful? 1

Page 1 of 1