Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-11
17
Medium Priority
?
491 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:grahamy
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8111311
And what is creating ActionEvents?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8111315
If you set it to visible in start() it should show
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8111342
What's with this appearing/disappearing button though?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:grahamy
ID: 8111354
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8111377
Should be OK. Can you post your code?
0
 

Expert Comment

by:java_gurl
ID: 8111418
you're code should work. maybe you're retarded and don't know how.

Just a suggestion "grahamy"!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8111459
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
 
LVL 2

Expert Comment

by:glottis
ID: 8114517
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
 
LVL 92

Expert Comment

by:objects
ID: 8115180
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 8119241
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
 

Expert Comment

by:sexperts_exchange
ID: 8341330
Hey Yvette, I wouldn't mind having these points!

Or else i'll come out to portmarnock and kick your ass!
0
 

Expert Comment

by:SpideyMod
ID: 8345116
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
 
LVL 1

Expert Comment

by:Computer101
ID: 8345232
All 4 accounts suspended

Computer101
E-E Admin
0
 

Expert Comment

by:CleanupPing
ID: 9058784
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
 
LVL 35

Expert Comment

by:girionis
ID: 9700599
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
 
LVL 92

Expert Comment

by:objects
ID: 9718781
Thought this account got suspended.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question