Results 1 to 6 of 6
Thread: JTable PropertyChangeListener
- 04-05-2012, 03:47 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
JTable PropertyChangeListener
Hi
Im new over here.I have a question about PropertyChangeListener
I have a 3 classes
Class No. 1
reads data from a file to a info about a trip then adds to the collection
of course before that trip becomes an object of class 2(class2 is JavaBean class),
to be clear collection stores objects of class 2
Class No. 3 Creating a GUI and all the elements of the collection are going to the JTable (each line is the one trip)
And now the question is how to change the field, for example the price of trip of course that has to update collection, I would like to do this using PropertyChangeListener'a
I dont have idea how to connect PropertyChangeListener with JTable and my collection
Of course that is only a part of the code.Java Code:public class class1 { public staticList<Offers> offersList = new ArrayList<Offers>(); public static void main(String[] args) throws IOException, ParseException { . . . . offersList.add(newOffer); for(Offers s:offersList){ DateFormat df = new SimpleDateFormat("dd/MM/yyyy",Locale.FRANCE); frame.model.insertRow(number, new Object[] {df.format(s.getDepartur()), s.getCountry(),s.getPrice() }); ///////////////////////////////////////////////////// class class2 implements Serializable{ public Date departure; public String country; public double price; private PropertyChangeSupport propertyChange = new PropertyChangeSupport(this); public Date getDepartur() { return this.departure; } public String getCountry(){ return this.country; } public double getPrice(){ return this.price; } public synchronized void setPrice(double aPrice) { double oldValue = price; price = aPrice; propertyChange.firePropertyChange("count", new Double(oldValue), new Double(aPrice)); } public synchronized void addPropertyChangeListener(PropertyChangeListener l) { propertyChange.addPropertyChangeListener(l); } public synchronized void removePropertyChangeListener(PropertyChangeListener l) { propertyChange.removePropertyChangeListener(l); } } ///////////////////////////////////////////////////////////////////////////////////////////// public class3 Frame extends JFrame implements ActionListener, PropertyChangeListener { int number = 0; JMenuItem open,save; DefaultTableModel model; private Action action; //test praca private int row; private int column; private Object oldValue; private Object newValue; JTable table; JFileChooser chooser = new JFileChooser(); Frame() throws IOException { Object[][] data2 = {}; String[] columnNames = { "Field1", "Field2","Field3"}; model = new DefaultTableModel(data2, columnNames); table = new JTable(model) {}; . . . .
thx in advance
- 04-05-2012, 05:06 PM #2
Re: JTable PropertyChangeListener
Moved from New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 05:12 PM #3
Re: JTable PropertyChangeListener
Do you want the bean to listen to the table or the table to listen to the bean?
db
editDon't make a 'God Class' -- split up the responsibility. Your application isn't a JFrame, is it? rather, it uses a JFrame.Java Code:extends JFrame implements ActionListener, PropertyChangeListener
Last edited by DarrylBurke; 04-05-2012 at 05:14 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 05:27 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: JTable PropertyChangeListener
I think that better will be if the bean will listen to the table
But my brain works as a user's brain :) focused on JTable :)
- 04-05-2012, 07:49 PM #5
Re: JTable PropertyChangeListener
So, add a PropertyChangeListener to the JTable. In the propertyChange(...) method sysout the information available from the PropertyChangeEvent. that will guide you as to what property you need to listen for, and what value or change is of interest.
Check out the tutorial on How to Write a Property Change Listener (The Java Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 09:02 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
JTable validate already existing JTable data and set Background colour issue
By pi4r0n in forum AWT / SwingReplies: 4Last Post: 04-02-2012, 07:57 PM -
PropertyChangeListener for FieldEditor in a PreferencePage
By neonfly in forum EclipseReplies: 0Last Post: 02-07-2010, 08:06 PM -
PropertyChangeSupport and PropertyChangeListener over RMI
By cerberus in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 03-12-2009, 05:10 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
How to use PropertyChangeListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks