Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

buttons and troubles...

Thread view: 
man4*.* - 05 Nov 2006 21:15 GMT
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[i] = new JButton(paint[0]);}

putted 9 ActionListeners:

Container cp = getContentPane();
 cp.setLayout(new FlowLayout());
 for ( int i = 0; i < 9; i++)
  cp.add(button[i]);
 for ( int i = 0; i < 9; i++)
  button[i].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]);
Daniel Pitts - 05 Nov 2006 22:28 GMT
> I've got 9 buttons that are presented with one icon, and when I press that
> button I want to change it, but
[quoted text clipped - 28 lines]
> 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.
man4*.* - 06 Nov 2006 09:58 GMT
> 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...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.