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.
Get table data cell information -> text fields
Page 1 of 16 Replies - 1499 Views - Last Post: 05 April 2011 - 09:59 PM
Replies To: Get table data cell information -> text fields
#2
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?
Edit: Just to clarify, are you talking about editing the textfields in the JTable or external to it?
#3
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!
Hope this helps!
#4
Re: Get table data cell information -> text fields
Posted 05 April 2011 - 07:11 AM
macosxnerd101, 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?
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.
#5
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.
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.
#6
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
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?
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?
#7
Re: Get table data cell information -> text fields
Posted 05 April 2011 - 09:59 PM
How can
event.getFirstIndex() >= 0)
not be >= 0 ?
event.getFirstIndex() >= 0)
not be >= 0 ?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|