TextField

Discussion in 'Java' started by man4*.*, Nov 3, 2006.

  1. man4*.*

    man4*.* Guest

    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?
    man4*.*, Nov 3, 2006
    #1
    1. Advertising

  2. man4*.* schrieb:
    > 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 );
    }
    }
    });
    Michael Rauscher, Nov 3, 2006
    #2
    1. Advertising

  3. I forgot:

    Bye
    Michael
    :)
    Michael Rauscher, Nov 3, 2006
    #3
  4. man4*.*

    man4*.* Guest

    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.. ;-)
    man4*.*, Nov 3, 2006
    #4
  5. man4*.*

    mfreak Guest

    > 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..
    }
    }
    mfreak, Nov 3, 2006
    #5
  6. "man4*.*" <> wrote in message news:eifvkb$j3s$...
    > 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.
    }
    }
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, Nov 3, 2006
    #6
  7. man4*.* schrieb:
    > 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
    Michael Rauscher, Nov 4, 2006
    #7
  8. man4*.*

    Daniel Pitts Guest

    man4*.* wrote:
    > 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.
    Daniel Pitts, Nov 4, 2006
    #8
  9. man4*.*

    man4*.* Guest

    > 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...
    man4*.*, Nov 4, 2006
    #9
  10. man4*.*

    Daniel Pitts Guest

    man4*.* wrote:
    > > 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...


    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.
    Daniel Pitts, Nov 4, 2006
    #10
    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. Remco Groot Beumer

    How to read input from a textfield in a datagrid !?!

    Remco Groot Beumer, Nov 6, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    300
    Remco Groot Beumer
    Nov 6, 2003
  2. Soul
    Replies:
    0
    Views:
    379
  3. Jerome
    Replies:
    2
    Views:
    402
    Trevor Benedict R
    Mar 14, 2005
  4. =?Utf-8?B?TUw=?=

    Can HMTLInputFile textfield be readonly?

    =?Utf-8?B?TUw=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    322
  5. prefersgolfing

    remove "_" from TextField="#InnerText"

    prefersgolfing, Dec 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    prefersgolfing
    Dec 20, 2005
Loading...

Share This Page