I have JPanel
with text box, save button in another panel. If I click the save button
I have to get the 1st panel text box value.
How to access it?
I have JPanel
with text box, save button in another panel. If I click the save button
I have to get the 1st panel text box value.
How to access it?
Just make the TextField as Public Static dats it. And then u can Access the TextField Using ClassName.TextFiledName
An easy solution will be create a constructor in the class where you implement your ActionListener and pass in the constructor the components that you need to update or to retrieve values.
This solution will work, but there is a better approach that allows make the code more reusable. Take a look at the observer pattern and use it in your code.
Well since there's no SSCCE, this will be a general answer.
First you could make the text box public static field and access it from the other class but that will be one of the worst code you will ever write. Second you can use setters/getters methods which are cool but don't behave well as your program grows complex. Setters/getters will create tight coupling between components. Finally I suggest using the Observer pattern. It may seem like using a nuke against a bicycle but in the end it is well worth the initial trouble - and you learn something in the process.
More information and example source code can be found at Source making.
You should have a
Controller
class from where you create the panel. Keep reference to the panel in the controller class. Expose a getter method in yourCustomPanel1
to return the text in the TextField(not the textfield itself). Similarly you create the secondCustomPanel2
also from theController
and keep reference. Define the listener class in your controller and pass it to your second panel. In the second panel add the listener to your button.This would be a simple solution.
Swing Tutorial