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)
Code :
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:
Code :
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 :
Code :
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
Re: How to add Image to my JButtons?
Quote:
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.
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
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?
1 Attachment(s)
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
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.
Re: How to add Image to my JButtons?
Sorry for the long reply:
Code :
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);
}
}
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.
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]
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.
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 ?
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?
Re: How to add Image to my JButtons?
it puts it at the end of the grid :/
this is what i basically want:
7x7
Code :
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....
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?
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
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:
Code :
button[0][1].setIcon(peg);
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 :)
Re: How to add Image to my JButtons?