1

I have a question about Java and JFrame in particular. Let's say I have a JFrame that I am using with a GridLayout. Supposing that I have added JButtons in the JFrame, how can I gain access to the one I want using it's position (by position, I mean a x and a y, used to define the exact place on the Grid). I have tried several methods, for instance getComponentAt(int x, int y), and have seen that those methods do not work as intended when combined with GridLayout, or at least don't work as intended in my case. So I tried using getComponent(), which seems fine.

The latest method, that seems to be on a right track for me is (assuming I have a JFrame with a GridLayout with 7 rows and 7 columns, x as columns, y as rows):

public JButton getButtonByXAndY(int x, int y) {
    return (JButton) this.getContentPane().getComponent((y-1) * 7 + x);
}

image http://www.freeimagehosting.net/newuploads/9d6hq.jpg

Using the above, say I want to get the JButton at (4, 4), meaning the 25th JButton in the JFrame, I would index through the first 21 buttons at first, and then add 4 more, finally accessing the JButton I want. Problem is this works like that in theory only. Any ideas?

P.S sorry for linking to an external website, but stack overflow won't allow me to upload the image, because I do not have the required reputation.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
  • It's probably better to just hold on to a reference to the object in the first place. Can you show us where you're adding them? – Tharwen May 31 '12 at 10:19
  • I am adding them on a class that Extends a JFrame using the JFrame.add(JButton) method with a for loop. – NlightNFotis May 31 '12 at 10:22
  • Why not put them into an `ArrayList` first, then put add all the buttons in the list to the JFrame? Then you'll have a reference to each one you can use later on instead of trying to work out which one you want from their positions. – Tharwen May 31 '12 at 10:26
  • Will try this, and report on the results. Thank you for your input so far. – NlightNFotis May 31 '12 at 10:31
  • What if it is really necessary to do it with an x and y? I am using a class that extends the JButton class, and I am using a Point variable to store its position on the JFrame. Shouldn't I use the the method I posted on the question? – NlightNFotis May 31 '12 at 10:35
  • But you could just use the JButton's `getLocation` and `setLocation` methods... Anyway, that wouldn't stop you holding onto the reference to the object. – Tharwen May 31 '12 at 10:41
  • Your getLocation method just showed me how terribly bugged my code is right now. Even though I am initializing the JButtons in for loops and pass the x and y loop counter each time as the Point position of the Button, when I set it to print the brick's position, all of them report it's 0.0. Seems like I have a hella lot of work to do. – NlightNFotis May 31 '12 at 10:52
  • Hmmm... could you edit your post to contain a full, compilable example to show how you're doing these things? – Tharwen May 31 '12 at 12:50
  • Ah! I thought you were talking about pixel positions! Clearly you weren't. Sorry... (But you could still keep a list of the buttons you put into the layout) – Tharwen May 31 '12 at 15:15

2 Answers2

2

This example compares two approaches:

  1. Maintain a List<JButton> and calculate the index algebraically.

  2. Extend JButton and allow each button to maintain its own grid location.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

GridLayout start at 0,0 and JButton at (4,4 -position in GridLayout) meaning the 32th (4*7+4 not 3*7+4) JButton in the JFrame. Sorry if I misunderstood the questions :(

You can try:

   public static JButton getButtonByXAndY(int x, int y) {
        return (JButton) f.getContentPane().getComponent(y * 7 + x);
    }
Yonutt
  • 111
  • 2
  • How did you come up with the 32th? I hope you failed there, because in any other case, I have failed, miserably. – NlightNFotis May 31 '12 at 13:35
  • Does your frame like this ? b0,0 b0,1 .... b0,6 /n b1,0 ....b1,6 /n .... b6,0 ...b6,6 and then b4,4 meaning the 32th – Yonutt May 31 '12 at 14:29