Question

I have a problem. I want to create an array of type

private JButton[] butte;
butte[3] = button;

Firing such code ends in failure

All the code

public void LoadAllConfigureToCard(JPanel map)
    {
        Component[] component = map.getComponents();
        for(int i=0; i<component.length; i++)
        {
            if (component[i] instanceof JButton)
            {
                final JButton button = (JButton)component[i];
                button.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent arg0) {
                if(nocarddrag)
                {
                    Rectangle butt = button.getBounds();
                    int w = butt.width;
                    int h = butt.height;
                    PointerInfo a = MouseInfo.getPointerInfo();
                    Point pt = a.getLocation();
                    Rectangle movc = new Rectangle(pt.x, pt.y, w, h);
                    button.setBounds(movc);
                    cardisdrag = true;

                    butte[3] = button;
                }
                }

                });

            }
        }

    }

What should I do to be able to give a prefix in an array of type int?

Was it helpful?

Solution

You need to first initialize your array:

private JButton[] butte = new JButton[100]; // or any other size you may want

OTHER TIPS

Try

JButton[] btnArray = new JButton[10];
btnArray[3] = someButton;

You forgot to create an object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top