Results 1 to 6 of 6
  1. #1
    drooone is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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
    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) {};
    .
    .
    .
    .
    Of course that is only a part of the code.
    thx in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JTable PropertyChangeListener

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JTable PropertyChangeListener

    Do you want the bean to listen to the table or the table to listen to the bean?

    db

    edit
    Java Code:
      extends JFrame implements ActionListener, PropertyChangeListener
    Don't make a 'God Class' -- split up the responsibility. Your application isn't a JFrame, is it? rather, it uses a JFrame.
    Last edited by DarrylBurke; 04-05-2012 at 05:14 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    drooone is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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 :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    drooone is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: JTable PropertyChangeListener

    I was reading that thread befor 3 times with no effect :)
    how to setup Listener for table , so i could start listen for change of value of specific cell.

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 07:57 PM
  2. Replies: 0
    Last Post: 02-07-2010, 08:06 PM
  3. PropertyChangeSupport and PropertyChangeListener over RMI
    By cerberus in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-12-2009, 05:10 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. How to use PropertyChangeListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •