jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes 2 newbie questions on JButton/actionListener and validate() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "2 newbie questions on JButton/actionListener and validate()" Watch "2 newbie questions on JButton/actionListener and validate()" New topic
Author

2 newbie questions on JButton/actionListener and validate()

Vincent Hendriks
Greenhorn

Joined: Oct 19, 2010
Posts: 16
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):

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
Vincent Hendriks
Greenhorn

Joined: Oct 19, 2010
Posts: 16
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

Joined: Oct 19, 2010
Posts: 16
Mohamed, I succeeded with setting the different ActionListeners too

Thanks again!!!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Thanks again!!!


Congrats
 
 
subject: 2 newbie questions on JButton/actionListener and validate()