Results 1 to 11 of 11
  1. #1
    Samari413 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Displaying an image

    I want the picture to show for a few seconds and then go away....
    it does show, but it doesnt want to go away...
    frame.setVisible(false); does not work...
    please help me!!


    Java Code:
       import javax.swing.*;
       import java.awt.*;
    
         public class GoeiePunt extends JFrame {
       
           public void showImage() {
         
          
             JFrame frame = new JFrame("VOLPUNTE! Baie geluk!");
          
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
             frame.setSize(600, 500);
          
             frame.setResizable(false);
          
             frame.setLocationRelativeTo(null);
     
          
             String imgStr = "PIC.jpg";
          
             ImageIcon image = new ImageIcon(imgStr);
          
             JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
          
             frame.getContentPane().add(label1);
          
             frame.validate();
          
             frame.setVisible(true);
          
          	 for(int tel = 1; tel < 9999990;tel++)
    			 {
    			 
    			 }
          }
           GoeiePunt()
          {
             showImage();
          } 
       }
    Last edited by Eranga; 11-06-2010 at 01:30 PM. Reason: moderator edit: code tags added

  2. #2
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Talking

    Hey Samari! Glad to see you found the forum... Please use code tags to put in code: Unformatted code is really hard to read. Click on edit at the bottom of your post an put it in. Here is the link on how to do this: Java Forums - BB Code List
    Hope you find this useful.
    Last edited by Johanis; 11-06-2010 at 12:03 PM. Reason: us = is

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags next time when you are posting code segments over here in the forum. Unformatted codes are really hard to read.

    Regarding your question, use swing timer instead serVisible invoke.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Samari413 View Post
    I want the picture to show for a few seconds and then go away....
    it does show, but it doesnt want to go away...
    frame.setVisible(false); does not work...
    please help me!!
    I've added code tags for you, but I'm curious that you say that setVisible(false) doesn't work, yet I don't see where you have used setVisible(false) in the code above.

    Myself, I'd use a Swing Timer if I wanted to show something for a set period of time, and I probably wouldn't display it in a JFrame but more likely a JDialog or a JPanel in the main JFrame.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Myself, I'd use a Swing Timer if I wanted to show something for a set period of time, and I probably wouldn't display it in a JFrame but more likely a JDialog or a JPanel in the main JFrame.
    I agreed too. Mainly timer object calls an action listener at regular intervals or only ones, where easy to control the design part in easy steps.

  6. #6
    Samari413 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Thank you...I deleted the setVisible(false) because it did not work.
    As i only started programming a while ago I dont really know how to do everything. Could you maybe explain to me how to use a Swing Timer?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Samari413 View Post
    Thank you...I deleted the setVisible(false) because it did not work.
    When that happens, we ask that you post the non-working code so that we can see what you're doing wrong. Otherwise we have no idea really.


    As i only started programming a while ago I dont really know how to do everything. Could you maybe explain to me how to use a Swing Timer?
    I can do better than that. I can link you to the tutorial where you'll find information and sample code: Swing Timer Tutorial

    Much luck!

  8. #8
    Samari413 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I used a swing timer.
    It still doesnt seem to be working.
    Could you help me find my mistake?

    Java Code:
     import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
       import javax.swing.JFrame; import javax.swing.Timer;
       import javax.swing.*; import java.awt.event.*;
       
        public class Volpunte extends JFrame
       { 
           public void showImage() 
          {
             JFrame frame = new JFrame("VOLPUNTE! Baie geluk!");
          
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
             frame.setSize(600, 500);
          
             frame.setResizable(false);
          
             frame.setLocationRelativeTo(null);
          
          
             String imgStr = "PIC.jpg";
          
          
             ImageIcon image = new ImageIcon(imgStr);
          
             JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
          
             frame.getContentPane().add(label1);
          
          
          
             frame.validate();
          }
          
           public void Timer()
          {
             
             ActionListener actionListener = 
                 new ActionListener()
                {
                    public void actionPerformed(ActionEvent actionEvent)
                   {
                      showImage();
                   }
                }
                ; Timer timer = new Timer(500, actionListener);
             timer.start();
          }
          
          
           Volpunte()
          {
             Timer();
          }
       	
       	
       }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your timer is repeatedly calling the showImage() method every 500 ms so I imagine that you're displaying a bunch of JFrames with images in them, I don't see anywhere where you make the image disappear, and I don't think this is what you want. I recommend that you display your image in your main program, then create a Timer has as its time delay the amount of time you wish to display the image and that is set to not repeat (the Timer tutorial and API will help you to see how to do this). In the timer ActionListener's actionPerformed method you'll want to have your code that will make the image disappear. Then after you have displayed your image call start() on the timer and in xxx ms (whatever the timer delay has been set to), it will call your methods to make the image go away. Please look at the examples in the tutorial as they will help.

    Luck
    Last edited by Fubarable; 11-06-2010 at 05:47 PM.

  10. #10
    Samari413 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Thank you =)

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 06:48 AM
  2. Trouble Displaying Image
    By LankanSniper in forum Java 2D
    Replies: 3
    Last Post: 12-10-2009, 10:26 AM
  3. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 07:01 AM
  4. Displaying an image in a group
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:01 PM
  5. Displaying different image types
    By splinter64uk in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 09:12 AM

Posting Permissions

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