need help on this.

W

wee

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..
}
}
 
J

Jeff Higgins

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()?
}
}
 
J

Jeff Higgins

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 said:
 
J

Jeff Higgins

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 )
 
J

Jeff Higgins

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()
 
E

Eric Sosman

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.
 
D

Daniele Futtorovic

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,
 
W

wee

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!
 
W

wee

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..
 
E

Eric Sosman

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.)
 
W

wee

<<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top