7 Replies - 357 Views - Last Post: 03 April 2013 - 02:12 PM Rate Topic: -----

#1 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 07:43 AM

When I clicked the 2nd row, its values appears on the textfields but when I clicked the 1st row, it doesn't set the values on the textfields. Anyone know a solution to this or other work-around? I'm using netbeans Thank you!

Screenshot: http://tinypic.com/v...pic=25zmfte&s=6

Here's my code:
private void jTable1MouseClicked(java.awt.event.Mouse… evt) {
	jButton1.setEnabled(false);
	jButton3.setEnabled(false);
	jButton4.setEnabled(true);
	int row = jTable1.getSelectedRow();
	Object temp = jTable1.getModel().getValueAt(row, 0);
	str = (String) temp;
	searchRec();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with editing values of jTable - JAVA/OOP?

#2 pbl  Icon User is offline

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

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

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 08:06 AM

Where do you set the JTextField to str ?
Was This Post Helpful? 0
  • +
  • -

#3 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 11:25 AM

Oops, sorry.. I forgot to include search method

    private void searchRec() {

        try {
            if (str != null) {
                sql = "Select * from released_reports_table Where consultantName='" + str + "'";
                rs = stmt.executeQuery(sql);

                rs.next();
                fname = rs.getString("fname");
                lname = rs.getString("lname");
                mname = rs.getString("mname");
                
                jTextField1.setText(fname);
                jTextField2.setText(lname);
                jTextField3.setText(mname);
                disObj();
            }
        } 
        catch (SQLException err) {
            JOptionPane.showMessageDialog(this, err.getMessage());
        }

    }


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 12:29 PM

So after that line

Object temp = jTable1.getModel().getValueAt(row, 0);

add

System.out.println("Object fetch from row: " + row + " is " + temp);

and see if you have what you expected
Was This Post Helpful? 0
  • +
  • -

#5 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 04:25 PM

View Postpbl, on 02 April 2013 - 12:29 PM, said:

So after that line

Object temp = jTable1.getModel().getValueAt(row, 0);

add

System.out.println("Object fetch from row: " + row + " is " + temp);

and see if you have what you expected


System.out.println("Object fetch from row: " + row + " is " + temp); works.
It says row: 0 when I clicked the 1st row and row: 1 when I clicked the 2nd row. However, the textfield values doesn't still change.
I think the problem lies within search method, can you help me pinpoint it? Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

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

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 02 April 2013 - 07:06 PM

and temp has the good value ?
Was This Post Helpful? 0
  • +
  • -

#7 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 03 April 2013 - 06:02 AM

View Postpbl, on 02 April 2013 - 07:06 PM, said:

and temp has the good value ?


Oh! I just noticed that only the row returns a good value, like I said it returns the right value (row 0 and row 1).

I tried again, and noticed that even I clicked the 1st row, 2nd row's values are the one appearing on the textfields instead of the 1st's.


EDIT:
I think I should replace "Select * from released_reports_table Where consultantName ='" + str + "'" with a more unique value.
Maybe ID instead of consultantName, however I don't want to include a ID column on my table :|
Any possible work arounds here?

This post has been edited by Hypernal: 03 April 2013 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: Problem with editing values of jTable - JAVA/OOP?

Posted 03 April 2013 - 02:12 PM

If you have two rows with the same "consultantName" you will have to prompt the user to know which row(s) he wants to see
or may be you should use a JTable or a JTextArea as output field
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1