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 18 of 18

Thread: How to add Image to my JButtons?

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to add Image to my JButtons?

    I am creating a game at the moment, it consists of a 2D array full of buttons with a gridLayout of (7,7)

    ImageIcon myImage = new ImageIcon("C:\\Temp\\Image1.gif");
            JButton[][] button = new JButton[7][7];
            this.setLayout(new GridLayout(7, 7));
     
     
            for(int i = 0; i < 7; i++) {
                for(int j = 0; j < 7; j++) {
                    //button[i][j].addActionListener(this);
                    button[i][j] = new JButton();
                    add(button[i][j]);
                }
            }

    At the moment this fills up my array with blank buttons.

    I want to add an image to a specific location in my array, example [1][1].
    I have tried underneath the for loop:
    button[1][1].setIcon(peg);
            add(button[1][1]);
    But it doesn't display the image in the correct button ?
    Is there an easier way to add these images?

    I have also tried :

    ImageIcon myImage = new ImageIcon("C:\\Temp\\Image1.gif");
            JButton[][] button = new JButton[7][7];
            this.setLayout(new GridLayout(7, 7));
     
     
            for(int i = 0; i < 7; i++) {
                for(int j = 0; j < 7; j++) {
                    //button[i][j].addActionListener(this);
                    button[i][j] = new JButton(myImage);
                    add(button[i][j]);
                }
            }

    This will fill up my array of buttons with the images, so how would I go about setting a button say in position [1][1], to be blank?

    Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    But it doesn't display the image in the correct button ?
    Does the image show in any of the buttons? If so, then you need to figure out why the location of the button is not where you want it to be.

    How do the locations of the buttons in the 2D array correlate with their locations in the GUI display?
    Try setting the text of the buttons with their i,j locations in the 2D array to see.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    I have set the locations of i & j in the 2D array.

    it shows, as it should : 0,0 | 0,1 | 0,2 etc..

    But when i do :

    button[0][1].setIcon(peg);
    add(button[0][1]);


    It will display the icon in the button n [7] [7] each time

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    So the only button with an image is the last one.
    Strange that the location is [7][7] when the array does not have 8 slots. The max index for a 7 element array is 6.

    Can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    Sorry I had to change it, was originally coming up at [6][6] like you said, but even now, it still does not come up at the right space.

    I have added the .zip of my array, it will compile and run, the buttons all come up 7x7 grid.

    I have commented out the code I am trying to do under MyDrawingPanel class.

    Thank you again
    Attached Files Attached Files

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    Please post the small test program here on the forum so it can be copied for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    Sorry for the long reply:

    import java.awt.*;
    import javax.swing.*;
     
     
    public class Array extends JFrame{
     
        public static void main(String[] args) {
            new Array();
        }
     
        MyDrawingPanel myDrawingPanel = new MyDrawingPanel(this);
     
        public Array()
        {
            setSize(1160,660); 
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            Container cp = getContentPane();    
            cp.setLayout(new FlowLayout());
            cp.add(myDrawingPanel);
            setBackground(Color.white);
     
            setVisible(true);
     
        }    
    }
     
    class MyDrawingPanel extends JPanel {
        Array parentRef;
        public MyDrawingPanel(Array parentRef)
        {
            this.parentRef=parentRef;
            setPreferredSize(new Dimension(800,600));
            setBackground(Color.red);
     
            String blank = "-"; //location empty
            String full = "0"; //location full
     
            int k = 0;
     
            ImageIcon peg = new ImageIcon("C:\\Temp\\PEG1.gif");
            JButton[][] button = new JButton[8][8];
            this.setLayout(new GridLayout(7, 7));
     
            for(int i = 0; i < 7; i++) {
                for(int j = 0; j < 7; j++) {
                    //button[i][j].addActionListener(this);
                    button[i][j] = new JButton();
                    add(button[i][j]);
                }
            }
     
            //Will not add button to correct space
            //button[0][1] = new JButton(peg);
            //add(button[0][1]);
        }
     
        public void paint(Graphics g)
        {
            super.paint(g);       
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    What is supposed to happen when the code executes that shows the problem?
    When I execute the code a grid is displayed. There are no images shown.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    Yes, at first no images are shown.

    I then want to add an image to 'some' of the buttons. For example add a image to button in position [0][1]

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    If you want the button at location [0][1] to show an image you need to set the icon for that button to the image.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    That's what I was trying to do with :

    button[0][1].setIcon(peg);
    add(button[0][1]);

    I take it that is wrong ?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    The button at [0][1] has already been added to the GUI and is being shown. Where will adding it again put it?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    it puts it at the end of the grid :/
    this is what i basically want:

    7x7
       xxx   
       xxx
    xxxxxxxx
    xxxxxxxx
    xxxxxxxx
       xxx
       xxx

    So all buttons will be filled apart from the ones shown, [0][0], [0][1], [1][0],[1][1] and so on....

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    Do you understand what your code was doing?
    Are you able to have the program do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    From what I can gather my code is creating a 2D array of buttons in the grid layout 7x7. The for loops then add the rows and columns to the layout.

    I could have the for loops to set the buttons to already have the image inside them, but then I am stuck in the same position of trying to make some of the buttons null to display nothing.

    I assume what my code is doing, is adding the buttons in the layout 7x7, then I am trying to make one of the buttons in a certain position change to either have an image or have nothing, but instead of overriding the button it is adding another to the grid.

    I'm not sure what to do to avoid this problem. Thanks for all your help so fat

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    The button you want to change is in the array and has been added to the GUI and is being shown.
    Don't add it to the GUI again unless you want to change its position. You can access the button and its methods via its reference in the array. Call one of the button's methods to change it:
    button[0][1].setIcon(peg);
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to add Image to my JButtons?

    It seems so simple now you have said it, thank you so much!!
    I don't know why I kept trying to add a new JButton!
    Thank you

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add Image to my JButtons?

    Glad it's working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JButtons not showing
    By Shaybay92 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 27th, 2011, 07:42 AM
  2. How can i add an image into my source code?
    By joelmeler in forum Java Theory & Questions
    Replies: 14
    Last Post: August 1st, 2011, 06:15 PM
  3. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  4. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM
  5. JTables with JButtons, I'm overlooking something
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2010, 11:30 AM