Link to home
Start Free TrialLog in
Avatar of guavamay
guavamay

asked on

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.
Avatar of Venabili
Venabili
Flag of Bulgaria image

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
Avatar of guavamay
guavamay

ASKER

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.
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...
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,
Avatar of zzynx
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'
>> 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
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.
I'd like to see all your code. Please post it.
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.



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".
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.

I'm afraid I can't help you any further if I can't see that GUI code.
Sorry.
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.
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx