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.
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.
posted 5 years ago
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:
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.