6 Replies - 2045 Views - Last Post: 05 April 2011 - 09:59 PM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Get table data cell information -> text fields

Posted 31 March 2011 - 12:54 PM

Hello,

I just have a question about this. I know there is a getValueAt method to get data out of the jtable however I want to make it so when a user clicks on a row it fills in the corresponding textfields. Is there a mouse-click event for jtables i could use for this or is there a better way to approach this? I haven't been able to find any useful information anywhere on this.
Is This A Good Question/Topic? 0
  • +

Replies To: Get table data cell information -> text fields

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

Re: Get table data cell information -> text fields

Posted 31 March 2011 - 09:39 PM

You can use the JTable getSelectedRow() method, and iterate through each cell, updating it appropriately.

Edit: Just to clarify, are you talking about editing the textfields in the JTable or external to it?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4359
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: Get table data cell information -> text fields

Posted 31 March 2011 - 09:49 PM

Well you can try using a ListSelectionListener which listens for selection changes. Once you detect a selection changed, you can get the row and read each cell using getValueAt. It is just a matter of then setting the appropriate textboxes with those values you fetch from the cells in the selected row.

Hope this helps! :)
Was This Post Helpful? 1
  • +
  • -

#4 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Get table data cell information -> text fields

Posted 05 April 2011 - 07:11 AM

View Postmacosxnerd101, on 31 March 2011 - 09:39 PM, said:

You can use the JTable getSelectedRow() method, and iterate through each cell, updating it appropriately.

Edit: Just to clarify, are you talking about editing the textfields in the JTable or external to it?


I plan to take any information from whichever row the user selects and put it into each respective text field. However I am having trouble with the listener, is there any way to easily check if my listener is working? Martyr2 suggested I use a ListSelectionListener, would this work with a JTable or does it only work with a list control on my form? I'm not having much luck with the listselectionlistener.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

Re: Get table data cell information -> text fields

Posted 05 April 2011 - 07:15 AM

You can check out the Oracle tutorial on ListSelectionListener. There is a section for JTables as well.

As to check if the Listener is working, throw in some System.out.println() statements in the ListSelectionListener methods. If you get the outputs, the Listener is set up correctly.
Was This Post Helpful? 1
  • +
  • -

#6 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Get table data cell information -> text fields

Posted 05 April 2011 - 12:32 PM

Thanks for the guide. It was very helpful.

I still cannot get this working, however.

In my constructor
ListSelectionModel selectionModel = customerTable.getSelectionModel();
		selectionModel.addListSelectionListener( this );





@Override
	public void valueChanged(ListSelectionEvent event) {
		// TODO Auto-generated method stub		
		if(event.getSource() == customerTable.getSelectionModel() &&
				event.getFirstIndex() >= 0)
		{
			TableModel model = (TableModel)customerTable.getModel();
			String string = (String)model.getValueAt(customerTable.getSelectedRow(), customerTable.getSelectedColumn());
			System.out.println("value selected = " + string);
		}
		
	}



Isn't this the bare minimum to get a listener to work for a JTable? Am I missing something? It will not spit anything out in the console if I do anything to my table. Does anyone have any clues?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Get table data cell information -> text fields

Posted 05 April 2011 - 09:59 PM

How can

event.getFirstIndex() >= 0)

not be >= 0 ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1