Results 1 to 3 of 3
- 08-02-2007, 01:39 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 51
- Rep Power
- 0
- 08-02-2007, 10:26 PM #2
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); } }
- 08-05-2007, 04:02 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
JFileChooser remember the location
By Mr tuition in forum AWT / SwingReplies: 3Last Post: 12-08-2007, 05:17 PM -
(Location based Service) application for Pocket PC
By talk_to_vivekmishra in forum CLDC and MIDPReplies: 0Last Post: 08-14-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks