Results 1 to 8 of 8
  1. #1
    tihionline is offline Member
    Join Date
    Jul 2016
    Posts
    3
    Rep Power
    0

    Default Refresh data on object from another object

    Hi,

    I have object A (JDialog), which contains object B (JPanel), and also creates object C (JDialog).

    Object C has JButton, and I want, when somebody clicks on that JButton, object C disposes, and object B do method (that refresh data in part of object B).

    How can I do that?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Refresh data on object from another object

    Make sure C has a reference to B. Something like this:
    Java Code:
    class B extends JPanel {
    
       public void showC() {
          C dialog = new C(this);
          dialog.setVisible(true);
       }
    
       public void refresh() {
    
       }
    }
    
    class C extends JDialog {
       private B parent;
    
       public C( B parent ) {
          this.parent = parent;
       }
       
       private void close() {
          parent.refresh();
          dispose();      
       }
    }
    Edit: creating a field named parent in Swing can be tricky. Rename it to anything else if it conflicts.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    tihionline is offline Member
    Join Date
    Jul 2016
    Posts
    3
    Rep Power
    0

    Default Re: Refresh data on object from another object

    But i can't create a reference to B in class C, because i create class C from class A, and not from class B.

    So I have something like this:

    class A extends JDialog {
    B panel = new B();
    add(panel);

    C otherFrame = new C();

    }

    class B extends JPanel {
    public void refresh() {
    }
    }

    class C extends JDialog {
    JButton button = new JButton("Click me!");
    }

    And now i want to set on button in class C, when user clicks on it, to dispose() C, and also to run b.refresh() method.
    Is it possible?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Refresh data on object from another object

    What about this:
    Java Code:
    C otherFrame = new C();
    B panel = new B( otherFrame );
    add(panel);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Refresh data on object from another object

    In case you're wondering how I got this Java code all nicely indented and colourful, I use [code][/code] tags around my code.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Refresh data on object from another object

    Java Code:
    interface YourButtonListener {
        void handleButtonClick();
    }
    class A extends JDialog implements YourButtonListener {
    
        public void handleButtonClick() {
            // do whatever it is this is supposed to do
        }
    
       // when you create the C dialog then pass this into the constructor.
    }
    class C extends JDialog {
        
        public C(YourButtonListener listener) {
            // assign to attribute
        }
    
        // add action listener to the button, on click the call the listener.handleButtonClick()
    }
    That's how you get things to talk to each other.
    You define an interface for the thing doing the listening, consisting of the events you want to handle (this is quite often a single method), then assign that to the thing you want to listen on. When a relevant event occurs then the thing being listened to calls its listeners.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    tihionline is offline Member
    Join Date
    Jul 2016
    Posts
    3
    Rep Power
    0

    Default Re: Refresh data on object from another object

    Quote Originally Posted by Tolls View Post
    Java Code:
    interface YourButtonListener {
        void handleButtonClick();
    }
    class A extends JDialog implements YourButtonListener {
    
        public void handleButtonClick() {
            // do whatever it is this is supposed to do
        }
    
       // when you create the C dialog then pass this into the constructor.
    }
    class C extends JDialog {
        
        public C(YourButtonListener listener) {
            // assign to attribute
        }
    
        // add action listener to the button, on click the call the listener.handleButtonClick()
    }
    That's how you get things to talk to each other.
    You define an interface for the thing doing the listening, consisting of the events you want to handle (this is quite often a single method), then assign that to the thing you want to listen on. When a relevant event occurs then the thing being listened to calls its listeners.
    This works, thank you.
    I also tried to create class D, and implement YourButtonListener in that. But when i click on the button on class A, nothing happens on class D. Do you have any idea why?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Refresh data on object from another object

    Without code, no.
    Could be squirrels.
    ;)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to dump out whatever data an object contains
    By Zarah in forum New To Java
    Replies: 21
    Last Post: 05-25-2016, 12:08 AM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. How do I refresh a TreeViewer object?
    By xcallmejudasx in forum SWT / JFace
    Replies: 0
    Last Post: 10-27-2008, 03:51 PM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. how to clear the data of an object
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:50 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
  •