JButton and a new Frame

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    JButton and a new Frame

    Hello. I got a problem with a JButton in a game that i am making.
    I have a mainFrame that is where we play the game.In this class i have a main method in order to test it.We also have a first window.In first window we have a button play and when we push it , we show the mainFrame.

    The problem: When i push the button it shows me the mainFrame but all the window is white.( i should see some pictures and buttons)

    In actionPerformed method i copy paste the code of the main class of mainFrame, which works correctly, so i guess that it will works also correct in actionPerformed ,but no.
    thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your button's actionPerformed () method runs in the AWT thread so that thread
    is busy and doesn't have any time left to do all your drawing operations. When
    you did the same from your main() method you did the frame initialization in
    another thread leaving the AWT thread free to do all the drawing.

    Never keep the AWT thread busy (for too long), i.e. do your mainFrame setup
    and stuff in another thread. Have a look at the SwingUtilities and Thread
    classes.

    kind regards,

    Jos

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Thank you jos but i wouldn't like to learn about threads now.I don't have the time.
      Is there any way to close the first window(not to setVisible(fals e) but close it) and then run the main from the other class?(all these in Action Performed)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by kalar
        Thank you jos but i wouldn't like to learn about threads now.I don't have the time.
        Is there any way to close the first window(not to setVisible(fals e) but close it) and then run the main from the other class?(all these in Action Performed)
        Simply use the SwingUtilities class; now you have something like this:

        [code=java]
        public void actionPerformed (ActionEvent ae) {
        <do something here>
        }
        [/code]

        All you have to do is change it to this:

        [code=java]
        public void actionPerformed (ActionEvent ae) {
        SwingUtilities. invokeLater(new Runnable() {
        public void run() {
        <do something here>
        }
        });
        }[/code]

        This invokes whatever needs to be done later when the AWT thread has nothing
        to do anymore. Read the API documentation for details.

        kind regards,

        Jos

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          i make what you say jos but it didn't work.Look now what it happens(i delete the picture in the first window)


          when i push play it opens the second window,it plays the music that i have put but it shows me back of the screen ,the first window and the code in Jcreator,which i have open him

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by kalar
            i make what you say jos but it didn't work.Look now what it happens(i delete the picture in the first window)


            when i push play it opens the second window,it plays the music that i have put but it shows me back of the screen ,the first window and the code in Jcreator,which i have open him
            I don't know what you're doing in that actionPerformed () method so I can't give
            you a reasonable answer; but it does activate the code? It just doesn't display
            the other frame? Please elaborate.

            kind regards,

            Jos

            Comment

            • kalar
              New Member
              • Aug 2007
              • 82

              #7
              Originally posted by JosAH
              I don't know what you're doing in that actionPerformed () method so I can't give
              you a reasonable answer; but it does activate the code? It just doesn't display
              the other frame? Please elaborate.

              kind regards,

              Jos
              so in ActionPerformed i am doing:
              Code:
                  public void actionPerformed(ActionEvent e)
                  {
                  	if ("play".equals(e.getActionCommand()))
                  	{
                  	SwingUtilities.invokeLater(new Runnable() {
                
                          public void run() {
                
                           Game.modif();//this  just fill some arrays
                           Game frame = new Game();//here is the mainFrame of the game
                           frame.setVisible(true);
                      
              
                           //this just play a sound
                           AudioClip click;
                           URL urlClick = Game.class.getResource("slow.wav");
                           click = Applet.newAudioClip(urlClick);
                           click.loop();
                           frame.play();
                     
                    
                
                          }
                
                       });
                      
                     	
                  	}

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by kalar
                so in ActionPerformed i am doing:
                Code:
                    public void actionPerformed(ActionEvent e)
                    {
                    	if ("play".equals(e.getActionCommand()))
                    	{
                    	SwingUtilities.invokeLater(new Runnable() {
                  
                            public void run() {
                  
                             Game.modif();//this  just fill some arrays
                             Game frame = new Game();//here is the mainFrame of the game
                             frame.setVisible(true);
                        
                
                             //this just play a sound
                             AudioClip click;
                             URL urlClick = Game.class.getResource("slow.wav");
                             click = Applet.newAudioClip(urlClick);
                             click.loop();
                             frame.play();
                       
                      
                  
                            }
                  
                         });
                        
                       	
                    	}
                Ok, now carefully tell us what happens and shouldn't happen and what doesn't
                happen that should. As I understand from your previous message the sound
                plays (check) and the window is shown (also check). What is wrong?

                kind regards,

                Jos

                Comment

                • kalar
                  New Member
                  • Aug 2007
                  • 82

                  #9
                  yes the sound plays but as you can see in the image in my post the second window doesn' t show anything. It shows the first window and the code from jcreator that is opened behined the programm.
                  it should be like this the second window

                  i take this picture when i run the main method from the mainFrame game
                  this main has the same code with this in ActionPerformed

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by kalar
                    yes the sound plays but as you can see in the image in my post the second window doesn' t show anything. It shows the first window and the code from jcreator that is opened behined the programm.
                    it should be like this the second window

                    i take this picture when i run the main method from the mainFrame game
                    this main has the same code with this in ActionPerformed
                    All this only implies that your code (possibly in your other frame) obstructs the
                    AWT thread so it can't draw anything anymore. Try the following little hack:

                    [code=java]
                    public void actionPerformed (ActionEvent ae) {
                    new Thread(new Runnable() {
                    public void run() {
                    <do something here>
                    }
                    }).start();
                    }
                    [/code]

                    This lifts all your code into another thread and shouldn't block the AWT thread.

                    kind regards,

                    Jos

                    Comment

                    • kalar
                      New Member
                      • Aug 2007
                      • 82

                      #11
                      Thank you Jos!!!! It works.
                      Now is there a way to close only the first window(it goes back from the mainFrame of the game ) or the first Thread(if i say it correct) and leave the mainFrame of the game or the second thread as it is in order to play?

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by kalar
                        Thank you Jos!!!! It works.
                        Now is there a way to close only the first window(it goes back from the mainFrame of the game ) or the first Thread(if i say it correct) and leave the mainFrame of the game or the second thread as it is in order to play?
                        Sure, what do you think JFrame.dispose( ) or JFrame.setVisib le(false) do?

                        kind regards,

                        Jos

                        Comment

                        Working...