Results 1 to 4 of 4
  1. #1
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Arrow How to set the text of a TextField to a highlighted row in a JTable?

    Hello everyone,

    Hope ya'll are doing great.

    Just a simple question: I have a JTable with data about cars in one interface (with a button called "deal") and a few JTextFields in another interface. The JTextFields correspond to the car details (i.e. "model", "make", "year" etc). Now, what I want to do is, when I click on (highlight) a row on the JTable then click on "deal", I want the JTextFields in the other interface to be filled with the corresponding car details. I think I'm supposed to use mouseListener but I'm not quite sure how to go about it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could get the JTable's ListSelectionModel via the getSelectionModel() and then add a ListSelectionListener to the model.

  3. #3
    charithdula is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    First register a listener on the selection model
    here's how to do it.
    jTable1.getSelectionModel().addListSelectionListen er(new ListSelectionListener()
    {
    public void valueChanged(ListSelectionEvent e)
    {
    if(!(jTable1.getSelectionModel().isSelectionEmpty( )))
    {
    //retrieving the selected row index
    int row = jTable1.getSelectedRow();

    //if a single row is selected from the table, take each cell values into the controls
    if (jTable1.getRowSelectionAllowed()) {


    String model = jTable1.getValueAt(row, 0).toString();
    jTextField1.setText(model);

    }
    });

  4. #4
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Thanx a bunch charithdula and Fubarable. Problem solved =)

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  2. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  3. accessing the text of textfield of one class in another
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 02:54 PM
  4. Styled Text with highlighted Odd Line
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:31 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •