8 Replies - 4563 Views - Last Post: 27 April 2011 - 05:30 PM Rate Topic: -----

#1 mittu.thefire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-December 10

getting cell location of a component from GridLayout..

Posted 26 April 2011 - 08:38 AM

i inserted buttons a gridLayout..
if one of the buttons is clicked i want to get the location of the cell from gridlayout and have to process it..

For Example:

A gridLayout of 4 rows 4 columns is created.
button at 2,2 is clicked then i have to get those 2,2..

thanks in advance...
Is This A Good Question/Topic? 0
  • +

Replies To: getting cell location of a component from GridLayout..

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 08:40 AM

You could store each JComponent in a JComponent[][]. Then iterate through your array until you find the JComponent that == the JComponent that was clicked. There is no GridLayout method to do this, though.

You could also use a JTable to store your JComponents, which has more functionality to deal with this.
Was This Post Helpful? 1
  • +
  • -

#3 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 09:08 AM

View Postmacosxnerd101, on 26 April 2011 - 08:40 AM, said:

You could store each JComponent in a JComponent[][]. Then iterate through your array until you find the JComponent that == the JComponent that was clicked. There is no GridLayout method to do this, though.

You could also use a JTable to store your JComponents, which has more functionality to deal with this.


You can have a common actionPerformed() and you can use the function getSource() of the ActionEvent and compare with each element and have the indices.

This post has been edited by sakshamkum: 26 April 2011 - 09:08 AM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 09:10 AM

That's only if the JTable is storing JComponents that can fire ActionEvents. Better to check the source of the MouseEvent instead.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 12:13 PM

For each button (having added an ActionListener to it) you can see its position in the grid with

    public void actionPerformed(java.awt.event.ActionEvent e) {
        JButton b = (JButton)e.getSource();
	Rectangle r = b.getBounds();
	Point p = b.getLocation();
	int row = p.y / r.height;
	int col = p.x / r.width;
	System.out.printf("At cell %d,%d\n", row, col);
    }



Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 08:53 PM

There are hundreds of way to do that


   ...
   JButton a = new MyButton("Button1", 0, 0);
   JButton b = new MyButton("Button2", 0, 1);
   ...


class MyButton extends JButton implements ActionListener {

    int x, y;

    MyButton(String text, int x, int y) {
      super(text);
      this.x = x;
      this.y = y;
      addActionListener(this);
    }

    public void ActionPerform(ActionEvent e) {
      // button at x,y has been clicked
    }
}


Was This Post Helpful? 2
  • +
  • -

#7 mittu.thefire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-December 10

Re: getting cell location of a component from GridLayout..

Posted 26 April 2011 - 10:48 PM

thanks for replys...

If buttons are getting change their locations in the gridLayout..
then what to do..
is it possible if I choose GridbagLayout..
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: getting cell location of a component from GridLayout..

Posted 27 April 2011 - 02:12 AM

Quote

If buttons are getting change their locations in the gridLayout..


Why would they? If they did, the code i posted should still work
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: getting cell location of a component from GridLayout..

Posted 27 April 2011 - 05:30 PM

View Postmittu.thefire, on 27 April 2011 - 12:48 AM, said:

thanks for replys...

If buttons are getting change their locations in the gridLayout..
then what to do..
is it possible if I choose GridbagLayout..

You can't in a GridLayout
I would avoid GridbagLayout
You can swap 2 buttons characteristics thow

JButton temp = (JButton) button1.clone();

button1.setText(button2.getText());
button1.setBackground(button2.getBackground());
... other characteristics

button2.setText(temp.getText());
button2.setBackground(temp.getBackground());
... other characteristics


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1