Results 1 to 4 of 4

Thread: Buttons Problem

  1. #1
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default Buttons Problem

    Java Code:
                for(int x = 0; x < 256; x++)
                {                
                    imageIcons[x] =  new ImageIcon(images[x]);
                    buttons[x] = new JButton(imageIcons[x]);
                    
                    buttons[x].setMnemonic(KeyEvent.VK_I);
                    buttons[x].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) 
                        {
                            pickedTileSetzen(x); //needs an integer , but it doesnt work with x - cause its in the for() and thats not 'final'.
                        }
                    });
                    MainFrame.getContentPane().add(buttons[x]);
                }
    i have 256 buttons and 256 ImageIcons.
    the buttons should do the method pickedTileSetzen(int x).
    the first button in the list should do pickedTileSetzen(0) and the last one pickedTileSetzen(255)
    but it doesnt work if i put x in there, cause x would have to be final.
    is there a trick with which i can solve that problem?
    i dont want to do 256 buttons one after another.. thats why i use 'for'.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    is there a trick with which i can solve that problem?
    Java Code:
    final int y = x;
    :
    :
        pickedTileSetzen(y);
    :
    db

  3. #3
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    thank you :)

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    is there a trick with which i can solve that problem?
    For a better trick create a single ActionListener that can be shared by all the buttons. Something like:

    Java Code:
    ActionListener al = new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            JButton button = (JButton)e.getSource;
            int index = Integer.parseInt(button.getActionCommand());
            pickedTileSetzen( index );
    };
    
    ...
    
    for (...)
    {
        ...
        buttons[x].setActionCommand("" + x);
        buttons[x].addActionListener( al );
    }
    Last edited by camickr; 08-25-2011 at 06:04 PM.

Similar Threads

  1. Renaming Buttons Problem
    By Huskies in forum New To Java
    Replies: 11
    Last Post: 07-29-2011, 12:53 AM
  2. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 08:13 AM
  3. [B]Tab Sequence problem with radio buttons[/B]
    By shobha2k8 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-25-2008, 07:25 AM
  4. How to use SWT Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •