I have a Frame on which I have created a grid architecture of 12*16 (putting labels on panels) and put an image in each grid cell. When I try to get the origin coordinates of each label using getBounds()
and getX()
and getY()
method. It is showing (0,0)
.
I am not getting why it is showing (0,0)
for all labels. Is there any way to access the coordinate of origin of a component.
Here is my piece of code.
public static void main(String[] arg){
Toolkit tk = Toolkit.getDefaultToolkit();
int xsize = (int)tk.getScreenSize().getWidth();
int ysize =(int) tk.getScreenSize().getHeight();
String str,str1;
Border border = LineBorder.createGrayLineBorder();
BufferedReader bout;
BufferedImage image= null;
JPanel[] panel;
panel = new JPanel[16];
button = new JLabel[16];
JFrame frame = new JFrame("Image Display");
GridLayout g = new GridLayout();
FileInputStream file;
ResizeFunction a = new ResizeFunction();
try {
file = new FileInputStream("D:\\cache1\\newtask_list.txt");
bout = new BufferedReader(new InputStreamReader(file));
str = bout.readLine();
for(int j=0;j<16;j++){
panel[j] = new JPanel();
panel[j].setLayout(g);
g.setColumns(1);
g.setRows(12);
for(int i=0;i<12;i++){
button[i] = new JLabel();
button[i].setSize(xsize/16, ysize/12);
int x=(int) button[i].getBounds().getX();
int y=(int) button[i].getBounds().getY();
System.out.println("button["+i+"]["+j+"]="+x+","+y);
button[i].setIgnoreRepaint(true);
str= bout.readLine();
str1 = str.substring(0,23);
image = ImageIO.read(new File("D:\\cache1\\newtask\\"+str1));
BufferedImage b = a.resizeImage(image,image.getWidth(),image.getHeight(),button[i].getWidth(),button[i].getHeight());
ImageIcon icon = new ImageIcon(b);
button[i].setIcon(icon);
button[i].setBorder(border);
panel[j].add(button[i]);
}
frame.add(panel[j]);
}
} catch (Exception ex) {
Logger.getLogger(ButtonGrid.class.getName()).log(Level.SEVERE, null, ex);
}
frame.setLayout(new GridLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(xsize, ysize);
frame.setVisible(false);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
MouseCursorXYLabel xy = new MouseCursorXYLabel();
xy.displayJFrame(frame);
ButtonGrid panel1 = new ButtonGrid();
}
The reason why they all have the coordinates (0,0) is that, by the time you ask for x and y, they don't have any position yet. The frame gets displayed at the bottom of your method.
from getBounds()
:
The bounds specify this component's width, height, and location relative to its parent
When you get the x and y coordinates of your label, the label does not have a parent, nor is the parent panel added to the frame, nor is the frame visible (it does not have any size).
Additionally, in your second loop you create the new JLabel instances (192 in total), but you put them all in an array of size 16. So you are overwriting the references in you array for each JPanel.
What you could do is put all new JLabels in a 2 dimensional array:
JLabel[][] buttons = new JLabel[16][12]
After your frame gets displayed you execute something like this:
for(int i=0;i<16;i++){
for(int j=0;j<12;j++){
System.out.println("button[" + i + "][" + j + "]=" + buttons[i][j].getBounds().x + "," + buttons[i][j].getBounds().x);
}
}