1

I'm using a JFrame with a GridBagLayout and I want to be able to get the component(in my case a JButton) from specific x,y coordinates. Shouldn't this be possible because I gave it x,y coordinates with a GridBagConstraints to begin with?

Is there an easy way to do this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bamapag
  • 13
  • 1
  • 3
  • You should register `mouseClicked` event to each component instead.. this will help you to retrieve any of the components clicked on that JFrame.. – Vishal K Mar 08 '13 at 18:44
  • What I'm trying to do is take a certain JButton and perform a set of operations on JButtons in a square pattern around it. i.e if My button is at 1,1 I want to be able to get to buttons 0,0 , 1,0 , 2,0 , 0,1 , 2,1 and so on. – Bamapag Mar 08 '13 at 18:52

3 Answers3

3

I want to be able to get the component(in my case a JButton) from specific x,y coordinates.

  1. Add an ActionListener to every button.
  2. In the ActionListener code you can get the button you clicked on by using the event.getSource() method of the ActionEvent.
  3. Once you know the source you can get the parent Container using the getParent() method of the Component.
  4. Then you can get the layout manager using the getLayout() method of the Container.
  5. Once you have the GridBagLayout you can bet the constraints for the Component using the getContraints() method.
  6. Then you can get the x/y coordinates.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    @Bamapag maybe another and quite direct way for this job could be to use [putClientProperty](http://stackoverflow.com/a/13531549/714968), you can to multiple this methods, with any additional details, then not reason to get this coordinates on the fly, – mKorbel Mar 08 '13 at 19:21
1

If you know x,y coordinates on JFrame you want, you can find what is the cell (x,y in GridBagConstraints) with location() method of GridBagLayout. Knowing the x,y on GridBagLayout, you want with iterate through JFrame's components and find which has the same x,y in GridBagConstraints comparing it to Constraints given on method getConstraints(Component comp).

Take a look at GridBagLayout API

Jean Waghetti
  • 4,711
  • 1
  • 18
  • 28
0

enter image description here
Here is little ridiculous example of achieving what you looking for . I hope it would be of your help...

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Point;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.Set;
import java.util.LinkedHashMap;
class GridBagFrame extends JFrame implements ActionListener
{
    GridBagLayout gb ;
    GridBagConstraints gc;
    Map <Point,JButton> map ;
    final int SIZE = 20;
    public void createAndShowGUI()
    {
        gb = new GridBagLayout();
        gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.BOTH;
        map = new LinkedHashMap<Point,JButton>();
        Container container = getContentPane();
        container.setLayout(gb);
        int x =0 , y = -1 ;
        JButton[] button = new JButton[SIZE];
        for (int i = 0 ; i < SIZE ; i++ )
        {
            button[i] = new JButton(String.valueOf(i + 1));
            if (i % 4 == 0)
            {
                x = 0 ;
                y = y +1;
            }
            gc.gridx = x++;
            gc.gridy = y;
            gb.setConstraints(button[i],gc);
            container.add(button[i]);
            map.put(new Point(x,y),button[i]);
            button[i].setActionCommand(x+","+y);
            button[i].addActionListener(this);
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setResizable(false);
        setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent evt)
    {
        resetAll();
        JButton source = (JButton)evt.getSource();
        String command = source.getActionCommand();
        String[] arr = command.split(",");
        int x = Integer.parseInt(arr[0]);
        int y = Integer.parseInt(arr[1]);
        for ( int iy = y - 1; iy <= y + 1; iy++)
        {
            for (int ix = x -1 ; ix <= x + 1 ; ix++)
            {
                JButton button = map.get(new Point(ix,iy));
                if (button != null)
                {
                    button.setForeground(Color.red);
                }
            }
        }
        source.setForeground(Color.blue);
    }
    private void resetAll()
    {
        Set<Point> pointSet = map.keySet();
        for (Point point : pointSet )
        {
            map.get(point).setForeground(Color.black);
        }
    }
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                GridBagFrame frame = new GridBagFrame();
                frame.createAndShowGUI();
            }
        });
    }
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38