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.
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.
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.invoke Later(new Runnable() {
public void run() {
new MainMenu().setVisible(true );
}
}); */
JTable customerjTable = new JTable();
SelectionListener listener = new SelectionListener(customer jTable);
customerjTable.getSelectio nModel().a ddListSele ctionListe ner((ListS electionLi stener) listener);
customerjTable.getColumnMo del().getS electionMo del().addL istSelecti onListener ((ListSele ctionListe ner) listener);
}
2. I created a separate SelectionListerner class and here is the code ..
------
class SelectionListener implements ListSelectionListener {
JTable customerjTable;
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept ion("Not yet implemented");
}
public void valueChanged(ListSelection Event e) {
System.out.println("heelo" );
if (e.getSource() == customerjTable.getSelectio nModel() && customerjTable.getRowSelec tionAllowe d()) {
int first = e.getFirstIndex();
int last = e.getLastIndex();
System.out.println(first);
} else if (e.getSource() == customerjTable.getColumnMo del().getS electionMo del()
&& customerjTable.getColumnSe lectionAll owed()) {
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.
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.invoke
public void run() {
new MainMenu().setVisible(true
}
}); */
JTable customerjTable = new JTable();
SelectionListener listener = new SelectionListener(customer
customerjTable.getSelectio
customerjTable.getColumnMo
}
2. I created a separate SelectionListerner class and here is the code ..
------
class SelectionListener implements ListSelectionListener {
JTable customerjTable;
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept
}
public void valueChanged(ListSelection
System.out.println("heelo"
if (e.getSource() == customerjTable.getSelectio
int first = e.getFirstIndex();
int last = e.getLastIndex();
System.out.println(first);
} else if (e.getSource() == customerjTable.getColumnMo
&& customerjTable.getColumnSe
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(customer jTable);
and
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept ion("Not yet implemented");
}
You do not create the object at all...
SelectionListener listener = new SelectionListener(customer
and
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept
}
You do not create the object at all...
ASKER
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,
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,
Replace
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept ion("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'
SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept
}
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
Whenever a row is selected in the table the listener listens on, the method valueChanged() will be triggered
ASKER
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.
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.
ASKER
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.invoke Later(new Runnable() {
public void run() {
new MainMenu().setVisible(true );
}
}); */
JTable customerjTable = new JTable();
SelectionListener listener = new SelectionListener(customer jTable);
customerjTable.getSelectio nModel().a ddListSele ctionListe ner((ListS electionLi stener) listener);
customerjTable.getColumnMo del().getS electionMo del().addL istSelecti onListener ((ListSele ctionListe ner) listener);
System.out.println("mainme nu-main");
}
}
Here is the code from SelectionListener.java
========================== ====
class SelectionListener implements ListSelectionListener {
JTable customerjTable;
/* SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept ion("Not yet implemented");
} */
SelectionListener(JTable customerjTable) {
this.customerjTable = customerjTable;
System.out.println("class selecttion");
}
public void valueChanged(ListSelection Event e) {
System.out.println("heelo" );
if (e.getSource() == customerjTable.getSelectio nModel() && customerjTable.getRowSelec tionAllowe d()) {
int first = e.getFirstIndex();
int last = e.getLastIndex();
System.out.println(first);
} else if (e.getSource() == customerjTable.getColumnMo del().getS electionMo del()
&& customerjTable.getColumnSe lectionAll owed()) {
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.findByCustomerI D", query = "SELECT c FROM Customer1 c WHERE c.customerID = :customerID"), @NamedQuery(name = "Customer1.findByCustomerN ame", 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.findByClientSin ce", 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.DAT E)
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.fireProperty Change("cu stomerID", oldCustomerID, customerID);
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
String oldCustomerName = this.customerName;
this.customerName = customerName;
changeSupport.fireProperty Change("cu stomerName ", oldCustomerName, customerName);
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
String oldContact = this.contact;
this.contact = contact;
changeSupport.fireProperty Change("co ntact", oldContact, contact);
}
public Date getClientSince() {
return clientSince;
}
public void setClientSince(Date clientSince) {
Date oldClientSince = this.clientSince;
this.clientSince = clientSince;
changeSupport.fireProperty Change("cl ientSince" , oldClientSince, clientSince);
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
String oldAddress1 = this.address1;
this.address1 = address1;
changeSupport.fireProperty Change("ad dress1", oldAddress1, address1);
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
String oldAddress2 = this.address2;
this.address2 = address2;
changeSupport.fireProperty Change("ad dress2", 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(ot her.custom erID))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Myapp.Customer1[customerI D=" + customerID + "]";
}
public void addPropertyChangeListener( PropertyCh angeListen er listener) {
changeSupport.addPropertyC hangeListe ner(listen er);
}
public void removePropertyChangeListen er(Propert yChangeLis tener listener) {
changeSupport.removeProper tyChangeLi stener(lis tener);
}
}
All these Classes are in my same package Myapp.
Thanks so much for taking the time to help me. I really appreciate it.
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.invoke
public void run() {
new MainMenu().setVisible(true
}
}); */
JTable customerjTable = new JTable();
SelectionListener listener = new SelectionListener(customer
customerjTable.getSelectio
customerjTable.getColumnMo
System.out.println("mainme
}
}
Here is the code from SelectionListener.java
==========================
class SelectionListener implements ListSelectionListener {
JTable customerjTable;
/* SelectionListener(JTable customerjTable) {
throw new UnsupportedOperationExcept
} */
SelectionListener(JTable customerjTable) {
this.customerjTable = customerjTable;
System.out.println("class selecttion");
}
public void valueChanged(ListSelection
System.out.println("heelo"
if (e.getSource() == customerjTable.getSelectio
int first = e.getFirstIndex();
int last = e.getLastIndex();
System.out.println(first);
} else if (e.getSource() == customerjTable.getColumnMo
&& customerjTable.getColumnSe
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
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.DAT
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.fireProperty
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
String oldCustomerName = this.customerName;
this.customerName = customerName;
changeSupport.fireProperty
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
String oldContact = this.contact;
this.contact = contact;
changeSupport.fireProperty
}
public Date getClientSince() {
return clientSince;
}
public void setClientSince(Date clientSince) {
Date oldClientSince = this.clientSince;
this.clientSince = clientSince;
changeSupport.fireProperty
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
String oldAddress1 = this.address1;
this.address1 = address1;
changeSupport.fireProperty
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
String oldAddress2 = this.address2;
this.address2 = address2;
changeSupport.fireProperty
}
@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(ot
return false;
}
return true;
}
@Override
public String toString() {
return "Myapp.Customer1[customerI
}
public void addPropertyChangeListener(
changeSupport.addPropertyC
}
public void removePropertyChangeListen
changeSupport.removeProper
}
}
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".
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".
ASKER
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 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.
Sorry.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanx
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