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?

2
  • 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 8, 2013 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 8, 2013 at 18:52

3 Answers 3

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.
1
  • 1
    @Bamapag maybe another and quite direct way for this job could be to use putClientProperty, you can to multiple this methods, with any additional details, then not reason to get this coordinates on the fly,
    – mKorbel
    Mar 8, 2013 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

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();
            }
        });
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.