Java Swing Events/Listeners

There are two ways to implement Swing Events/Listeners:

1. Have everything in 1 file and have anonymous inner classes that implement the listeners.

2. Have a main GUI class that has separate classes of JPanels that implement the listeners.

I have worked with case 1 where everything is in one file so that I can call functions within the one GUI class when an event is caught. I am having problems with approach 2 because I do not know how to call functions in the main GUI class when an event is caught in the separate JPanel classes.

Right now I have a GUI.java and a TreePanel.java, GUI.java is the main class and contains a panel TreePanel. TreePanel implements TreeSelectionListener and extends JPanel and catches an event. My question is, how do I call a function in the GUI class from TreePanel? Doing a "super" in TreePanel will give me JPanel. Doing getParent() on TreePanel's parent containers until it reaches the JFrame of GUI didn't help because my GUI is not a JFrame.

Any ideas?

[1033 byte] By [tomkitta] at [2007-11-26 12:46:43]
# 1

> how do I call a function in the GUI class from TreePanel?

TreePanel needs a reference to GUI. So for instance when in GUI you

create a new TreePanel, you could pass "this" as an argument to

TreePanel's constructor.

GUI should implement some method that is carried out in response to

selection events. This method should be accessible from TreePanel.

Then, when a TreePanel object detects a selection event, it calls the method

in the GUI (to which it has a reference).

[Edit] I forgot to add that you really should post this sort of question in the Swing

forum. It strikes me that there are lots of ways to organise things (should

GUI be a JFrame, or contain a JFrame? Should GUI expose a method

which theTreePanel uses, or should the TreePanel provide a method

so that the GUI can make itself a listener for the selection events.) The best

knowledge - and hence the best answers - will be in the Swing forum.

pbrockway2a at 2007-7-7 16:27:20 > top of Java-index,Java Essentials,Java Programming...
# 2

This does what you are talking about (I think) but with a button rather than a

list./*

* File: listen/GUI.java

*/

package listen;

import javax.swing.JFrame;

import javax.swing.UIManager;

public class GUI {

JFrame frame;

GUI() {

frame = new JFrame("Listener test");

// Pass the ButtonPanel a reference to "this" so it

// it knows who to inform about events.

frame.add(new ButtonPanel(this));

}

public void processClick() {

System.out.println("Ding!");

}

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {}

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

JFrame.setDefaultLookAndFeelDecorated(true);

GUI app = new GUI();

app.frame.pack();

app.frame.setVisible(true);

}

});

}

}

// End file listen/GUI.java

/*

* File: listen/ButtonPanel.java

*/

package listen;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JPanel;

public class ButtonPanel extends JPanel implements ActionListener {

// The GUI that should be informed about events

private GUI app;

public ButtonPanel(GUI app) {

this.app = app;

JButton but = new JButton("Click me!");

but.addActionListener(this);

add(but);

}

// We catch the button clicks here

public void actionPerformed(ActionEvent e) {

// And pass it on to the GUI container/"parent"

app.processClick();

}

}

// End file listen/ButtonPanel.java

[Edit]Changed example when I read more closely and realised GUI

was not intended to be a JFrame.

pbrockway2a at 2007-7-7 16:27:20 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks! I got the conceptual part in your first response and thought you were right on. And yes, it is true that there are a lot of ways to organize this -- and that is why I couldn't figure this out. E.g. Eclipse organizes it's Swing components in a certain way and NetBeans organizes it in a certain way.

Thanks again for the solution/

tomkitta at 2007-7-7 16:27:20 > top of Java-index,Java Essentials,Java Programming...