• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

2 newbie questions on JButton/actionListener and validate()

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to build a little program to manage an FAQ, that can basically:

1 store a question (text1) and an answer (text2)
2 make a button in my gui that has the question on it
3 copy the answer to the clipboard when I click the button

Number 1 goes OK. I have a question about number 2 and 3:

Number 2: when I add a new question and answer combinaton (fiche), I validate the panel in which the buttons are displayed to make the new button visible.
However, it add's not only the new button, but also the buttons that were already there!
So when I have one button A displayed and add a new one B, it doesn't refresh to A B, but to A AB. After adding a third button, it's not A B C, but A AB ABC. So it add's them up!
Clearly I'm making a mistake here, but I can't find it

Number 3: this is more of a general question: while building, I came to the conclusion that I can't copy the answer to the clipboard because the answer variable is in another class (Fiche) than the one the code is running in (TestFrame/ActionListener class in TestFrame). How do I solve this?
Is it possible to include the ActionListener in the constructor of the (J)Button so that I can attach the answer as a parameter while creating a new button (like it does with the question now)?
Or does the answer lie in another direction?
(I already have a working code to copy the content of a JTextArea to the clipboard: I haven't displayed it in my code yet because of the problem: I can't access the right variable yet)

Thanks for any help !

This is my code (it compiles and runs):

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I could understand from the question and the given code (correct me if I have misunderstood):

For- Number 2- You can remove all the buttons from the panel (If the panel contains only buttons, you can remove all the components from the panel) and then add the buttons- You are looping through each FAQ and adding the corresponding button?

For Number-3- If you want to access the FAQ Class- You can create a Custom Action listener which Implements ActionListener. Along with that you can have the reference to the current FAQ Class for which the instance of this was created and you can access in the ActionPerformed.


Then you can have a "Copy" button for each FAQ and then while setting the ActionListener for each Copy button, you can get the corresponding Fiche instance for that FAQ and then pass it along with the CopyActionListener constructor.

I hope this is what you were looking for.
 
Vincent Hendriks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohamed,

For- Number 2- You can remove all the buttons from the panel (If the panel contains only buttons, you can remove all the components from the panel) and then add the buttons- You are looping through each FAQ and adding the corresponding button?



Exactly! I added and now it works like a charm
validate() didn't do what I thought it would do...

For Number-3- If you want to access the FAQ Class- You can create a Custom Action listener which Implements ActionListener. Along with that you can have the reference to the current FAQ Class for which the instance of this was created and you can access in the ActionPerformed.



Ahh... so instead of customizing the button itself, I have to customize the ActionListener...
That's usefull
I'll start working on that.

Thanks Mohamed!
 
Vincent Hendriks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohamed, I succeeded with setting the different ActionListeners too

Thanks again!!!
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vincent Hendriks wrote:Mohamed, I succeeded with setting the different ActionListeners too

Thanks again!!!



Congrats
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic