4 Replies - 7070 Views - Last Post: 14 April 2011 - 06:09 AM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

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

Getting cell data out of jtable(mouse listener)

Posted 12 April 2011 - 12:10 PM

Hello,

I have a quick question about getting information out of a table cell. I am using a mouse listener to check for which row was selected then having the information moved into respective text fields. I am using a mouse listener because it's the only type of listener I can get to work.

I understand that table.getValueAt(int,int) returns information however when I try this it's only returning the position of the row(such as 1,2,3,4,etc). How can I get it to return the actual value that is in the cell and not the location of it?

Thanks! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting cell data out of jtable(mouse listener)

#2 Criel  Icon User is offline

  • D.I.C Head

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

Re: Getting cell data out of jtable(mouse listener)

Posted 12 April 2011 - 01:41 PM

Figured it out. close plz
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




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

Re: Getting cell data out of jtable(mouse listener)

Posted 12 April 2011 - 02:39 PM

@Criel: Would you mind sharing your solution? :)

Just to add as well for future readers, the MouseEvents the MouseListener deals with return the (x,y) coordinates of the Events. You can use these coordinates, passing them to the JTable rowAtPoint() and columnAtPoint() methods. The values returned from those methods are what you want to pass to the JTable getValueAt() method.
Was This Post Helpful? 0
  • +
  • -

#4 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: Getting cell data out of jtable(mouse listener)

Posted 12 April 2011 - 05:55 PM

For JTable use a SelectionListener that extends ListSelectionListener

	class SelectionListener implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent e) {
			if(e.getValueIsAdjusting())			// mouse button not released yet
				return;
			int row = getSelectedRow();
			if(row < 0)							// v clearSelection
				return;
			int col = getSelectedColumn();	
			
			if(col < 0)							// vrai lors d'un clearSelection
				return;

			// cell a (row,col) has been clicked
			clearSelection();
		}


Was This Post Helpful? 0
  • +
  • -

#5 Criel  Icon User is offline

  • D.I.C Head

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

Re: Getting cell data out of jtable(mouse listener)

Posted 14 April 2011 - 06:09 AM

I was just making a silly mistake, all I did was create a method and call it in my mouseClicked method.

private void rowIdentifier(int row) {
		//System.out.println("asdf " + row + " asdf");
		idTFInfo = (String) customerTable.getValueAt(row, 0);
		firstNameTFInfo = (String) customerTable.getValueAt(row, 1);
		lastNameTFInfo = (String) customerTable.getValueAt(row, 2);
		MITFInfo = (String) customerTable.getValueAt(row, 3);
		addressTFInfo = (String) customerTable.getValueAt(row, 4);
		phoneTFInfo = (String) customerTable.getValueAt(row, 5);
		cityTFInfo = (String) customerTable.getValueAt(row, 6);
		stateTFInfo = (String) customerTable.getValueAt(row, 7);
		zipTFInfo = (String) customerTable.getValueAt(row, 8);

		idTextField.setText(idTFInfo);
		firstNameTextField.setText(firstNameTFInfo);
		lastNameTextField.setText(lastNameTFInfo);
	    miTextField.setText(MITFInfo);
	    addressTextField.setText(addressTFInfo);
	    phoneTextField.setText(phoneTFInfo);
	    cityTextField.setText(cityTFInfo);
	    stateTextField.setText(stateTFInfo);
	    zipTextField.setText(zipTFInfo);
	}

	private void mouse() {
		customerTable.addMouseListener(new MouseAdapter(){		
			public void mouseClicked(MouseEvent e)
			{

				Point p = e.getPoint();
				int row = customerTable.rowAtPoint(p);
				int column = customerTable.columnAtPoint(p);
				rowIdentifier(row);

				
			}
		});



@pbl: Is there a benefit for using SelectionListener if I got the mouse listener to work with the JTable?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1