Results 1 to 15 of 15
  1. #1
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default thumbnail display timing

    I'm displaying a window containing jpg thumbnails. I'm using getScaledInstance() to do this. Everything is working fine except that all the thumbnails appear at once; I would like to have the thumbnails appear as they are created. I'm a newbie so I'm not super familiar with buffering and threads. Thanks.

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

    Default

    Can you explain how you are adding the images to the GUI?
    If you want the next image added after a time delay you could use a Swing Timer to call a method every so often to add the image.

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

    Default

    It sounds like you want to use a SwingWorker to allow you to create your images in a background thread and then display them on the event thread. If so, then you'll want to check out this tutorial: Lesson: Concurrency in Swing

  4. #4
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the response. I'm creating a JFrame, and then using getContentPane() to get the frame's content pane. I then set up a flow layout in the content pane. Then, for each image in the folder I create a button, add the button to the pane, create an ImageIcon from the jpg, get the Image reference from the ImageIcon, create a thumbnail from the Image reference using getScaledInstance(), attach the thumbnail to the ImageIcon, attach the ImageIcon to the button, and attach a listener to the button.

    The problem is that no thumbnails appear in the window until all the thumbnails are created (i.e. all the thumbnails appear at once). So for a folder with a large number of jpgs it could be a long time before anything appears in the window. It would be nice if each thumbnail appeared in the window as it was created. There is something going on the background which I don't understand which is causing this to happen.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,260
    Rep Power
    25

    Default

    It could be because you are using the GUI's thread and it won't change the display until it gets control back.
    You may want to look at post#3 about moving your code off of the GUI's thread.
    Perhaps if you post your code so we could see what you are doing.

  6. #6
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    frame = new JFrame("Photo Manager");
    contentPane = (JPanel)frame.getContentPane();
    contentPane.setLayout(new FlowLayout());

    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_O NLY);
    fc.showOpenDialog(frame);
    File f = fc.getSelectedFile();
    String[] fileList = f.list();
    for (String s : fileList)
    {
    int i = s.lastIndexOf('.', s.length() - 1);
    if (i == -1)
    break;
    String extension = s.substring(i);
    if (extension.equalsIgnoreCase(".jpg"))
    {
    JButton button = new JButton();
    contentPane.add(button);
    s = f.getAbsolutePath() + "/" + s;
    button.setActionCommand(s);
    ImageIcon ii = new ImageIcon(s);
    Image im = ii.getImage();
    Image im2 = im.getScaledInstance(180, -1, Image.SCALE_FAST);
    ii.setImage(im2);
    button.setIcon(ii);
    button.addActionListener(new photoButtonListener());
    }
    }

    I can also create a thumbnail using the drawImage() method of the Graphics class.

    I would also like to replace the call to JFileChooser with something that will display a folder "tree" and notify the app when the user clicks on a folder. I'm not sure if there's a class for that in Java.

    I have a feeling I'm going to learn a lot about Java from doing this project.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Fubarable has already given you a link to a tutorial. Did you take the time to go through it?

    db

  8. #8
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    I have looked briefly at it; I will go through it more carefully tomorrow. Thanks.

  9. #9
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    I was able to get to do what I want by using a SwingWorker for each thumbnail. Thanks everyone for your help.

    There does not appear to be a way to turn off Swing buffering.

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

    Default

    Quote Originally Posted by gatensbury View Post
    I was able to get to do what I want by using a SwingWorker for each thumbnail. Thanks everyone for your help.
    Great!

    There does not appear to be a way to turn off Swing buffering.
    What do you mean by this? And how is whatever it is affecting you?

  11. #11
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    In an event handler I had code to place a bunch of buttons in a window, and then create and attach a thumbnail to each button. None of the buttons appear in the window until the event handler is finished. If there are a large number of buttons (thumbnails), nothing will appear in the window for a long time. This apparently happens because Swing doesn't directly write objects to the screen - it writes them to one or more buffers first, and then transfers the entire buffer to the screen at a later time. There appears to be no way to get Swing to write objects directly to the screen (i.e. there always has to be at least one buffer).

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

    Default

    None of the buttons appear in the window until the event handler is finished
    If you are using the GUI's thread in the event handler, then until you finish using the GUI's thread, the GUI can not do anything to display what you are adding to the GUI.
    You should create your own thread as soon as possible and exit from the GUI's thread so that it can update the display.

    Can you write a SSCCE that demonstrates this?

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

    Default

    Quote Originally Posted by gatensbury View Post
    In an event handler I had code to place a bunch of buttons in a window, and then create and attach a thumbnail to each button. None of the buttons appear in the window until the event handler is finished. If there are a large number of buttons (thumbnails), nothing will appear in the window for a long time. This apparently happens because Swing doesn't directly write objects to the screen - it writes them to one or more buffers first, and then transfers the entire buffer to the screen at a later time. There appears to be no way to get Swing to write objects directly to the screen (i.e. there always has to be at least one buffer).
    This has nothing to do with Swing "buffering" at all. As we've told you all along, you must do certain things in background threads. I second Norm's request for an SSCCE.

  14. #14
    gatensbury is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Ya, I think I'll use a different language, a******.
    Last edited by Fubarable; 07-30-2011 at 12:45 AM. Reason: obscenity removed

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

    Default

    Quote Originally Posted by gatensbury View Post
    Ya, I think I'll use a different language, a******.
    Where in any of our posts do you see any personal attacks on you? What in this thread do you see that warrants your attack on me/us?

    And please refrain from use of obscenities, or you will be banned.

Similar Threads

  1. Chat GUI timing problem
    By waysgoose in forum Networking
    Replies: 2
    Last Post: 09-08-2010, 11:28 PM
  2. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  3. URLConnection not timing out as expected
    By StephenS in forum Networking
    Replies: 2
    Last Post: 04-02-2010, 03:10 AM
  4. Replies: 3
    Last Post: 11-10-2009, 10:24 PM
  5. Timing array loop performance
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:43 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
  •