Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

TextField

Thread view: 
man4*.* - 03 Nov 2006 18:08 GMT
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?
Michael Rauscher - 03 Nov 2006 18:23 GMT
man4*.* schrieb:
> if I choose from the JList one item I would like to get it written in
> JTextField
[quoted text clipped - 3 lines]
> 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 );
        }
    }
});
Michael Rauscher - 03 Nov 2006 18:24 GMT
I forgot:

Bye
Michael
:)
man4*.* - 03 Nov 2006 18:52 GMT
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.. ;-)
mfreak - 03 Nov 2006 19:08 GMT
> right now, I'm creating classes for each component like:
>
[quoted text clipped - 7 lines]
>
> 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..
 }
}
Fred Kleinschmidt - 03 Nov 2006 22:00 GMT
> THX, I've modified you're code a litle bit and found what I was looking
> for..
[quoted text clipped - 16 lines]
> 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.
   }
}
Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Michael Rauscher - 04 Nov 2006 01:47 GMT
man4*.* schrieb:
> THX, I've modified you're code a litle bit and found what I was looking
> for..
[quoted text clipped - 13 lines]
>
> 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
Daniel Pitts - 04 Nov 2006 02:38 GMT
> THX, I've modified you're code a litle bit and found what I was looking
> for..
[quoted text clipped - 16 lines]
> 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.
man4*.* - 04 Nov 2006 18:48 GMT
> 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.
[quoted text clipped - 3 lines]
> 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...
Daniel Pitts - 05 Nov 2006 00:01 GMT
> > tt2.addActionListener(new ActionListener() {
> >    public void actionPerformed(ActionEvent e) {
[quoted text clipped - 17 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.