Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23736

Re: need help on this.

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: need help on this.
Date 2013-04-30 17:02 +0200
Organization A noiseless patient Spider
Message-ID <klom6v$q67$1@dont-email.me> (permalink)
References <1ea6b46f-380f-4839-b4dc-74d72a10b5e6@googlegroups.com>

Show all headers | View raw


On 30/04/2013 15:18, wee allegedly 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..
> 	}
> }
> 

As Jeff hinted, you can simply iterate the array to find at which index
the object returned by #getSource() resides. Using a more sophisticated
data structure, like a List, would even be better.

However, altogether this is a clumsy way to go about this, and IMHO
there would be preferable alternatives. If you want to associate
arbitrary data with each JTextField instance, you could for instance
store them beforehand in a map with the JTextField as the key. This
would have the drawback of potentially "leaking" component references
out of the UI hierarchy, so an even better alternative would be to use
the JComponent's "client property" functionality, as exemplified in the
following with a property holding the field's index:

 public class ArrayUI extends JFrame {
    private static final INDEX_PROPERTY = "#index property#";
    private static final int NUM_ITEMS = 20;

    private final JPanel pane = new JPanel();

    public ArrayUI() {
       super("title");
       FlowLayout fl = new FlowLayout();
       setLayout(fl);
       Handler handle = new Handler();

       for (int i = 0; i < NUM_ITEMS; i++) {
           JTextField jtf = new JTextField("Text here " + i, 10);
           jtf.addMouseListener(handle);
           jtf.putClientProperty( INDEX_PROPERTY, Integer.valueOf(i) );
           pane.add(jtf);
       }

       add(pane);
       pack();
    }
 	
    private static class Handler extends MouseAdapter {
        public void mouseClicked(MouseEvent e){
            if( e.getSource() instanceof JComponent ){
                Integer index = (Integer) ((JComponent)
e.getSource()).getClientProperty( INDEX_PROPERTY );
            }
        }
    }
 }

I've fixed a couple of issues with your code /passim/:
 - do not expose instance fields, especially if they're not final; write
accessors (getters) if external classes need to access them, but
seriously consider the necessity of any such access.
 - make internal classes static unless there's a compelling reason not to.

HTH,
-- 
DF.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

need help on this. wee <rbulseco@gmail.com> - 2013-04-30 06:18 -0700
  Re: need help on this. Jeff Higgins <jeff@invalid.invalid> - 2013-04-30 09:50 -0400
    Re: need help on this. Jeff Higgins <jeff@invalid.invalid> - 2013-04-30 09:58 -0400
      Re: need help on this. Jeff Higgins <jeff@invalid.invalid> - 2013-04-30 10:09 -0400
    Re: need help on this. Jeff Higgins <jeff@invalid.invalid> - 2013-04-30 10:01 -0400
      Re: need help on this. Jeff Higgins <jeff@invalid.invalid> - 2013-04-30 10:39 -0400
  Re: need help on this. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-04-30 10:48 -0400
  Re: need help on this. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-04-30 17:02 +0200
  Re: need help on this. wee <rbulseco@gmail.com> - 2013-04-30 17:41 -0700
    Re: need help on this. wee <rbulseco@gmail.com> - 2013-05-06 21:29 -0700
      Re: need help on this. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-07 08:14 -0400
        Re: need help on this. wee <rbulseco@gmail.com> - 2013-05-10 03:52 -0700

csiph-web