2

Say, you have a subclass of JFrame, and use it to create your own custom JFrame. In this class (we'll call it mainFrame), we create a reference to another custom JFrame class (we'll call this one sidePanel).

In sidePanel, you have different buttons, radio buttons,..

My question is, is there a way to notify mainFrame the user presses on a button?

I've created a (untested) example of what I mean:

class mainFrame extends JFrame {
   public mainFrame() {
      super("main frame");
      //...........
      sidePanel panel = new sidePanel();
      //...........
   }
   public static void main(String[] args) {
      mainFrame mainF = new mainFrame();
      //.........
   }
}

And the sidePanel class:

class sidePanel extends JFrame {

   public sidePanel() {
      super("sidePanel frame");
      //...........
      JButton button1 = new JButton();
      button1.addActionListener(new ActionListener() {  
          public void actionPerformed(ActionEvent e)
            {
               //Notify mainFrame somehow button is pressed
          }});
      //...........
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Charnia
  • 605
  • 1
  • 5
  • 6
  • *"Say, you have a subclass of JFrame, and use it to create your own custom JFrame. In this class (we'll call it mainFrame, we create a reference to another custom JFrame.."* That is two design mistakes within 2 sentences. Nice going! 1) An app. typically needs a reference to a frame rather than a class that extends frame. 2) Don't use more than one frame. There are a variety of alternatives to displaying the rest of the content. – Andrew Thompson Jan 22 '12 at 10:08
  • @AndrewThompson Well, I just wanted to make an application with a main frame and a side frame like GIMP or a lot of other painting programs. Just using 2 JFrames was the easiest thing to do. What do you suggest instead? I'm open for other ideas. – Charnia Jan 22 '12 at 10:14
  • *"like GIMP"* GIMP is often sited as a classic example of why we should not foist multi-frame applications on the (hapless) end user. *"I'm open for other ideas."* 1) One `JFrame` that owns many `JDialog` instances. 2) A `JDesktopPane` with many `JInternalFrame` instances 3) One `JFrame` with many floatable `JToolBar` instances.. – Andrew Thompson Jan 22 '12 at 10:53
  • Other suggestion: a JSplitPane, with the side pane... on the side. – JB Nizet Jan 22 '12 at 11:08
  • Thanks for the tips, I have chose for the JToolBar option :) – Charnia Jan 22 '12 at 21:38

1 Answers1

2

To notify mainFrame of an event, the SidePanel instance (really bad name for a Frame) must have a reference to mainFrame. Pass mainFrame as an argument of the SidePanel constructor, and callback mainFrame from the actionPerformed method in SidePanel:

SidePanel panel = new SidePanel(this);

and in SidePanel:

public void actionPerformed(ActionEvent e) {
    mainFrame.buttonHasBeenClicked();
    ...
}

This tightly couples both classes though. A way to decouple them is to make the SidePanel object accept listeners for custom events, and to fire such an event when the button is clicked. The mainFrame would construct the SidePanel instance, and add itself (or an inner anonymous class instance) as a listener to the sidePanel.

See this page for an example.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your answer and the link, but are there other ways of doing it (with for example, threading)? Thanks – Charnia Jan 22 '12 at 10:38
  • All event-handling code in a Swing app is executed in the event-dispatch thread. And anyway, threading is used to execute multiple tasks in parallel, not to communicate between objects. I don't see how threading is relevant for your problem. – JB Nizet Jan 22 '12 at 11:07
  • +1 for `EventListenerList`; see also this [answer](http://stackoverflow.com/a/3072979/230513). – trashgod Jan 22 '12 at 12:32