How to comunicate between two jtable

Hello,
I have a jtable  in my first panel which display records from a customer table. What I would like to do is by selecting any row (a customer actually) from this table, display the list of departments from department table  (this table has foreign key of customer id) in my second panel either in form of jlist or jtable. I would like to create this jlist or second jtable programatically.
Any help would be greately appreciated.

Thanks.
guavamayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VenabiliCommented:
Just listen for the selection event and when it happens, read what had been selected and create the second table accordingly.

http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html is an example of how to listen for selection changes.
Other examples:
http://java.sun.com/docs/books/tutorial/uiswing/events/listselectionlistener.html
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
http://forums.devx.com/archive/index.php/t-27297.html
0
guavamayAuthor Commented:
Thanks for the info. I have been already went through those links. I am very new to java and still learning. I am using the selectionlistner but its not working.

1. I have main menu which displays after I logged in successfully. In this main menu I have on the top panel JTable which displays customer record from db. This JTable I created from Swing JTable and created separate Customer.java.
 In this mainmenu class, in the main method section I have the following code
------------------------------
  public static void main(String args[]) {
      /*  java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainMenu().setVisible(true);
            }
        }); */
        JTable customerjTable = new JTable();
    SelectionListener listener = new SelectionListener(customerjTable);
    customerjTable.getSelectionModel().addListSelectionListener((ListSelectionListener) listener);
    customerjTable.getColumnModel().getSelectionModel().addListSelectionListener((ListSelectionListener) listener);


    }
2. I created a separate SelectionListerner class and here is the code ..
------
class SelectionListener implements ListSelectionListener {
    JTable customerjTable;

    SelectionListener(JTable customerjTable) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    public void valueChanged(ListSelectionEvent e) {
        System.out.println("heelo");
         if (e.getSource() == customerjTable.getSelectionModel() && customerjTable.getRowSelectionAllowed()) {
             int first = e.getFirstIndex();
             int last = e.getLastIndex();
             System.out.println(first);
         } else if (e.getSource() == customerjTable.getColumnModel().getSelectionModel()
           && customerjTable.getColumnSelectionAllowed()) {
             int first = e.getFirstIndex();
             int last = e.getLastIndex();
         }
         if (e.getValueIsAdjusting()) {
            System.out.println("The mouse button has not yet been released");
         }

    }

}

I am stuck here... When I select a row from my jtable, I am not getting System.out.println from the SelectionListener class. Shouldn't I be getting it?

Thanks.
0
VenabiliCommented:
One think that I am seeing immediatelly:
SelectionListener listener = new SelectionListener(customerjTable);
and
SelectionListener(JTable customerjTable) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

You do not create the object at all...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

guavamayAuthor Commented:
I took the information from the same link that you provided
http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html

So should I just have the SelectionListerner Class as it shows in my number 2 section? I am not sure I understand clearly. How this SelectionListener Class is going to execute?

Thanks,
0
zzynxSoftware engineerCommented:
Replace

    SelectionListener(JTable customerjTable) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

by

    SelectionListener(JTable customerjTable) {
        this.customerjTable = customerjTable; // you copy the incoming argument in the class's local variable
    }

and of course be sure that the table you visually see
>> I have on the top panel JTable which displays customer record from db
is the one your listener listens on. In other words that table should be 'customerjTable'
0
zzynxSoftware engineerCommented:
>> How this SelectionListener Class is going to execute?
Whenever a row is selected in the table the listener listens on, the method valueChanged() will be triggered
0
guavamayAuthor Commented:
Hi zzynx,
Thanks so much for answering my question.
However, I replaced the code as you suggested, but still not working.  I don't think the SelectionListener Class is getting executed at all. Because I am still not gettin the System.out.println information!
Yes, it is the customerjTable I have on the top panel which I am trying to select the row.
What am I doing wrong?
Thanks.
0
zzynxSoftware engineerCommented:
I'd like to see all your code. Please post it.
0
guavamayAuthor Commented:
Hi zzynx,
Here is the code from mainmenu.java where I have customerjTable on the top panel. I created this table from Swing Palette. After login successfully this table does display with my customer records.
=====================

public class MainMenu extends javax.swing.JFrame {

    /** Creates new form MainMenu */
    public MainMenu() {
        initComponents();
    }
   public static void main(String args[]) {
      /*  java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainMenu().setVisible(true);
            }
        }); */
        JTable customerjTable = new JTable();
    SelectionListener listener = new SelectionListener(customerjTable);
    customerjTable.getSelectionModel().addListSelectionListener((ListSelectionListener) listener);
    customerjTable.getColumnModel().getSelectionModel().addListSelectionListener((ListSelectionListener) listener);
    System.out.println("mainmenu-main");

    }
}

Here is the code from SelectionListener.java
==============================
class SelectionListener implements ListSelectionListener {
    JTable customerjTable;

   /* SelectionListener(JTable customerjTable) {
        throw new UnsupportedOperationException("Not yet implemented");
    } */
    SelectionListener(JTable customerjTable) {
        this.customerjTable = customerjTable;
        System.out.println("class selecttion");
    }

