?
Solved

How to comunicate between two jtable

Posted on 2010-04-03
16
Medium Priority
?
441 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:guavamay
  • 7
  • 7
  • 2
16 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 29710019
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
 

Author Comment

by:guavamay
ID: 29750581
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29897220
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:guavamay
ID: 29916337
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
 
LVL 37

Expert Comment

by:zzynx
ID: 29996180
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
 
LVL 37

Expert Comment

by:zzynx
ID: 29996278
>> 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
 

Author Comment

by:guavamay
ID: 30085291
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
 
LVL 37

Expert Comment

by:zzynx
ID: 30120733
I'd like to see all your code. Please post it.
0
 

Author Comment

by:guavamay
ID: 30165309
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
 
LVL 37

Expert Comment

by:zzynx
ID: 30622231
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
 

Author Comment

by:guavamay
ID: 30624613
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
 
LVL 37

Expert Comment

by:zzynx
ID: 30625275
I'm afraid I can't help you any further if I can't see that GUI code.
Sorry.
0
 

Author Comment

by:guavamay
ID: 30634459
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 600 total points
ID: 30725436
>> 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
 

Accepted Solution

by:
guavamay earned 0 total points
ID: 31276740
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
 
LVL 37

Expert Comment

by:zzynx
ID: 31277843
Thanx
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question