14 Replies - 2424 Views - Last Post: 18 March 2011 - 02:33 PM Rate Topic: -----

#1 yousufhussain52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-March 11

access outer class method from inner class

Posted 15 March 2011 - 07:34 PM

I am trying to navigate to another frame when i click on the button to do this i have use the mouse adapter class .
I am able to open the frame when i click on the button by using setVisible() but when i am trying to access the setVisible() funtion to make it invisible i am unable to do........
Please help.......
thanks in advance.........
startpage is outer class
Example code:::::::::::


admin_btn.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					Adminpage ap =new Adminpage();
					startpage.this.setVisible(false);//this is not working
					//this.setVisible(false);
					ap.setVisible(true);
					
					System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
				}
			});


Please :code:

This post has been edited by pbl: 15 March 2011 - 07:38 PM
Reason for edit:: Code tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: access outer class method from inner class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 15 March 2011 - 07:42 PM

This is why I never use anonymous class

And what is this suppose to be

startpage.this.

you cannot not have variableName.this
"this" references the current object it always start and expression

in an anonymous class this is useless, it references the anonymous class
Was This Post Helpful? 0
  • +
  • -

#3 yousufhussain52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-March 11

Re: access outer class method from inner class

Posted 16 March 2011 - 12:06 AM

View Postpbl, on 15 March 2011 - 07:42 PM, said:

This is why I never use anonymous class

And what is this suppose to be

startpage.this.

you cannot not have variableName.this
"this" references the current object it always start and expression

in an anonymous class this is useless, it references the anonymous class


The startpage is the name of the class in which the anonymous mouse event is declare.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: access outer class method from inner class

Posted 16 March 2011 - 08:42 AM

As pbl mentioned, I rarely use anonimyus classes.
If you have a lot of code and functionality for the MouseAdapter interface, better create an inner class, for it (or just implement the MouseAdapter at the class).
This will also make the code easier to read.

this.setVisible(true) will be enough if uyou use the anonimyus class.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 16 March 2011 - 06:22 PM

View Postjapanir, on 16 March 2011 - 10:42 AM, said:

this.setVisible(true) will be enough if uyou use the anonimyus class.

FALSE !!!
This is exactly why I hate anonymous classes and never used them... no reference to the class you are included in
this.setVisible(true); will try to make the MouseListener visible not the instance of the class it is included in and as the MouseListener class does not have a setVisible() method it won't even compile
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: access outer class method from inner class

Posted 16 March 2011 - 11:36 PM

pbl, You are right. for some reason I was thinking about an inner class.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 17 March 2011 - 05:23 PM

View Postjapanir, on 17 March 2011 - 01:36 AM, said:

pbl, You are right. for some reason I was thinking about an inner class.

This is why I always use inner classes never anonymous ones
Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: access outer class method from inner class

Posted 17 March 2011 - 05:51 PM

Here's an example of how to call methods in outer classes. It includes nested anonymous classes. It is the first program I have written with nested anonymous classes so I would like to dedicate it to pbl. ;)

import java.awt.event.ActionEvent;
importimport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class FrameSwitch extends JFrame {

	FrameSwitch() {
		JButton clicky = new JButton("Do me!");
		add(clicky);
		setSize(300, 300);
		setLocation(100, 100);

		clicky.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new JFrame() {
					/* static block */ {
						add(new JLabel("Yeah baby! Yeah!"));
						// Here I need to qualify the "this" reference with FrameSwitch
						// because the anonymous JFrame also has getSize() and getLocation() 
						setLocation(FrameSwitch.this.getLocation());
						setSize(FrameSwitch.this.getSize());
						// This demonstrates how it looks in the innermost class fir the method first.
						setVisible(true);
					}
				};
				// Here the "this" reference is not neccessary. The ActionListener has
				// no setVisible() method so it will bubble up to the outer class.
				// However, this.setVisible() would incorrectly refer to the ActionListener.
				// If I want to explicitly use this, I have to FrameSwitch.this.setVisible(false)
				setVisible(false);
			}
		});

		setVisible(true);
	}

	public static void main(String[] args) {
		new FrameSwitch();
	}

}

This post has been edited by cfoley: 17 March 2011 - 06:04 PM
Reason for edit:: whoops. I included a whole extra class in there!

Was This Post Helpful? 2
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 17 March 2011 - 06:03 PM

Sure, you do not access anything from within the class your anonymous class is in. You create new JFrame, new JLabel, ...

Better to put it in a separate method while you are at it, it would make your code lighter

Thank for the offrand anyway :)

This post has been edited by pbl: 17 March 2011 - 06:08 PM
Reason for edit:: typos

Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: access outer class method from inner class

Posted 17 March 2011 - 06:05 PM

Quote

Sure, you do not efrnce anything from within the class you are. You create new JFrame, new JLabel, ...


Not sure I understand what you're getting at here. :(

This post has been edited by cfoley: 17 March 2011 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 17 March 2011 - 06:09 PM

Sorry post edited
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: access outer class method from inner class

Posted 17 March 2011 - 06:18 PM

Oh, I see. I access the setVisible() method.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 17 March 2011 - 07:45 PM

View Postcfoley, on 17 March 2011 - 08:18 PM, said:

Oh, I see. I access the setVisible() method.

Yes the setVisible() method of an object Frame built within your anonymous class
But if your anonymous class is in a JFrame, as example, you cannot setVisible() the JFrame into which your anonymous class which was the OP original attempt
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: access outer class method from inner class

Posted 18 March 2011 - 01:51 AM

Yes, that's exactly what my program does. There are two setVisible() calls on two different objects in that program. I've modified the program below so that both are located in the nested anonymous class. To clarify, these are the two important lines:

						setVisible(true);
						FrameSwitch.this.setVisible(false);



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class FrameSwitch extends JFrame {

	FrameSwitch() {
		JButton clicky = new JButton("Do me!");
		add(clicky);
		setSize(300, 300);
		setLocation(100, 100);

		clicky.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new JFrame() {
					{
						add(new JLabel("Yeah baby! Yeah!"));
						setLocation(FrameSwitch.this.getLocation());
						setSize(FrameSwitch.this.getSize());
						setVisible(true);
						FrameSwitch.this.setVisible(false);

					}
				};
			}
		});

		setVisible(true);
	}

	public static void main(String[] args) {
		new FrameSwitch();
	}

}

Was This Post Helpful? 1
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: access outer class method from inner class

Posted 18 March 2011 - 02:33 PM

Never sen that one

setSize(FrameSwitch.this.getSize());

I think it is horrible there are much more OO oriented ways of doing it but I'll give you a +1 anyhow :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1