This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Making a JButton that switches text from 2 JTextFields on click with mouse

 
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a window using JFrame and on my JPanel I have added 3 components: 2 JTextFields ("field1" and "field2") and inbetween them a JButton ("switch"). My goal is to switch the value of field1 to field2 and vice versa when the JButton is clicked. I thought this ActionListener which I have added to my JButton would achieve my goal:



However, it changes the value of field2 into the value of field1 but not the other way around.

Any help would be appreciated.

Thanks,

Kris
 
Kris VanStrijdonck
Greenhorn
Posts: 2
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the answer; I do not need any help anymore.

For anyone wondering, my mistake was not using a variable to hold the text in field2:


 
Marshal
Posts: 65779
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Some languages support this sort of syntax x, y := y, x but Java┬« doesn't, so the technique you used is the commonest way to swap two values. There is another way to do it:-I don't think a throwaway object like that will actually require much of an overhead. I challenge you to show us the code for the TextSwapper class

It is probably better to have the actionPerformed method very short and let it call another method.
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kris VanStrijdonck wrote:I have found the answer; I do not need any help anymore.


Well done finding the solution yourselves....and sharing it with others! For all your efforts, you just earned yourself a cow! Moo!
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crossposted, to :-) I have found the answer; I do not need any help anymore. :-) yes thats right
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!