aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with the JButton. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with the JButton." Watch "Problem with the JButton." New topic
Author

Problem with the JButton.

Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
1- i have 2 buttons, i want to add them to the same location, when i click on button1 then the button1 will hidden and show button2, when i click on button2 then
button2 will hidden and show button1 again? please help me to slove my problem.
2- I have one button, but in this button i want to add 2 images (image1 and image2), when start the program, it's will show default button with image1, i want to
when i click on this button then image1 will hidden and show image2 on the button, when i click obn this button again so image2 will hidden and image1 show again.\
thanks for you help
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8


1- i have 2 buttons, i want to add them to the same location, when i click on button1 then the button1 will hidden and show button2, when i click on button2 then
button2 will hidden and show button1 again? please help me to slove my problem.
2- I have one button, but in this button i want to add 2 images (image1 and image2), when start the program, it's will show default button with image1, i want to
when i click on this button then image1 will hidden and show image2 on the button, when i click obn this button again so image2 will hidden and image1 show again.\
thanks for you help


1) Why do you want to do that? What effect/functionality are you trying to achieve ?It is bad usability to show/hide/flip components on an existing UI. Are you aware, you can change the Icon/Text of a button any time?

2)Same as above. You can change the Icon of the button. Check out AbstractButton#setIcon(Icon icon) and AbstractButton#setSelectedIcon(Icon icon). You can subclass the JButton and maintain the state and flip icons accordingly. Also, if you want your button to "stay pressed/toggled" check out JToggleButtton.
[ October 05, 2008: Message edited by: Maneesh Godbole ]

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Thanks you very much.
That's my requrement, i have to finish it.
When i click on button 1, i will show panel1 and draw the chart in panel1 whith bar chart.
When i click on button 2, i will show panel2 and draw the chart in panel2 with line chart.
Button1 and button2 must the same location or only one button but deference image.
thanks for your help
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
dear all
i have new problem, i want to show the corresponding panel when i click on the button.(when i click on button with BART.jpg image it's will show panel1, when i click on button with LINE.jpg image it's will show panel2)I already create a flag, but it's not work.
Here is my source code:

please help me, thanks you very much
[ October 06, 2008: Message edited by: Tran Tuan Hung ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8



Try calling revalidate() and repaint() after you set the panels.
Also take a look at the CardLayout. It does exactly what you want to do without you needing to code much.
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
i try this:

But it's not work, i already find the method revalidate() but i was not found.
I will research CardLayout follow your suggestion.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Sorry. I should have done this before I posted initially.

I ran your original code. Works for me just fine.
Most probably you are clicking on the button titled "BUTTON ON PANEL 1" on the NORTH which is NOT the button with the registered listener.
Try clicking on the button at the center (with the icon).

I still strongly urge you to try out the CardLayout.
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Thanks you for reply,
OK, i already research on CardLayout, anhd i code it follow, but it still not works, please help me to correct it.
Here is my source code:
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Tran Tuan Hung:
[QB].. but it still not works, please help me to correct it.


What exactly do you mean when you say it doesn't work?
Have you looked at this:
http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
I already successed, thanks you very much.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Tran Tuan Hung:
I already successed, thanks you very much.


Care to share your solution so others can benefit from it in the future?
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
OK You are the man,
I'll keep this solution for helping others. I think CardLayout that's userfull, interesting, but perhaps there're few persion know that.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

I think you misunderstood.
I was requesting you to post your code snippet that works (like you have done previously), so that, in future, if anyone wants to solve a similar problem, they can refer to your code.
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Oh, OK and sorry for my misunderstood.
I post it now, hihi
Here is my source code, please comment for me hihi, i modify from your links.

[ October 07, 2008: Message edited by: Tran Tuan Hung ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with the JButton.