• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two separate classes (not inner classes) and ActionListener question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice nice, sounds good! Thanks again Pete :-) bout to go try that now
 
Sebastion Hill
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 :-)
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic