Results 1 to 4 of 4
Thread: Buttons Problem
- 08-25-2011, 03:28 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 71
- Rep Power
- 0
Buttons Problem
i have 256 buttons and 256 ImageIcons.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]); }
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'.
- 08-25-2011, 03:31 PM #2
is there a trick with which i can solve that problem?dbJava Code:final int y = x; : : pickedTileSetzen(y); :
- 08-25-2011, 03:54 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 71
- Rep Power
- 0
thank you :)
- 08-25-2011, 06:02 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
For a better trick create a single ActionListener that can be shared by all the buttons. Something like:is there a trick with which i can solve that problem?
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
-
Renaming Buttons Problem
By Huskies in forum New To JavaReplies: 11Last Post: 07-29-2011, 12:53 AM -
Help with Buttons
By wld4ubabay in forum New To JavaReplies: 20Last Post: 05-17-2010, 08:13 AM -
[B]Tab Sequence problem with radio buttons[/B]
By shobha2k8 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-25-2008, 07:25 AM -
How to use SWT Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05-05-2008, 04:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks