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


Groups > comp.lang.java.programmer > #23727 > unrolled thread

need help on this.

Started bywee <rbulseco@gmail.com>
First post2013-04-30 06:18 -0700
Last post2013-05-10 03:52 -0700
Articles 12 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#23727 — need help on this.

Fromwee <rbulseco@gmail.com>
Date2013-04-30 06:18 -0700
Subjectneed help on this.
Message-ID<1ea6b46f-380f-4839-b4dc-74d72a10b5e6@googlegroups.com>
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..
	}
}

[toc] | [next] | [standalone]


#23729

FromJeff Higgins <jeff@invalid.invalid>
Date2013-04-30 09:50 -0400
Message-ID<klohvp$18q$1@dont-email.me>
In reply to#23727
On 04/30/2013 09: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..
           // Arrays.binarySearch()?
> 	}
> }
>

[toc] | [prev] | [next] | [standalone]


#23730

FromJeff Higgins <jeff@invalid.invalid>
Date2013-04-30 09:58 -0400
Message-ID<kloidr$3lf$1@dont-email.me>
In reply to#23729
On 04/30/2013 09:50 AM, Jeff Higgins wrote:
> On 04/30/2013 09: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..
> // Arrays.binarySearch()?
ArrayList<JtextField> items = new ArrayList<JTextField>()
items.contains()
>> }
>> }
>>
>

[toc] | [prev] | [next] | [standalone]


#23733

FromJeff Higgins <jeff@invalid.invalid>
Date2013-04-30 10:09 -0400
Message-ID<kloj2p$7pq$1@dont-email.me>
In reply to#23730
On 04/30/2013 09:58 AM, Jeff Higgins wrote:
> On 04/30/2013 09:50 AM, Jeff Higgins wrote:
>> On 04/30/2013 09: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..
>> // Arrays.binarySearch()?
> ArrayList<JtextField> items = new ArrayList<JTextField>()
> items.contains()
Oops, indexOf()
>>> }
>>> }
>>>
>>
>

[toc] | [prev] | [next] | [standalone]


#23731

FromJeff Higgins <jeff@invalid.invalid>
Date2013-04-30 10:01 -0400
Message-ID<kloik5$4uv$1@dont-email.me>
In reply to#23729
On 04/30/2013 09:50 AM, Jeff Higgins wrote:
> On 04/30/2013 09: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..
> // Arrays.binarySearch()?
for ( JTextField f : item )
>> }
>> }
>>
>

[toc] | [prev] | [next] | [standalone]


#23734

FromJeff Higgins <jeff@invalid.invalid>
Date2013-04-30 10:39 -0400
Message-ID<klokrg$i8v$1@dont-email.me>
In reply to#23731
On 04/30/2013 10:01 AM, Jeff Higgins wrote:
> for (int i = 0; i< item.length; i++)

[toc] | [prev] | [next] | [standalone]


#23735

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-04-30 10:48 -0400
Message-ID<klole7$lhp$1@dont-email.me>
In reply to#23727
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

[toc] | [prev] | [next] | [standalone]


#23736

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2013-04-30 17:02 +0200
Message-ID<klom6v$q67$1@dont-email.me>
In reply to#23727
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.

[toc] | [prev] | [next] | [standalone]


#23753

Fromwee <rbulseco@gmail.com>
Date2013-04-30 17:41 -0700
Message-ID<f9967431-22f3-4519-9dbb-a6eae8b4c368@googlegroups.com>
In reply to#23727
thank you very much for the replies. i'm going to try a few of them, although i do need to figure out some of the suggested solutions.

@jeff higgins - thank you for your suggestions.
@eric sosman - my intentions actually was to store contents of a ResultSet into the arrays and then display them. and then, with the mouseClicked, i would know which of the displayed result a user picks so that i can go and process that choice (or at least that's the program logic that i intended).
@daniele futtorivic - pardon my programming skills, i never really learned java when i started self-studying it a few years back, so i'm practically still a newbie until now (lol). i'm writing your comments down as a reminder. hopefully i'd minimize, if not eliminate, such bad programming style.

thanks again for the suggestions guys.. Cheers!

[toc] | [prev] | [next] | [standalone]


#23880

Fromwee <rbulseco@gmail.com>
Date2013-05-06 21:29 -0700
Message-ID<1cbfe10e-bb38-4477-a349-6136495a427f@googlegroups.com>
In reply to#23753
just an update, used the suggestion of eric sosman in using setName(), and it worked for me. funny that i never found that setName() method in the JTextField documentation..

thanks again for the help..

[toc] | [prev] | [next] | [standalone]


#23886

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-05-07 08:14 -0400
Message-ID<kmar1a$16h$1@dont-email.me>
In reply to#23880
On 5/7/2013 12:29 AM, wee wrote:
> just an update, used the suggestion of eric sosman in using setName(), and it worked for me. funny that i never found that setName() method in the JTextField documentation..

     Glad it was helpful.  Daniele Futtorovic's suggestion of
using putClientProperty() looks better, though -- certainly
more general, and more powerful.

     (You probably didn't find setName() in JTextField's Javadoc for
the simple reason that it isn't there, or "almost isn't there."  If
you look closely, you'll find it mentioned in one of the lists of
inherited methods: setName() is a method of java.awt.Component, and
therefore of all Component's descendants.  The putClientProperty()
method is also inherited rather than JTextField-specific, this time
from javax.swing.JComponent.  Moral: When you're looking for a method
in some class or other, don't forget to look at the ancestry.)

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#23981

Fromwee <rbulseco@gmail.com>
Date2013-05-10 03:52 -0700
Message-ID<60db931f-ed0f-4abc-974d-d253aef01517@googlegroups.com>
In reply to#23886
<<Moral: When you're looking for a method
in some class or other, don't forget to look at the ancestry.>>

lol.. you're absolutely right, though i would certainly miss that thought, considering that me still newbie on this. (and for the life of me, i still haven't gotten the hang of fully comprehending the javadocs.)

i certainly did consider Daniele Futtorovic's suggestion, but i just haven't reached that level of java 'knowledge', yet. not just yet. i do consider his suggestion to be advanced for my newbie brains to comprehend (lol). when i'm finished doing this little (and simple) desktop application that i'm currently (and slowly) working on, i'll improve on the methods that i have used and i'll certainly include and experiment on mr. Futtorivic's putClientProperty() method.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web