need help on this.

Discussion in 'Java' started by wee, Apr 30, 2013.

  1. wee

    wee Guest

    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 = new JTextField(("Text here " + i), 10);
    item.addMouseListener(handle);
    pane.add(item);
    }
    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..
    }
    }
     
    wee, Apr 30, 2013
    #1
    1. Advertising

  2. wee

    Jeff Higgins Guest

    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 = new JTextField(("Text here " + i), 10);
    > item.addMouseListener(handle);
    > pane.add(item);
    > }
    > 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()?
    > }
    > }
    >
     
    Jeff Higgins, Apr 30, 2013
    #2
    1. Advertising

  3. wee

    Jeff Higgins Guest

    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 = new JTextField(("Text here " + i), 10);
    >> item.addMouseListener(handle);
    >> pane.add(item);
    >> }
    >> 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()
    >> }
    >> }
    >>

    >
     
    Jeff Higgins, Apr 30, 2013
    #3
  4. wee

    Jeff Higgins Guest

    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 = new JTextField(("Text here " + i), 10);
    >> item.addMouseListener(handle);
    >> pane.add(item);
    >> }
    >> 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 )
    >> }
    >> }
    >>

    >
     
    Jeff Higgins, Apr 30, 2013
    #4
  5. wee

    Jeff Higgins Guest

    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 = new JTextField(("Text here " + i), 10);
    >>> item.addMouseListener(handle);
    >>> pane.add(item);
    >>> }
    >>> 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()
    >>> }
    >>> }
    >>>

    >>

    >
     
    Jeff Higgins, Apr 30, 2013
    #5
  6. wee

    Jeff Higgins Guest

    On 04/30/2013 10:01 AM, Jeff Higgins wrote:
    > for (int i = 0; i< item.length; i++)
     
    Jeff Higgins, Apr 30, 2013
    #6
  7. wee

    Eric Sosman Guest

    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 = new JTextField(("Text here " + i), 10);
    > item.addMouseListener(handle);
    > pane.add(item);
    > }
    > 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
    d
     
    Eric Sosman, Apr 30, 2013
    #7
  8. 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 = new JTextField(("Text here " + i), 10);
    > item.addMouseListener(handle);
    > pane.add(item);
    > }
    > 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.
     
    Daniele Futtorovic, Apr 30, 2013
    #8
  9. wee

    wee Guest

    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!
     
    wee, May 1, 2013
    #9
  10. wee

    wee Guest

    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..
     
    wee, May 7, 2013
    #10
  11. wee

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, May 7, 2013
    #11
  12. wee

    wee Guest

    <<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.
     
    wee, May 10, 2013
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,506
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    787
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    431
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    599
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    534
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page