code_junkie

Java actionListener for a nameless JButton?

Nov 12th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. for (int i = 65; i < 91; i++){
  2. alphabetPanel.add(new JButton("<html><center>" + (char)i));
  3. }
  4.  
  5. ActionListener listener = something;
  6.  
  7. for (int i = 65; i < 91; i++){
  8. JButton button = new JButton("<html><center>" + (char)i);
  9. button.addActionListener( listener );
  10. alphabetPanel.add(button);
  11. }
  12.  
  13. for (int i = 65; i < 91; i++){
  14. JButton button = new JButton("<html><center>" + (char)i));
  15. button.addActionListener( new ButtonListener());
  16. alphabetPanel.add(button);
  17. }
  18.  
  19. class ButtonListener implements ActionListener {
  20. ButtonListener() {
  21. }
  22. public void actionPerformed(ActionEvent e) {
  23. //TODO:
  24. }
  25. }
  26.  
  27. button.setName((char)i)); // or button.setName(i);
  28.  
  29. alphabetPanel.add(new JButton("<html><center>" + (char)i) {{
  30. addActionListener(new ActionListener() {
  31. public void actionPerformed(ActionEvent event) {
  32. ...
  33. }
  34. });
  35. }});
  36.  
  37. alphabetPanel.add(new JButton(new AbstractAction("<html><center>" + (char)i) {
  38. public void actionPerformed(ActionEvent event) {
  39. ...
  40. }
  41. }));
  42.  
  43. Form alphabetForm = new Form(alphabetPanel);
  44. for (char c='A'; c <= 'Z'; ++c) {
  45. alphabetForm.button("<html><center>" + c, new ActionListener() {
  46. public void actionPerformed(ActionEvent event) {
  47. ...
  48. }
  49. });
  50. }
  51.  
  52. new JButton().addActionListener(new ActionListener(){
  53. @Override
  54. public void actionPerformed(ActionEvent arg0) {
  55. // TODO your action
  56. }
  57. });
  58.  
  59. for (int i = 65; i < 91; i++){
  60. alphabetPanel.add(new JButton("<html><center>" + (char)i).addActionListener(new ActionListener(){
  61. @Override
  62. public void actionPerformed(ActionEvent arg0) {
  63. // TODO your action
  64. }
  65. }));
  66. }
Add Comment
Please, Sign In to add comment