    public void valueChanged(ListSelectionEvent e) {
        System.out.println("heelo");
         if (e.getSource() == customerjTable.getSelectionModel() && customerjTable.getRowSelectionAllowed()) {
             int first = e.getFirstIndex();
             int last = e.getLastIndex();
             System.out.println(first);
         } else if (e.getSource() == customerjTable.getColumnModel().getSelectionModel()
           && customerjTable.getColumnSelectionAllowed()) {
             int first = e.getFirstIndex();
             int last = e.getLastIndex();
         }
         if (e.getValueIsAdjusting()) {
            System.out.println("The mouse button has not yet been released");
         }

    }

}

Here is the code from Customer1.java which was created through customerjTable
==============
@Entity
@Table(name = "customer1", catalog = "Test", schema = "")
@NamedQueries({@NamedQuery(name = "Customer1.findAll", query = "SELECT c FROM Customer1 c"), @NamedQuery(name = "Customer1.findByCustomerID", query = "SELECT c FROM Customer1 c WHERE c.customerID = :customerID"), @NamedQuery(name = "Customer1.findByCustomerName", query = "SELECT c FROM Customer1 c WHERE c.customerName = :customerName"), @NamedQuery(name = "Customer1.findByContact", query = "SELECT c FROM Customer1 c WHERE c.contact = :contact"), @NamedQuery(name = "Customer1.findByClientSince", query = "SELECT c FROM Customer1 c WHERE c.clientSince = :clientSince"), @NamedQuery(name = "Customer1.findByAddress1", query = "SELECT c FROM Customer1 c WHERE c.address1 = :address1"), @NamedQuery(name = "Customer1.findByAddress2", query = "SELECT c FROM Customer1 c WHERE c.address2 = :address2")})
public class Customer1 implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "customerID")
    private Integer customerID;
    @Column(name = "customerName")
    private String customerName;
    @Column(name = "contact")
    private String contact;
    @Column(name = "clientSince")
    @Temporal(TemporalType.DATE)
    private Date clientSince;
    @Column(name = "address1")
    private String address1;
    @Column(name = "address2")
    private String address2;
     
    public Customer1() {
    }

    public Customer1(Integer customerID) {
        this.customerID = customerID;
    }

    public Integer getCustomerID() {
        return customerID;
    }

    public void setCustomerID(Integer customerID) {
        Integer oldCustomerID = this.customerID;
        this.customerID = customerID;
        changeSupport.firePropertyChange("customerID", oldCustomerID, customerID);
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        String oldCustomerName = this.customerName;
        this.customerName = customerName;
        changeSupport.firePropertyChange("customerName", oldCustomerName, customerName);
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        String oldContact = this.contact;
        this.contact = contact;
        changeSupport.firePropertyChange("contact", oldContact, contact);
    }

    public Date getClientSince() {
        return clientSince;
    }

    public void setClientSince(Date clientSince) {
        Date oldClientSince = this.clientSince;
        this.clientSince = clientSince;
        changeSupport.firePropertyChange("clientSince", oldClientSince, clientSince);
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        String oldAddress1 = this.address1;
        this.address1 = address1;
        changeSupport.firePropertyChange("address1", oldAddress1, address1);
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        String oldAddress2 = this.address2;
        this.address2 = address2;
        changeSupport.firePropertyChange("address2", oldAddress2, address2);
    }

   
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (customerID != null ? customerID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customer1)) {
            return false;
        }
        Customer1 other = (Customer1) object;
        if ((this.customerID == null && other.customerID != null) || (this.customerID != null && !this.customerID.equals(other.customerID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Myapp.Customer1[customerID=" + customerID + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}

All these Classes are in my same package Myapp.
Thanks so much for taking the time to help me. I really appreciate it.



0
zzynxSoftware engineerCommented:
That code doesn't give me much more valuable information about your problem.
The code for the listener seems OK.
Please show me the GUI code that proves that the table your making selections in is indeed "customerjTable".
0
guavamayAuthor Commented:
Sorry zzynx, I thought the customer1.java is the GUI code for this table. I didn't create this table programatically. I used JTable from Swing Palette and went through the process by selecting the table from database and selected the columns etc. Then I just rename the JTable to customerjTable.  This table I drew it in the frame of my mainmenu.
I am sorry I don't understand.

0
zzynxSoftware engineerCommented:
I'm afraid I can't help you any further if I can't see that GUI code.
Sorry.
0
guavamayAuthor Commented:
I understand! What I will do is to post all the code my mainmenu has it. Because thats were I drew that table. I will do them tonight. Hope that will help you to see the code that you are looking for.
In the meantime, just want to confirm, which will help me to understand the code that you are looking for is, the codes that created the customerjTable - correct?

Thanks.
0
zzynxSoftware engineerCommented:
>> to understand the code that you are looking for is, the codes that created the customerjTable - correct?
I want to see the GUI code where "customerjTable" is used.
I want to see a screenshot of what your screen/window containing the table looks like and the code that creates that window/screen in which I expect to see the "customerjTable" variable.
0
guavamayAuthor Commented:
Hi zzynx,
Sorry for the late respond. Guess what! I made it work. I can't believe it I fixed it. I added a mouseclick event on that table and got the result that I was looking for.
Anyhow, the points are yours to keep.  Thanks.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
Thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.