So I'm basically in a predicament. I'm trying to find the location of a JButton using the .getLocation() accessor, however this only seems to work for me if I put it after the window is set as visible - otherwise it only returns x as 0 and y as 0. I cannot put this after the window is set visible in my particular case as if I do this, the buttons aren't initialised in the first place. I'm basically wondering if their is an accessor that finds the location of these buttons before the window is set as visible? Here's the code I'm using that returns x & y as 0:
public class Test {
public Test() {
JButton buttonOne = new JButton();
JButton buttonTwo = new JButton();
JPanel panel = new JPanel();
JFrame window = new JFrame("Test");
GridLayout g = new GridLayout();
panel.add(buttonOne);
panel.add(buttonTwo);
panel.setLayout(g);
System.out.println(buttonTwo.getLocation());
window.setContentPane(panel);
window.setSize(512, 512);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}