Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: need help on this. Date: Tue, 30 Apr 2013 10:48:41 -0400 Organization: A noiseless patient Spider Lines: 63 Message-ID: References: <1ea6b46f-380f-4839-b4dc-74d72a10b5e6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 30 Apr 2013 14:45:27 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="0d73d8cc209bff1c6395088b400d0605"; logging-data="22073"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180/gcI1rNbJ/78nRgbQ+Bj" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: <1ea6b46f-380f-4839-b4dc-74d72a10b5e6@googlegroups.com> Cancel-Lock: sha1:JMbeAdrTday8uvH3MzBX9pLgqUo= Xref: csiph.com comp.lang.java.programmer:23735 On 4/30/2013 9:18 AM, wee wrote: > i have this code: > > public class ArrayUI extends JFrame { > public JPanel pane = new JPanel(); > public JTextField[] item = new JTextField[20]; > > public ArrayUI() { > super("title"); > FlowLayout fl = new FlowLayout(); > setLayout(fl); > Handler handle = new Handler(); > > for (int i = 0; i < item.length; i++) { > item[i] = new JTextField(("Text here " + i), 10); > item[i].addMouseListener(handle); > pane.add(item[i]); > } > add(pane); > pack(); > } > > private class Handler extends MouseAdapter { > public void mouseClicked(MouseEvent e){ > > } > // i want to get the index of the array (item[]) of the JTextField > // object that received the mouseClicked action. > // any idea how i can do that? > // using the getSource() method returns the object itself, > // not the index of the array. help please.. Get the source object, then walk through the array, index by index, until you find it. My question, though: Why do you want the array index? If the answer is "Because there are other arrays with associated information, and I need the index to access it," there may be better approaches. Here are a few: - You might store the extra information directly on the JTextField object, possibly with setName() -- or maybe with setAction(), if that's more appropriate. - If none of the JTextField's attributes seem a suitable home for what you want to store, write a WeeTextField class that extends JTextField and just carries the extra information around. Note that you needn't write much code; all the real work happens in the JTextField superclass, and you just deal with the "decorations." - Put the extra information in the Handler class, and use a separate Handler instance for each JTextField instead of making them all share the same instance. > } > } > -- Eric Sosman esosman@comcast-dot-net.invalid