Applet - Button.setVisible(true) wont work outside init()

Hi,

I've written an applet (not a j applet). Inside the init() method I create a button, set it to invisible i.e. Button.setVisible(false), add it to a panel and then add the panel to the applet. In the actionPerformed method I then set the Button to visible again. It appears that setting the button to visible inside the init() method will work, but setting it to visible in any other method wont. Is there any other way that I can make a Button appear outside of the init() method?

Thanks a lot.
grahamyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
And what is creating ActionEvents?
0
CEHJCommented:
If you set it to visible in start() it should show
0
CEHJCommented:
What's with this appearing/disappearing button though?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

grahamyAuthor Commented:
Another button is creating action events. Basically when the other button is pressed I want this button to be visible, but not before the button is pressed.
0
CEHJCommented:
Should be OK. Can you post your code?
0
java_gurlCommented:
you're code should work. maybe you're retarded and don't know how.

Just a suggestion "grahamy"!
0
CEHJCommented:
This kinid of thing does what you want - give it a try:

import java.awt.* ;
import java.awt.event.* ;
import java.applet.* ;
import javax.swing.* ;
import javax.swing.event.* ;


public class LogApplet1 extends JApplet implements ActionListener {
  JButton buttonA;
  JButton buttonB;

public void init( )
{

  Container c = getContentPane();
  c.setLayout(new FlowLayout());

  try {
    buttonA = new JButton("Appear/Disappear");

    buttonA.addActionListener(this);
    buttonA.setVisible(false);
    c.add(buttonA);

    buttonB = new JButton("Controller");
    buttonB.addActionListener(this);
    c.add(buttonB);

  }
  catch(Exception e){
    e.printStackTrace();
  }


}


public void actionPerformed (ActionEvent evt)
{
  if (evt.getSource() == buttonB) {
    buttonA.setVisible(true);
  }
}

}

//////////////////////////////////

HTML file:

<applet code="LogApplet1.class" codebase="." width="400" height="400">
</applet>
0
glottisCommented:
the last post by CEHJ works for me. all you want to do is now remove all the "J"s to make it AWT one :)
0
objectsCommented:
Check that the button you create in your init() method is the same one that you make visible from your other method, and that you aren't accessing a different variable.
The button should be a class member variable.
0
CEHJCommented:
OK - here is a non-Swing version:

import java.awt.* ;
import java.awt.event.* ;
import java.applet.* ;


public class LogApplet1 extends Applet implements ActionListener {
  Button buttonA;
  Button buttonB;

  public void init() {
    try {
      buttonA = new Button("Appear/Disappear");

      buttonA.addActionListener(this);
      buttonA.setVisible(false);
      add(buttonA);

      buttonB = new Button("Controller");
      buttonB.addActionListener(this);
      add(buttonB);

    }
    catch(Exception e){
      e.printStackTrace();
    }
  }


  public void actionPerformed (ActionEvent evt) {
    if (evt.getSource() == buttonB) {
      buttonA.setVisible(true);
      validate();
    }
  }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sexperts_exchangeCommented:
Hey Yvette, I wouldn't mind having these points!

Or else i'll come out to portmarnock and kick your ass!
0
SpideyModCommented:
DO NOT ACCEPT ANY ANSWER TO THIS QUESTION UNTIL THIS GETS STRAIGHTENED OUT!

sexperts_exchange, grahamy, gibbonss,

The Jig is up.  All of you have violated the points abuse clause in the Membership agreement.  On top of that there's been foul language also against the MA.  A site administrator has been notified and I have removed all of your available question points until the matter gets resolved.

If you have any other IDs on this site, it would be wise to disclose them.

SpideyMod
Community Support Moderator @Experts Exchange
0
Computer101Commented:
All 4 accounts suspended

Computer101
E-E Admin
0
CleanupPingCommented:
grahamy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to CEHJ

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
objectsCommented:
Thought this account got suspended.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.