Results 1 to 4 of 4

Thread: jbuttons

  1. #1
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default jbuttons

    Hi,

    Is there a way to implement an action listener for a Jbutton without a name. I want the first button to display A, second to display B....

    Java Code:
              for (int i = 0; i < whites.length; i++) {
              JButton whiteKeyBtn = new JButton();
              whiteKeyBtn.setBackground(Color.white);
              wPanel.add(whiteKeyBtn);
              }
    Regards,
    Sarah.

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by sarah jain View Post
    Is there a way to implement an action listener for a Jbutton without a name. I want the first button to display A, second to display B....
    Yes, of course. Do you want a single ActionListener that will listen to all the buttons, or one per button? Either way, you'd add it by calling button.addActionListener(ActionListener).

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    		ActionListener listener = new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				System.out.println(e.getActionCommand());
    			}
    		};
    //....
    			for (int i = 0; i < whites.length; i++) {
    //....
    			whiteKeyBtn.setActionCommand(String.valueOf((char)(i + 'A')));
    			whiteKeyBtn.addActionListener(listener);
    :confused:

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

Similar Threads

  1. Please help!JButtons
    By fourpixel in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2010, 01:48 PM
  2. Help; Missing JButtons
    By Cyprusice in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 11:50 PM
  3. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  4. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 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
  •