Cant access JButtons outside of MouseListener - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Cant access JButtons outside of MouseListener

markrpmarkrp Posts: 1Member
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

Comments

  • AntoineAntoine Posts: 12Member
    : 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{

    }
Sign In or Register to comment.