Calling methods of the parent component in Java

I have the following situation, I think it’s best to show in the sample program code. I have a Java class that extends JPanel . There are two objects in this class, two more JPanels . One of the JPanel objects contains a JTable . I added a listener to this JTable , which detects a double click. When it detects a double click, I want to run the method in the upper class. How to 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 } } } ); } } 
+4
source share
5 answers

One way to solve this problem is to use composition instead of inheritance. You can pass JPanel to your JTable subclass.

 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(); } } 
+7
source

To access the method in the Outer class, you need to use the Outer.this.method () syntax.

So, in this case, you must use TheTable.this.callMethod() to access the "TheTable" class method from the listener.

I guess TopPanel added TopPanel to it. In this case, you can do something like:

 mouseClicked(...) { TopTable t = (TopTable)TheTable.this.getParent(); t.MethodToFire(); } 

This is a direct answer to the question, not the β€œbest” way to do it. A type-based approach would be the best long-term solution.

+4
source

You seem to be going around - why not just make everything from the outermost class? This is one of the reasons listeners exist:

 public class TopPanel { public TopPanel() { // Construct everything here OnePanel.TheTable.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ if (e.getClickCount() == 2){ MethodToFire } } } ); } } 
+2
source

You can set the Tetable tag to an event when a mouse is clicked that your TopPanel can listen to. This has the advantage that the Table and Panel classes are independent of each other.

The code is a bit more involved, but the denouement may be worth it.

 public class TopPanel extends JPanel { private TheTable table; public TopPanel() { table = new TheTable(this); table.addTheTableListener(new TheTableListener() { public void tableSelected() { // or tableSelected(TableSelectionEvent e) if you want to make a TableSelectionEvent methodToFire(); } }); } public void methodToFire() { } } public class TheTable extends JTable { private TopPanel panel; private EventListenerList listeners; public TheTable(TopPanel panel) { this.panel = panel; listeners = new EventListenerList(); this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { fireTableSelected(); } } ); } public void addTheTableListener(TheTableListener l) { listeners.add(l); } // also write removeListener private void fireTableSelected() { for (TheTableListener l : listeners) { l.tableSelected(); } } public interface TheTableListener extends EventListener { public void tableSelected(); } 
+2
source

I don't think Java has direct syntax for this unless you start playing with reflection (which, of course, is possible, but I don't find it very clean).

The moment you want to make a call, your "this" refers to the anonymous subclass of MouseAdapter.

The cleanest approach may be to create a private method, let it call X in TheTable and ask the adapter to execute this function.

Now, since this method X is in TheTable, it does not know that it is inside the panel, so it has no way to activate this function. If you are sure that each table is inside the panel, then you can add the panel link field to your Table class, initialize it when you create it (you may need to change the constructor to OnePanel), and then make a call from the method in TheTable.

If the panel method ultimately tries to explore not part of JPanel, you may need to create and pass interfaces.

0
source

All Articles