buttons and troubles...

Discussion in 'Java' started by man4*.*, Nov 5, 2006.

  1. man4*.*

    man4*.* Guest

    I've got 9 buttons that are presented with one icon, and when I press that
    button I want to change it, but
    I've complicated a little bit... (hope, you'll understand what I need, if
    not, I'll send whole code)

    button initialization:

    JButton[] button = new JButton[9];
    {for ( int i = 0; i < 9; i++)
    button = new JButton(paint[0]);}

    putted 9 ActionListeners:

    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    for ( int i = 0; i < 9; i++)
    cp.add(button);
    for ( int i = 0; i < 9; i++)
    button.addActionListener(new AL());

    AL is a class that implements ActionListener,
    also I've got 2 icons:

    static Icon[] paint = {
    new ImageIcon(path + "icon1.gif"),
    new ImageIcon(path + "icon2.gif")};

    i want each time I press a button to change from icon1 to icon2.
    in actionPerformed method I followed yours advices and put
    Object buttonSource = e.getSource();
    but since my buttons are Array obj. I can't say:
    buttonSource.setIcon(paint[1]);
    man4*.*, Nov 5, 2006
    #1
    1. Advertising

  2. man4*.*

    Daniel Pitts Guest

    man4*.* wrote:
    > I've got 9 buttons that are presented with one icon, and when I press that
    > button I want to change it, but
    > I've complicated a little bit... (hope, you'll understand what I need, if
    > not, I'll send whole code)
    >
    > button initialization:
    >
    > JButton[] button = new JButton[9];
    > {for ( int i = 0; i < 9; i++)
    > button = new JButton(paint[0]);}
    >
    > putted 9 ActionListeners:
    >
    > Container cp = getContentPane();
    > cp.setLayout(new FlowLayout());
    > for ( int i = 0; i < 9; i++)
    > cp.add(button);
    > for ( int i = 0; i < 9; i++)
    > button.addActionListener(new AL());
    >
    > AL is a class that implements ActionListener,
    > also I've got 2 icons:
    >
    > static Icon[] paint = {
    > new ImageIcon(path + "icon1.gif"),
    > new ImageIcon(path + "icon2.gif")};
    >
    > i want each time I press a button to change from icon1 to icon2.
    > in actionPerformed method I followed yours advices and put
    > Object buttonSource = e.getSource();
    > but since my buttons are Array obj. I can't say:
    > buttonSource.setIcon(paint[1]);


    Actually, you can, but you need to cast buttonSource to a JButton.

    JButton buttonSource = (JButton)e.getSource();
    buttonSource.setIcon(paint[1]);

    That should work. e.getSource() will return the Object that created
    the event, in this case *one* of the 9 buttons. e.getSource() return
    type is Object, because the source "could" be anything, but in this
    case you know it is a JButton, and just need to cast. To be on the
    safe side, you probably should check instanceof JButton, although this
    isn't something I do often, but I'm not usually a GUI programmer.

    Well, good luck.
    -Daniel.
    Daniel Pitts, Nov 5, 2006
    #2
    1. Advertising

  3. man4*.*

    man4*.* Guest

    > Actually, you can, but you need to cast buttonSource to a JButton.
    >
    > JButton buttonSource = (JButton)e.getSource();
    > buttonSource.setIcon(paint[1]);


    THX, now I understand what get wrong...
    man4*.*, Nov 6, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    1
    Views:
    615
    GreggTB
    May 20, 2005
  2. =?Utf-8?B?QmVu?=

    Image Buttons/Buttons not responding

    =?Utf-8?B?QmVu?=, Jul 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Teemu Keiski
    Jul 6, 2005
  3. Jeremy Jones

    Datagrid Nav buttons and numeric buttons

    Jeremy Jones, Mar 21, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    603
    Masudur
    Mar 22, 2007
  4. Jonathan N. Little
    Replies:
    3
    Views:
    515
    Andy Dingley
    Apr 2, 2007
  5. Dr. Leff
    Replies:
    3
    Views:
    281
    David Mark
    Oct 15, 2007
Loading...

Share This Page