Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default How can I pass the JPanel as a reference with a button click ?

    Java Code:
    public class LanguagePanel extends JPanel
    {
    	private JButton turkishButton = new JButton("Turkish");
    	private JButton englishButton = new JButton("English");
    	private MainFrame mainFrame;
    	
    	public LanguagePanel(MainFrame frame)
    	{
    		this.mainFrame = frame;
    		englishButton.addActionListener(new EnglishButtonListener());
    		this.add(englishButton);
    		this.add(turkishButton);
    	}
    	
    	class EnglishButtonListener implements ActionListener
    	{
    
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			mainFrame.setContentPane(this);
    		}
    		
    	}
    }
    In my actionPerformed method, I want to pass the LanguagePanel object as the parameter, but this method as it is right now is passing the button.

    Is there anyway I can pass the JPanel outer object that is holding the Button object as the parameter ?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: How can I pass the JPanel as a reference with a button click ?

    Actually, I would expect that to be passing the ActionListener. Are you just looking for the LanguagePanel.this keyword?

    PS- This smells like a job for CardLayout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: How can I pass the JPanel as a reference with a button click ?

    I guess I should quit being lazy and learn CardLayout!! >:

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: How can I pass the JPanel as a reference with a button click ?

    I could be wrong, but it looks like you want to change which JPanel is displayed when a button is pressed. If that's the case, then CardLayout is your friend here. If not, then I just don't fully understand your goals.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 8
    Last Post: 02-23-2012, 07:08 PM
  2. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 09:17 AM
  3. How to create pass by reference??
    By --> xeiyne! in forum CLDC and MIDP
    Replies: 4
    Last Post: 04-08-2010, 07:43 PM
  4. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 07:06 PM
  5. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •