Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Cant access JButtons outside of MouseListener Posted by markrp on 5 Nov 2005 at 5:56 AM
I Have array of JButtons if a 8x8 grid I want to click a button and change the icon of another in the grid (not the one clicked), I dont seem to be able to access the array outside the mouse listener.

class test
{
//cant access this why?
private JButton but[] = new JButton[64];

private class MyMouseAdapter extends MouseAdapter
{

public void mouseClicked(MouseEvent event)
{
JButton button = (JButton)event.getSource(); // this gets the clicked button but
}
}

Any help would be great
Report
Re: Cant access JButtons outside of MouseListener Posted by Antoine on 7 Nov 2005 at 8:08 AM
: I Have array of JButtons if a 8x8 grid I want to click a button and change the icon of another in the grid (not the one clicked), I dont seem to be able to access the array outside the mouse listener.
:
: class test
: {
: //cant access this why?
: private JButton but[] = new JButton[64];
:
: private class MyMouseAdapter extends MouseAdapter
: {
:
: public void mouseClicked(MouseEvent event)
: {
: JButton button = (JButton)event.getSource(); // this gets the clicked button but
: }
: }
:
: Any help would be great
:

Thats because you just defined an inner class-the 'MyMouseAdapter'. (You cant access a field in another class like that-especially if its private.id advice you to not make it public..not good programming practice). It would work if you implemented the MouseListener interface instead which will mean you define all the other MouseListener methods within your class.
i.e
class Test implements MouseListenter{

}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.