TextField

M

man4*.*

if I choose from the JList one item I would like to get it written in
JTextField
I saw an example where it is written in JTextArea, but I would like to get
it in Field
is it possible?
and can I use ActionListener for such things or i should use
ListSelectionListener?
 
M

Michael Rauscher

man4*.* said:
if I choose from the JList one item I would like to get it written in
JTextField
I saw an example where it is written in JTextArea, but I would like to get
it in Field
is it possible?
and can I use ActionListener for such things or i should use
ListSelectionListener?

Assuming a JTextField textField:

list.addListSelectionListener( new ListSelectionListener() {
public void valueChanged( ListSelectionEvent e ) {
if ( !e.getValueIsAdjusting() ) {
Object item = ((List)e.getSource()).getSelectedItem();
textField.setText(
(item != null) ? item.toString() : null );
}
}
});
 
M

man4*.*

THX, I've modified you're code a litle bit and found what I was looking
for..

So, I'm new in swing (new also in Java :) ) and I was wondering....
I'm creating one let's call it aplication, with few buttons, TexFields,
RadioButt., comboBox...
right now, I'm creating classes for each component like:

class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
}

and calling it with tt2.addActionListener(new T1A());

and now I have to code at least 10 new classes, is there any other way?
at the moment I'm learning, and I'm coding each line, not using any GUI
builder...and that's a
lot of code to write.. ;-)
 
M

mfreak

right now, I'm creating classes for each component like:
class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
}

and calling it with tt2.addActionListener(new T1A());

and now I have to code at least 10 new classes, is there any other way?

I'm not sure I understand what you're after, maybe something like this:

T1A t=new T1A();
tf1.addActionListener(t);
tf2.addActionListener(t);
jButton1.addActionListener(t);
jComboBox1.addActionListener(t);
//etc.......

class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object o=e.getSource();
if ( o==tf1 ){
//do something....
}
else if ( o==tf2 ){
//do something else....
}
else if ( o==jButton1){
//do something else....
}
//etc..
}
}
 
F

Fred Kleinschmidt

man4*.* said:
THX, I've modified you're code a litle bit and found what I was looking
for..

So, I'm new in swing (new also in Java :) ) and I was wondering....
I'm creating one let's call it aplication, with few buttons, TexFields,
RadioButt., comboBox...
right now, I'm creating classes for each component like:

class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
}

and calling it with tt2.addActionListener(new T1A());

and now I have to code at least 10 new classes, is there any other way?
at the moment I'm learning, and I'm coding each line, not using any GUI
builder...and that's a
lot of code to write.. ;-)
You don't even need any new classes.
Just have the class that you are writing (the one that contains
all the buttons, texts, etc.) implement ActionListener.
Then for each button:
button.addActionListener(this);
and create an actionPerformed() method in your class
that checks the source:

public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if ( obj == button ) {
// do something
}
else if ( obj == button2 ) {
// etc.
}
}
 
M

Michael Rauscher

man4*.* said:
THX, I've modified you're code a litle bit and found what I was looking
for..

So, I'm new in swing (new also in Java :) ) and I was wondering....
I'm creating one let's call it aplication, with few buttons, TexFields,
RadioButt., comboBox...
right now, I'm creating classes for each component like:

class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
}

and calling it with tt2.addActionListener(new T1A());

and now I have to code at least 10 new classes, is there any other way?

Depends. If you've got 10 completely different tasks then yes (forget
about if-else/switch-constructs to create whole-world-listeners).

In many cases you can reuse classes:

class TextSetterAction implements ActionListener {
private JTextComponent tc1;
private JTextComponent tc2;

public TextSetterAction( JTextComponent tc1, JTextComponent tc2 ) {
this.tc1 = tc1;
this.tc2 = tc2;
}

public void actionPerformed( ActionEvent e ) {
tc1.setText( tc2.getText() );
}
}

TextSetterAction tf1tf2Setter = new TextSetterAction(tf1,tf2);
tt2.addActionListener( tf1tf2Setter );
tt3.addActionListener( new TextSetterAction(tf3, tf4) );
....

Of course, you can reuse objects, too:

tt4.addActionListener( tf1tf2Setter );
...

There are more ways to make life easier (e. g. JComponent's client
property) but it depends on the situation.

Bye
Michael
 
D

Daniel Pitts

man4*.* said:
THX, I've modified you're code a litle bit and found what I was looking
for..

So, I'm new in swing (new also in Java :) ) and I was wondering....
I'm creating one let's call it aplication, with few buttons, TexFields,
RadioButt., comboBox...
right now, I'm creating classes for each component like:

class T1A implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
}

and calling it with tt2.addActionListener(new T1A());

and now I have to code at least 10 new classes, is there any other way?
at the moment I'm learning, and I'm coding each line, not using any GUI
builder...and that's a
lot of code to write.. ;-)

In Java, there is something called an anonymous inner class.

tt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
});

Oh, and a note on style. It makes it a lot easier to
edit/debug/fix/maintain/describe your code if you use meaningful names.
tf1 is obscure, it should be named something like
"emailAddressTextField" where "emailAddress" is the "function" of the
text field.

names like tt2, tf1, tf2, all start to look the same after a while, and
if you're like me and continue coding after you start to get tired,
you'll start putting tt1 instead of tt2, etc.., and it can be difficult
to figure out where your mistake is.
 
M

man4*.*

tt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf2.getText() );
}
});

well, that's exactly how BE described in Thinking in Java book how to
perform with Ac.Lis.
but, I'm always looking for something new & (maybee) interesting stuff
"emailAddressTextField" where "emailAddress" is the "function" of the
text field.

names like tt2, tf1, tf2, all start to look the same after a while, and
if you're like me and continue coding after you start to get tired,
you'll start putting tt1 instead of tt2, etc.., and it can be difficult
to figure out where your mistake is.


absolutle right, but, right now I'm just practicing and making some real
primitive
aplications to figure out how whole stuff works...
 
D

Daniel Pitts

man4*.* said:
well, that's exactly how BE described in Thinking in Java book how to
perform with Ac.Lis.
but, I'm always looking for something new & (maybee) interesting stuff



absolutle right, but, right now I'm just practicing and making some real
primitive
aplications to figure out how whole stuff works...

Okay, so your practicing, thats great, but why not practice the better
way of doing things. Wouldn't you prefer a doctor who practiced with a
real scalpel, instead of a plastic knife? Same applies to programming,
you pick up bad habits easily. "Oh, this is just a small method that
will never need to be changed." and then all of a sudden it is the core
to a larger system, and no one can fix any problems with it because its
written badly. :)

Also, when you have to type full names, you learn how to type faster.
Well, in any case. Good luck.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top