pete stein wrote:One way to do this simply is to give your panel that holds the delete button a public method that allows you to add an actionlistener to it, Then to use this, simply add this class's jpanel to your GUI and then add an actionlistener to the object with the public method above
Sebastion Hill wrote:Thank you for your help Pete. I got it working based on what you said. I am curious though if there is a way to pass any information (namely a string) along in the public method that allows you to add an actionlistener? I have a getTitle method in the "PanelWithDeleteBtn" class that returns a string ... is there any way I can get that string when the delete button is pushed?
pete stein wrote:
Sebastion Hill wrote:Thank you for your help Pete. I got it working based on what you said. I am curious though if there is a way to pass any information (namely a string) along in the public method that allows you to add an actionlistener? I have a getTitle method in the "PanelWithDeleteBtn" class that returns a string ... is there any way I can get that string when the delete button is pushed?
You don't want to pass the string when you add the actionlistener. You want the listener's actionPerformed to get the String when the method is called. For that, give the class that has the String a public getter method.
Sebastion Hill wrote:hey Pete, so I tried the equivalent of "public final class PanelWithDeleteBtn" but it still doesn't work ... I still get the same error and I don't think that I can set the actual variable to final because it is reading in a value from the user in the GUI, so wouldn't declaring it final prevent it from reading in the value?
And I know how to make an inner class within class PanelWithDeleteBtn but I still don't know how to access it to get to the actual String variable I am looking for. Just create a new instance of it and go that way? My PanelWithDeleteBtn creates multiple objects, one each time a button in class MainApp is pushed. I want to get the string for the current PanelWithDeleteBtn so that I can know which panel to delete. :-/
pete stein wrote:
Sebastion Hill wrote:hey Pete, so I tried the equivalent of "public final class PanelWithDeleteBtn" but it still doesn't work ... I still get the same error and I don't think that I can set the actual variable to final because it is reading in a value from the user in the GUI, so wouldn't declaring it final prevent it from reading in the value?
It works for me, and I'm afraid that it's hard to say why you're getting the error without seeing code where you try to do this. I can almost guarantee that the error has nothing to do with one component trying to read in a value from the user in the GUI, but can't give you a 100% guarantee without seeing code.
And I know how to make an inner class within class PanelWithDeleteBtn but I still don't know how to access it to get to the actual String variable I am looking for. Just create a new instance of it and go that way? My PanelWithDeleteBtn creates multiple objects, one each time a button in class MainApp is pushed. I want to get the string for the current PanelWithDeleteBtn so that I can know which panel to delete. :-/
I'm curious, what exactly is the app trying to do? Are you displaying a panel next to a panel, and then if the user presses delete, both panels disappear?
Here's a simple example where the second panel is declared a final local variable. The second panel has a button and a JTextField as well as a public method to get text out of the text field. The first panel will display that text in a jtextarea on button press:
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|