4 Replies - 1830 Views - Last Post: 08 May 2011 - 11:31 AM Rate Topic: -----

#1 FljpFl0p  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Question about passing variables between JPanels

Posted 08 May 2011 - 09:31 AM

I figure pasting my code here would just make it complicated so I'm just gonna write a simple code fragment.

Panel A:
public class PanelA extends JPanel
{
    private JButton button;
    
    public PanelA()
    {
        button = new JButton("");

        add(button);
    }
}



Panel B:
public class PanelB extends JPanel
{
    private PanelA panelA;

    public PanelB()
    {
        panelA = new PanelA();
        add.(PanelA);
    }
}


Basically, 'button' is added to 'panel A' and 'panel A' is added to 'panel B'. Now I need to pass a String from 'panel A' to 'panel B' everytime 'button' is clicked. It would be great if there's a way for 'panel B' to listen to the button, but I'm not sure if that's possible or not. The best option I have right now is to put the class 'panelA' inside the class 'panelB' so that they can share the String or even the button itself but I'd like to try other options.

If anyone has any way to solve this problem, please tell me!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Question about passing variables between JPanels

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Question about passing variables between JPanels

Posted 08 May 2011 - 09:55 AM

You have to let panelB implement ActionListener, and then add it as listener to the button

    public PanelA(PanelB panelB)
    {
        button = new JButton("");
		button.addActionListener( panelB );
        add(button);
    }


Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Question about passing variables between JPanels

Posted 08 May 2011 - 10:01 AM

Some prefer that buttons (and other components) have their own listeners, usually implemented as anonymous inner classes. In your case, when your PanelA button is pressed, the listener could be programmed to pass the desired value to PanelB using a PanelB mutator (or setter) method.
Was This Post Helpful? 0
  • +
  • -

#4 FljpFl0p  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Re: Question about passing variables between JPanels

Posted 08 May 2011 - 11:11 AM

CaSiOo's method makes the PanelB able to listen to button, but I can't figure out a way to pass the String to PanelB
I created an inner listener class in PanelA for the button and plan to use the mutator in this listener, but I can't refer to the PanelB because it's in the PanelA constructor, not in the class PanelA.
Using CoSiOo's code, mine would look like this:
 public class PanelA extends JPanel
{
    public PanelA(PanelB panelB)
    {
        button = new JButton("");
        button.addActionListener(new ButtonListener());
        button.addActionListener( panelB );
        add(button);
    }
    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            panelB.mutator();
        }
    }
}

This post has been edited by FljpFl0p: 08 May 2011 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 FljpFl0p  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Re: Question about passing variables between JPanels

Posted 08 May 2011 - 11:31 AM

The mutator seems to work this way, but another problem surfaces, the listener in PanelB runs before the ButtonListener, which means by the time the PanelB "//do something", there aren't any mutating done yet.
public class PanelA extends JPanel
{
    private JButton button;
    PanelB temp;

    public PanelA(PanelB panelB)
    {
        temp = panelB;
        button = new JButton("");
        button.addActionListener(new ButtonListener());
        button.addActionListener(temp);
        add(button);
    }
    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            temp.mutator();
        }
    }
}

public class PanelB extends JPanel implements ActionListener
{
    private PanelA panelA;

    public PanelB()
    {
        panelA = new PanelA(this);
        add.(panelA);
    }

    public void actionPerformed(ActionEvent e)
    {
        //do something
    }

    public void mutator()
    {
        
    }
}

It's 2:30AM here so I think I'm gonna get some sleep now, just saying in case someone has a solution, they'll know that I can't thank them right away.

This post has been edited by FljpFl0p: 08 May 2011 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1