Frustrated learning Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Frustrated learning Java

  1. #1
    Mr. T Guest

    Frustrated learning Java


    This program is supposed to move "a name"
    from a text field to a text area when the change button is clicked, it is
    also supposed to move "a name" back and forth on subsequent button clicks.
    I have gotten this far, but have been stuck now for a couple of days. this
    code will take "a name" from the text field and place it in the text area
    (But it does not remove "a name" from the text field) I am new to Java and
    what I thought would work (using if,else statments) did not work. Any Help
    would be appreciated.

    Thanks
    Code below
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;


    public class NameOnFrame extends JFrame
    {
    public NameOnFrame()
    {




    /* this creates a text field with my name in it
    @param in the Text Field with my name in it
    */
    final JTextField text = new JTextField("a name");
    text.setEditable(false);


    JButton changeButton = new JButton("Change");



    final JTextArea outputHere = new JTextArea("");
    outputHere.setEditable(false);

    //This is what happens when the button gets clicked
    class changeButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    //This gets the name from the JTextField
    String name = new String();
    name = text.getText();

    //This prints the name inside the text area
    outputHere.setText(name);



    }
    }

    ActionListener listener = new changeButtonListener();
    changeButton.addActionListener(listener);


    getContentPane().add(text, BorderLayout.WEST);
    getContentPane().add(changeButton, BorderLayout.EAST);
    getContentPane().add(outputHere, BorderLayout.SOUTH);
    }
    }

  2. #2
    Guest

    Re: Frustrated learning Java


    Replace what is in your actionPerformed method with this:

    outputHere.setText(text.getText());
    text.setText("");

    "Mr. T" <java.@127.0.0.1> wrote:
    >
    >This program is supposed to move "a name"
    >from a text field to a text area when the change button is clicked, it is
    >also supposed to move "a name" back and forth on subsequent button clicks.
    >I have gotten this far, but have been stuck now for a couple of days. this
    >code will take "a name" from the text field and place it in the text area
    >(But it does not remove "a name" from the text field) I am new to Java and
    >what I thought would work (using if,else statments) did not work. Any Help
    >would be appreciated.
    >
    >Thanks
    >Code below
    >import java.awt.BorderLayout;
    >import javax.swing.JButton;
    >import javax.swing.JFrame;
    >import java.awt.event.ActionEvent;
    >import java.awt.event.ActionListener;
    >import javax.swing.JTextField;
    >import javax.swing.JPanel;
    >import javax.swing.JTextArea;
    >
    >
    >public class NameOnFrame extends JFrame
    >{
    > public NameOnFrame()
    > {
    >
    >
    >
    >
    > /* this creates a text field with my name in it
    > @param in the Text Field with my name in it
    > */
    > final JTextField text = new JTextField("a name");
    > text.setEditable(false);
    >
    >
    > JButton changeButton = new JButton("Change");
    >
    >
    >
    > final JTextArea outputHere = new JTextArea("");
    > outputHere.setEditable(false);
    >
    > //This is what happens when the button gets clicked
    > class changeButtonListener implements ActionListener
    > {
    > public void actionPerformed(ActionEvent event)
    > {
    > //This gets the name from the JTextField
    > String name = new String();
    > name = text.getText();
    >
    > //This prints the name inside the text area
    > outputHere.setText(name);
    >
    >
    >
    > }
    > }
    >
    > ActionListener listener = new changeButtonListener();
    > changeButton.addActionListener(listener);
    >
    >
    > getContentPane().add(text, BorderLayout.WEST);
    > getContentPane().add(changeButton, BorderLayout.EAST);
    > getContentPane().add(outputHere, BorderLayout.SOUTH);
    > }
    >}



  3. #3
    Guest

    Re: Frustrated learning Java


    Thank you very much for your help. I tried that and it fixes 1 of the problems
    I was having. The only other problem was that it still doesn't bring "a name"
    back to the text field when the button is clicked for a 2nd time.
    Thanks again


    <java.@127.0.0.1> wrote:
    >
    >Replace what is in your actionPerformed method with this:
    >
    >outputHere.setText(text.getText());
    >text.setText("");
    >
    >"Mr. T" <java.@127.0.0.1> wrote:
    >>
    >>This program is supposed to move "a name"
    >>from a text field to a text area when the change button is clicked, it

    is
    >>also supposed to move "a name" back and forth on subsequent button clicks.
    >>I have gotten this far, but have been stuck now for a couple of days. this
    >>code will take "a name" from the text field and place it in the text area
    >>(But it does not remove "a name" from the text field) I am new to Java

    and
    >>what I thought would work (using if,else statments) did not work. Any

    Help
    >>would be appreciated.
    >>
    >>Thanks
    >>Code below
    >>import java.awt.BorderLayout;
    >>import javax.swing.JButton;
    >>import javax.swing.JFrame;
    >>import java.awt.event.ActionEvent;
    >>import java.awt.event.ActionListener;
    >>import javax.swing.JTextField;
    >>import javax.swing.JPanel;
    >>import javax.swing.JTextArea;
    >>
    >>
    >>public class NameOnFrame extends JFrame
    >>{
    >> public NameOnFrame()
    >> {
    >>
    >>
    >>
    >>
    >> /* this creates a text field with my name in it
    >> @param in the Text Field with my name in it
    >> */
    >> final JTextField text = new JTextField("a name");
    >> text.setEditable(false);
    >>
    >>
    >> JButton changeButton = new JButton("Change");
    >>
    >>
    >>
    >> final JTextArea outputHere = new JTextArea("");
    >> outputHere.setEditable(false);
    >>
    >> //This is what happens when the button gets clicked
    >> class changeButtonListener implements ActionListener
    >> {
    >> public void actionPerformed(ActionEvent event)
    >> {
    >> //This gets the name from the JTextField
    >> String name = new String();
    >> name = text.getText();
    >>
    >> //This prints the name inside the text area
    >> outputHere.setText(name);
    >>
    >>
    >>
    >> }
    >> }
    >>
    >> ActionListener listener = new changeButtonListener();
    >> changeButton.addActionListener(listener);
    >>
    >>
    >> getContentPane().add(text, BorderLayout.WEST);
    >> getContentPane().add(changeButton, BorderLayout.EAST);
    >> getContentPane().add(outputHere, BorderLayout.SOUTH);
    >> }
    >>}

    >



  4. #4
    MarkN Guest

    Re: Frustrated learning Java


    Add a boolean
    at the top of your class:

    public class NameOnFrame extends JFrame
    {
    Private boolean whereTo = true;



    In your code, check the flag and set the fields based on it. Typical non-Java
    stuff.

    if(whereTo)
    {
    outputHere.setText(text.getText());
    text.setText("");
    whereto = false;
    }
    else
    {
    text.setText(outputHere.getText());
    outputHere.setText("");
    whereto = true;
    }




    <java.@127.0.0.1> wrote:
    >
    >Thank you very much for your help. I tried that and it fixes 1 of the problems
    >I was having. The only other problem was that it still doesn't bring "a

    name"
    >back to the text field when the button is clicked for a 2nd time.
    >Thanks again
    >
    >
    ><java.@127.0.0.1> wrote:
    >>
    >>Replace what is in your actionPerformed method with this:
    >>
    >>outputHere.setText(text.getText());
    >>text.setText("");
    >>
    >>"Mr. T" <java.@127.0.0.1> wrote:
    >>>
    >>>This program is supposed to move "a name"
    >>>from a text field to a text area when the change button is clicked, it

    >is
    >>>also supposed to move "a name" back and forth on subsequent button clicks.
    >>>I have gotten this far, but have been stuck now for a couple of days.

    this
    >>>code will take "a name" from the text field and place it in the text area
    >>>(But it does not remove "a name" from the text field) I am new to Java

    >and
    >>>what I thought would work (using if,else statments) did not work. Any

    >Help
    >>>would be appreciated.
    >>>
    >>>Thanks
    >>>Code below
    >>>import java.awt.BorderLayout;
    >>>import javax.swing.JButton;
    >>>import javax.swing.JFrame;
    >>>import java.awt.event.ActionEvent;
    >>>import java.awt.event.ActionListener;
    >>>import javax.swing.JTextField;
    >>>import javax.swing.JPanel;
    >>>import javax.swing.JTextArea;
    >>>
    >>>
    >>>public class NameOnFrame extends JFrame
    >>>{
    >>> public NameOnFrame()
    >>> {
    >>>
    >>>
    >>>
    >>>
    >>> /* this creates a text field with my name in it
    >>> @param in the Text Field with my name in it
    >>> */
    >>> final JTextField text = new JTextField("a name");
    >>> text.setEditable(false);
    >>>
    >>>
    >>> JButton changeButton = new JButton("Change");
    >>>
    >>>
    >>>
    >>> final JTextArea outputHere = new JTextArea("");
    >>> outputHere.setEditable(false);
    >>>
    >>> //This is what happens when the button gets clicked
    >>> class changeButtonListener implements ActionListener
    >>> {
    >>> public void actionPerformed(ActionEvent event)
    >>> {
    >>> //This gets the name from the JTextField
    >>> String name = new String();
    >>> name = text.getText();
    >>>
    >>> //This prints the name inside the text area
    >>> outputHere.setText(name);
    >>>
    >>>
    >>>
    >>> }
    >>> }
    >>>
    >>> ActionListener listener = new changeButtonListener();
    >>> changeButton.addActionListener(listener);
    >>>
    >>>
    >>> getContentPane().add(text, BorderLayout.WEST);
    >>> getContentPane().add(changeButton, BorderLayout.EAST);
    >>> getContentPane().add(outputHere, BorderLayout.SOUTH);
    >>> }
    >>>}

    >>

    >



  5. #5
    Qbert Guest

    Re: Frustrated learning Java



    Where is your main method?


    "Mr. T" <java.@127.0.0.1> wrote:
    >
    >This program is supposed to move "a name"
    >from a text field to a text area when the change button is clicked, it is
    >also supposed to move "a name" back and forth on subsequent button clicks.
    >I have gotten this far, but have been stuck now for a couple of days. this
    >code will take "a name" from the text field and place it in the text area
    >(But it does not remove "a name" from the text field) I am new to Java and
    >what I thought would work (using if,else statments) did not work. Any Help
    >would be appreciated.
    >
    >Thanks
    >Code below
    >import java.awt.BorderLayout;
    >import javax.swing.JButton;
    >import javax.swing.JFrame;
    >import java.awt.event.ActionEvent;
    >import java.awt.event.ActionListener;
    >import javax.swing.JTextField;
    >import javax.swing.JPanel;
    >import javax.swing.JTextArea;
    >
    >
    >public class NameOnFrame extends JFrame
    >{
    > public NameOnFrame()
    > {
    >
    >
    >
    >
    > /* this creates a text field with my name in it
    > @param in the Text Field with my name in it
    > */
    > final JTextField text = new JTextField("a name");
    > text.setEditable(false);
    >
    >
    > JButton changeButton = new JButton("Change");
    >
    >
    >
    > final JTextArea outputHere = new JTextArea("");
    > outputHere.setEditable(false);
    >
    > //This is what happens when the button gets clicked
    > class changeButtonListener implements ActionListener
    > {
    > public void actionPerformed(ActionEvent event)
    > {
    > //This gets the name from the JTextField
    > String name = new String();
    > name = text.getText();
    >
    > //This prints the name inside the text area
    > outputHere.setText(name);
    >
    >
    >
    > }
    > }
    >
    > ActionListener listener = new changeButtonListener();
    > changeButton.addActionListener(listener);
    >
    >
    > getContentPane().add(text, BorderLayout.WEST);
    > getContentPane().add(changeButton, BorderLayout.EAST);
    > getContentPane().add(outputHere, BorderLayout.SOUTH);
    > }
    >}



  6. #6
    Mark Guest

    Re: Frustrated learning Java


    He may be calling it from another class or from a "scrapbook". Actually this
    is much better because the "main method" usually clouds thinking and judgement.

    I was able to run it.

    "Qbert" <java.@127.0.0.1> wrote:
    >
    >
    >Where is your main method?
    >
    >
    >"Mr. T" <java.@127.0.0.1> wrote:
    >>
    >>This program is supposed to move "a name"
    >>from a text field to a text area when the change button is clicked, it

    is
    >>also supposed to move "a name" back and forth on subsequent button clicks.
    >>I have gotten this far, but have been stuck now for a couple of days. this
    >>code will take "a name" from the text field and place it in the text area
    >>(But it does not remove "a name" from the text field) I am new to Java

    and
    >>what I thought would work (using if,else statments) did not work. Any

    Help
    >>would be appreciated.
    >>
    >>Thanks
    >>Code below
    >>import java.awt.BorderLayout;
    >>import javax.swing.JButton;
    >>import javax.swing.JFrame;
    >>import java.awt.event.ActionEvent;
    >>import java.awt.event.ActionListener;
    >>import javax.swing.JTextField;
    >>import javax.swing.JPanel;
    >>import javax.swing.JTextArea;
    >>
    >>
    >>public class NameOnFrame extends JFrame
    >>{
    >> public NameOnFrame()
    >> {
    >>
    >>
    >>
    >>
    >> /* this creates a text field with my name in it
    >> @param in the Text Field with my name in it
    >> */
    >> final JTextField text = new JTextField("a name");
    >> text.setEditable(false);
    >>
    >>
    >> JButton changeButton = new JButton("Change");
    >>
    >>
    >>
    >> final JTextArea outputHere = new JTextArea("");
    >> outputHere.setEditable(false);
    >>
    >> //This is what happens when the button gets clicked
    >> class changeButtonListener implements ActionListener
    >> {
    >> public void actionPerformed(ActionEvent event)
    >> {
    >> //This gets the name from the JTextField
    >> String name = new String();
    >> name = text.getText();
    >>
    >> //This prints the name inside the text area
    >> outputHere.setText(name);
    >>
    >>
    >>
    >> }
    >> }
    >>
    >> ActionListener listener = new changeButtonListener();
    >> changeButton.addActionListener(listener);
    >>
    >>
    >> getContentPane().add(text, BorderLayout.WEST);
    >> getContentPane().add(changeButton, BorderLayout.EAST);
    >> getContentPane().add(outputHere, BorderLayout.SOUTH);
    >> }
    >>}

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center