Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Two separate classes (not inner classes) and ActionListener question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Two separate classes (not inner classes) and ActionListener question" Watch "Two separate classes (not inner classes) and ActionListener question" New topic
Author

Two separate classes (not inner classes) and ActionListener question

Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
Could someone tell me if the following is possible? And if it is, could you help explain what needs to be done?



The deletebutton is in the ActivityTab class but I want my TabDemo class to be affected by it when it is pushed ...



Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
btw, if I add the ActivityClass to the TabDemo class as an inner class it works perfectly ... just wondering if that is required or if there is another way to leave them as separate classes and still accomplish the same thing?

this works:

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I think that allowing outside classes to add listeners to buttons is good, *but*, you definitely do not want to have the listener extend ActivityTab because the listener fails the "is-a" test. 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, e.g.,:


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
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
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


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?

btw, didn't mean to leave the extends ActivityTab in there ... I think I was trying things and forgot to take it out when I posted the first time
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
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.


I have public getter methods but I can't access them from within method ... for example, using your example, "panelWithDelBtn.getPanel();" would not work. In my code I am trying to use something like paneWithDelBtn.getTitle(); but if I do that then I get a runtime error saying that getTitle() needs to be final

error: "local variable [name] is accessed from within inner class; needs to be declared final" ... so I am unsure how to access the public getter method you're talking about
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
That's one of Java's rules -- inner classes can only access fields that are global to the class or local variables that have been declared final. So a solution to this is to make your panel object either a final variable or a class field. Either would work.
Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
nice nice, sounds good! Thanks again Pete :-) bout to go try that now
Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
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. :-/

Any other suggestions? My code is looks more like this using your previous example



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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:


Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
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:


Hi Pete so I was putting the final in the PanelWithDeleteBtn class and not in the MainApp class. I had previously tried "final PanelWithDeleteBtn.getTextFieldText" as opposed to "final PanelWithDeleteBtn panelWithDelBtn = new PanelWithDeleteBtn()" in the MainApp class but that didn't work I thought I had to make the actual PanelWithDeleteBtn class final.

Your example cleared it up and it works now. Also, in regards to your questions I have a JTabbedPane running vertically as the main frame for my GUI. It is divided into two separate tabs. In one tab there are more JTappedPanes running horizontally and the other tab there is an area for user input to list a name, description, and due date. After the user enters in the info and presses the "Save Button" the code creates a new tab that goes into the horizontal JTabbedPane.

There are a lot of horizontal tabs and the delete button is specific for each new tab created. I was just trying to get something to identify the tab so that I can delete the specific tab I am looking for.

Again thanks for your example, definitely cleared up what I was suppose to be marking as final. Thanks Pete :-)
 
 
subject: Two separate classes (not inner classes) and ActionListener question
 
Similar Threads
Need help understanding why my actionlistener is causing my program to freeze
could someone look at my code and help me with a JScrollPane in a JTextPane please?
completely lost in terms of trying to understand Collections.synchronizedList
How do I access my field data?
save object?