Calling methods of "parent" component in Java

Mel :

I have the following situation I think would be best to show in sample program code. I have a Java class that extends JPanel. In this class are two objects which are two more JPanels. In one of the JPanel objects is a JTable object. I added a listener to this JTable that detects a double click. When it detects a double click, I want to fire a method in the top class. How do I reference this method in Java?

public class TopPanel extends JPanel {

  JPanel OnePanel;
  JPanel TwoPanel;

  public void MethodToFire;

}

public class OnePanel extends JPanel {

  JTable TheTable;

}

public class TheTable extends JTable {

  public TheTable {
    this.addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent e){
          if (e.getClickCount() == 2){ SYNTAX CALLING THE METHOD IN TopPanel  }
      }
    } );
  }
}
David Koelle :

One way to solve this is to use composition instead of inheritance. You could pass the JPanel into your subclass of the JTable.


public class TopPanel extends JPanel 
{
  private TheTable table;

  public TopPanel()
  {
    table = new TheTable(this);
  }

  public void methodToFire() { }
}

public class TheTable extends JTable 
{
  private TopPanel panel;

  public TheTable(TopPanel panel)
  {
    this.panel = panel;

    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        doThing();
      }
    } );
  }

  private void doThing()
  {
    this.panel.methodToFire(); 
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling the method of the parent Vue component

From Dev

Calling a method on Parent Component from Child Component

From Dev

Child component is not calling parent component function

From Dev

XPages - Calling Java methods

From Dev

Calling the methods in java

From Dev

Calling Java Methods in XSLT

From Java

Calling multiple methods in Java

From Java

Calling the methods in a class in Java

From Dev

Calling methods on objects Java

From Dev

Multiple buttons in the child component with the methods in the parent component

From Dev

Calling methods of actual object instead of parent class

From Dev

Calling a children method of a functional component from parent

From Dev

Calling Parent Function in Child Component in React

From Dev

Calling parent functions from child component

From Dev

React: problem calling child function in parent component

From Java

Calling methods on string literals (Java)

From Java

Calling two Java methods asynchronously

From Dev

Calling Java methods with React locally

From Java

calling java methods in javascript code

From Dev

Calling methods in a Java Fx ActionEvent

From Dev

Generics in Java preventing calling of methods

From Dev

Calling methods for my java class

From Dev

Call multiple children methods from parent component

From Dev

Java threads calling parent functions

From Dev

Vue calling parent component function when child component function completes

From Dev

react: Calling child component's method from parent component

From Dev

Calling a method in the parent component from a child component using blazor server

From Dev

Calling parent component method from child component written in Jsx Vue

From Dev

Child Component in Parent Component is calling different function on click React

Related Related

  1. 1

    Calling the method of the parent Vue component

  2. 2

    Calling a method on Parent Component from Child Component

  3. 3

    Child component is not calling parent component function

  4. 4

    XPages - Calling Java methods

  5. 5

    Calling the methods in java

  6. 6

    Calling Java Methods in XSLT

  7. 7

    Calling multiple methods in Java

  8. 8

    Calling the methods in a class in Java

  9. 9

    Calling methods on objects Java

  10. 10

    Multiple buttons in the child component with the methods in the parent component

  11. 11

    Calling methods of actual object instead of parent class

  12. 12

    Calling a children method of a functional component from parent

  13. 13

    Calling Parent Function in Child Component in React

  14. 14

    Calling parent functions from child component

  15. 15

    React: problem calling child function in parent component

  16. 16

    Calling methods on string literals (Java)

  17. 17

    Calling two Java methods asynchronously

  18. 18

    Calling Java methods with React locally

  19. 19

    calling java methods in javascript code

  20. 20

    Calling methods in a Java Fx ActionEvent

  21. 21

    Generics in Java preventing calling of methods

  22. 22

    Calling methods for my java class

  23. 23

    Call multiple children methods from parent component

  24. 24

    Java threads calling parent functions

  25. 25

    Vue calling parent component function when child component function completes

  26. 26

    react: Calling child component's method from parent component

  27. 27

    Calling a method in the parent component from a child component using blazor server

  28. 28

    Calling parent component method from child component written in Jsx Vue

  29. 29

    Child Component in Parent Component is calling different function on click React

HotTag

Archive