?
Solved

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

Posted on 2003-03-11
17
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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