Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need Help with Arrays

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need Help with Arrays

    Hi, i am new here so i did not really know if i post it on the correct section. Still i got a question to ask. How do i insert different images into a array of buttons i have created. I have attached an example of the results. SnapShot.jpg

    More details would be that my teacher told me to use arrays to create JButtons, in each of the JButtons, there must be a different image.

    I am really new to programming and i would appreciate all kinds of helps. Thank You!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help with Arrays

    How do i insert different images into a array of buttons i have created.
    You can not insert images into an array of buttons.
    You can set the image for each button in the array.
    For example:
    JButton[] someBtns = ... // define an array of buttons
    ...
    someBtns[ix].<THE METHOD TO SET THE IMAGE>(theImage); // set image for a button in the array

  3. The Following User Says Thank You to Norm For This Useful Post:

    zennbang (July 30th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Arrays

    Thanks Norm!

    I just want to double check, if my coding is correct.

    centerPnl = new JPanel(new GridLayout(3, 3, 5, 5));

    picBtn = new JButton[9];
    for (int i = 0; i < picBtn.length; i++) {
    picBtn[i] = new JButton("");
    centerPnl.add(picBtn[i]);
    }
    picBtn[0].setIcon(new ImageIcon("pic01.jpg"));
    picBtn[1].setIcon(new ImageIcon("pic02.jpg"));
    picBtn[2].setIcon(new ImageIcon("pic03.jpg"));
    picBtn[3].setIcon(new ImageIcon("pic04.jpg"));
    picBtn[4].setIcon(new ImageIcon("pic05.jpg"));
    picBtn[5].setIcon(new ImageIcon("pic06.jpg"));
    picBtn[6].setIcon(new ImageIcon("pic07.jpg"));
    picBtn[7].setIcon(new ImageIcon("pic08.jpg"));
    picBtn[8].setIcon(new ImageIcon("pic09.jpg"));

    Thanks for you help!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help with Arrays

    I just want to double check, if my coding is correct.
    What did the compiler say?

Similar Threads

  1. Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2011, 01:21 PM
  2. 3 Arrays
    By D3158 in forum Collections and Generics
    Replies: 27
    Last Post: June 16th, 2011, 11:10 AM
  3. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM