Results 1 to 3 of 3
  1. #1
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default how to get the location of some button

    I have a 2-D button array (4*4), I random click one, how can I get the location for that button (such as row 3 column 2) ???

    thanks a lot

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComponentLocation implements ActionListener {
        JButton[][] buttons = new JButton[4][4];
    
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            Point gridLoc = getArrayLocation(button);
            System.out.println("gridLoc = [" + gridLoc.x +
                               ", " + gridLoc.y + "]");
        }
    
        private Point getArrayLocation(JButton target) {
            Point p = new Point(-1, -1);
            for(int j = 0; j < buttons.length; j++) {
                for(int k = 0; k < buttons[j].length; k++) {
                    if(buttons[j][k] == target) {
                        p.setLocation(j, k);
                        return p;
                    }
                }
            }
            return p;
        }
    
        private JPanel getContent() {
            JPanel panel  = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weighty = 1.0;
            gbc.weightx = 1.0;
            for(int j = 0; j < buttons.length; j++) {
                for(int k = 0; k < buttons[j].length; k++) {
                    int n = j*buttons[j].length + k + 1;
                    buttons[j][k] = new JButton("Button " + n);
                    buttons[j][k].addActionListener(this);
                    gbc.gridwidth = (k < buttons[j].length-1) ? 1 :
                                         GridBagConstraints.REMAINDER;
                    panel.add(buttons[j][k], gbc);
                }
            }
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new ComponentLocation().getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 11:07 AM
  2. JFileChooser remember the location
    By Mr tuition in forum AWT / Swing
    Replies: 3
    Last Post: 12-08-2007, 06:17 PM
  3. (Location based Service) application for Pocket PC
    By talk_to_vivekmishra in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-14-2007, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •