Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to solve this?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to solve this?

    This code generates 2 text fields and one button. Typing a text in the first textfield and then pressing on the button, copies the text from the first textfield (invoerVeld) to the second textfield (uitvoerVeld).

    package opdracht1;
     
    import javax.swing.*;
    import java.awt.event.*;
     
    public class SwitchPaneel extends JPanel implements ActionListener
    {
    	private JTextField invoerVeld;   
    	private JTextField uitvoerVeld;  
    	private JButton actieKnop;       
     
     
    	public SwitchPaneel() 
    	{
    		actieKnop = new JButton(" Verwissel ");   
    		actieKnop.addActionListener(this);       
     
    		invoerVeld = new JTextField(10);   
    		uitvoerVeld = new JTextField(10);  
     
    		add (invoerVeld); 
    		add (actieKnop);   
    		add (uitvoerVeld); 
    	}
     
    	public void actionPerformed (ActionEvent ae)      
    	{
    		uitvoerVeld.setText(invoerVeld.getText());
    	}
    }

    Now I need to make it so that pressing the button, gets the text from invoerVeld to uitvoerVeld, and the text from uitvoerVeld to invoerVeld.
    Basically, it has to switch the two inputs.

    When I try to do this:
    	public void actionPerformed (ActionEvent ae)      
    	{
    		uitvoerVeld.setText(invoerVeld.getText());
    		invoerVeld.setText(uitvoerVeld.getText());
    	}

    It doesn't work.
    How can I solve this?

    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to solve this?

    You should store uitvoerVeld.getText() in a temporary String variable, because once you change uitvoerVeld.setText(invoerVeld.getText()), you lost the text that was in uitvoerVeld.getText().

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to solve this?

    Quote Originally Posted by squeakbox View Post
    You should store uitvoerVeld.getText() in a temporary String variable, because once you change uitvoerVeld.setText(invoerVeld.getText()), you lost the text that was in uitvoerVeld.getText().
    How would I do this? Sorry, really new in Java.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to solve this?

    Tried this:

    	public void actionPerformed (ActionEvent ae)      
    	{
    		String veldinhoud = invoerVeld.getText();
    		uitvoerVeld.setText(invoerVeld.getText());
    		invoerVeld.setText(veldinhoud);
    	}

    But didn't work :O.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to solve this?

    Fixed it.

    	public void actionPerformed (ActionEvent ae)      
    	{
    		String veldinhoud = uitvoerVeld.getText();
    		uitvoerVeld.setText(invoerVeld.getText());
    		invoerVeld.setText(veldinhoud);
    	}

    Thanks squeakbox!

Similar Threads

  1. anyone can help me to solve this?
    By d3sm0nd in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 27th, 2012, 06:47 AM
  2. Please can anyone help solve this ?
    By jennyb in forum Java Theory & Questions
    Replies: 1
    Last Post: January 6th, 2012, 09:45 PM
  3. how I can solve this program ?
    By Noni in forum Object Oriented Programming
    Replies: 9
    Last Post: January 20th, 2011, 05:33 PM
  4. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 04:26 PM
  5. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 01:45 PM