Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Kris VanStrijdonck
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:


 
Campbell Ritchie
Marshal
Posts: 55672
161
  • 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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!
 
m Korbel
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!