Results 1 to 13 of 13
  1. #1
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Java Applet button not appearing

    I'm not very new to Java itself - I know how to use classes and loops and whatnot - but I have very limited knowledge of using applets. What I'm trying to create is just a simple menu system using buttons that I can get input from to send back to the program. The problem is that I can't get the second button to appear after clicking on the first one. I had seen something about revalidate being needed but not an explanation of what to do with it, so it's just thrown in at random points to see if it would help. Anyone know what I'm doing wrong?

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class PatternApplet extends Applet implements ActionListener {
    
        Button startButton;
        Button pre1Button;
    
        int menu = 0;
    
    	public void init() {
    
    		setLayout(new FlowLayout());
                    pre1Button = new Button("Preset 1");
        	        startButton = new Button("Start");
     
            add(startButton);
    
            if (menu == 1){
    		    setLayout(new FlowLayout());
    		    add(pre1Button);
    		    revalidate();
            }
    
            startButton.addActionListener(this);
    	}
    
    	public void paint(Graphics g) {
    		if (menu == 0){
    			g.drawString("Hit Start to continue.", 30, 60 );
    		}
    	}
    
    	public void actionPerformed(ActionEvent evt) {
    		 if (evt.getSource() == startButton){
    			remove(startButton);
    			menu = 1;
    			repaint();
    
    		 }
    		revalidate();
        }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    When is the code that adds the second button executed?
    Is it ever executed?

    Do you know when the init() method is called? Is it called more than once?

  3. #3
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    The code to add the second button is executed once the menu variable has been changed to 1. I'm guessing my problem is that the init method is only called once, and changing the variable does not cause it to execute the code for the second button. I had tried putting the second button code directly in the actionPerformed method, but the button still didn't appear.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    What do you want to do now? If you posted your code someone could try to help.

  5. #5
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    I already posted the code in the first post. What I need help with is what to do to make the second button appear once the start button has been pressed. Should I use some code to recall the init method? If so, could you please tell me what?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    If you want the new button to appear after the other button has been pressed, what happened when you added the button in the listener method? Why didn't that code work?

    No, you do not what to call the init() method. You could make a new method and call that.

  7. #7
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    When I added it to the listener method, it removed the start button as it was supposed to, but the screen remained blank instead of showing the new button. I tried using a method but got the same result.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    You need to post the code and the errors you are getting. I can't see what the problem is without seeing the code.

  9. #9
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    Here's the code...

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class buttontest extends Applet implements ActionListener {
    
        Button startButton;
        Button pre1Button;
    
        int menu = 0;
    
        public void init() {
    
            setLayout(new FlowLayout());
            pre1Button = new Button("Preset 1");
            startButton = new Button("Start");
    
            add(startButton);
    
            startButton.addActionListener(this);
        }
    
        public void paint(Graphics g) {
            if (menu == 0){
                g.drawString("Hit Start to continue.", 30, 60 );
            }
        }
    
        public void actionPerformed(ActionEvent evt) {
             if (evt.getSource() == startButton){
                remove(startButton);
                menu = 1;
                repaint();
                add(pre1Button);
             }
        }
    }
    and this is what happens when it runs -
    Java Applet button not appearing-menu0.png
    There are no console errors, the windows displays normally.

    I click the start button, it disappears, and the screen goes blank.
    Java Applet button not appearing-menu1.png

    There are still no errors. The only thing wrong is it not displaying another button.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    After you change the contents of a container you need to tell the container that you are done making changes and that you want the container's contents laid out again. See the validate() method.

  11. #11
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    I have heard of using the validate method, but as I have mentioned, do not know how to use it. Could you show me an example please?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java Applet button not appearing

    You call it: validate();

  13. #13
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Java Applet button not appearing

    Ah, thank you. I had been trying to use revalidate instead of validate. The program works as it should now.

Similar Threads

  1. Java Applet that displays two pictures with a button...
    By yenujnassah in forum New To Java
    Replies: 0
    Last Post: 11-10-2011, 09:54 AM
  2. Make java applet a button
    By hervey in forum New To Java
    Replies: 31
    Last Post: 10-30-2008, 05:44 AM
  3. java applet button problem .. :( plz help!
    By i4gotmyid in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:11 PM
  4. java applet button problem .. :( plz help
    By i4gotmyid in forum New To Java
    Replies: 0
    Last Post: 04-05-2008, 09:55 AM
  5. search button java applet awt
    By perplexingtrax in forum New To Java
    Replies: 2
    Last Post: 03-24-2008, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •