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

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.