Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #649

Flickering JList ListCell

From "GBachhaus" <gbachhaus@THRWHITE.remove-dii-this>
Subject Flickering JList ListCell
Message-ID <1168626246.214114.8890@m58g2000cwm.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
Date 2011-04-27 15:28 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Hi all,

maybe someone has an idea about this one, I'm out.
I'm working on an app with a JList containing images.
On Macs (both Intel and PPC, Java 1.5) the list is continuously
repainted (even when the app is hidden) causing the contents to appear
a flickering mess.
The same program works fine under Windows & Linux; the visible cells
are each rendered once and done.

I've pruned the program down to this 'ere: 10 images named "0.png" up
to "9.png" are loaded from a folder "images/" and displayed in a JList.
The list flickers on a Mac, and not on a Win/Linux-PC.

Any ideas ...??

Gundolf

//////////////

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

public class Main
  {
  public static void main(String[] args)
    {
    // ---- Create Vector of Images on disk
    String ImageDir = "images" + File.separator;
    Vector<Image> data = new Vector<Image>();
    for (int i = 0; i < 10; i++)
      {
      String fileName = new String(ImageDir + i + ".png");
      Image image = Toolkit.getDefaultToolkit().getImage(fileName);
      data.add(image);
      }

    // -- Build up JFrame
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle("Flickering JList MacTiculous Test V0.5");
    frame.getContentPane().setLayout(new BorderLayout());

    // -- JList in a scrollpane
    JList list = new JList(data);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setCellRenderer(new CustomListCellRenderer());
    list.setFixedCellWidth(320);
    list.setFixedCellHeight(200);
    JScrollPane scrollPane = new JScrollPane(list);

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(320, 4 * 200));
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    // -- Show it all
    frame.pack();
    frame.setVisible(true);
    }

  //-----------------------------------------------------------------
  //- Custom ListCellRenderer ---------------------------------------
  //-----------------------------------------------------------------

  private static final class CustomListCellRenderer extends Canvas
implements ListCellRenderer
    {
    private MediaTracker mediaTracker  = new MediaTracker(this);

    private Image image;

    private boolean selected;

    public Component getListCellRendererComponent(JList list, Object
value, //
        int index, // cell index
        boolean iss, // is the cell selected
        boolean chf) // the list and the cell have the focus
      {
      System.out.println("Getting image " + value);
      image = (Image) value;
      selected = iss;
      return this;
      }

    public void paint(Graphics g)
      {
      if (image == null) return;
      System.out.println("Painting image " + image);

      // -- Create a scaled image of the desired original
      int width = super.getWidth();
      int height = super.getHeight();
      Image scaledImage = image.getScaledInstance(width - 1, height -
1, Image.SCALE_DEFAULT);
      if (scaledImage == null) return;

      // -- Get our MediaTracker to load it
      mediaTracker.addImage(scaledImage, 0);
      try
        {
        mediaTracker.waitForID(0);
        }
      catch (InterruptedException e)
        {
        e.printStackTrace();
        }

      // -- Draw scaled image, flush it
      g.drawImage(scaledImage, 0, 0, null);
      mediaTracker.removeImage(scaledImage);
      scaledImage.flush();

      // -- Draw border around image
      g.setColor(selected ? Color.BLACK : Color.LIGHT_GRAY);
      g.drawRect(0, 0, width - 1, height - 1);
      }
    }

  }

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar


Thread

Flickering JList ListCell "GBachhaus" <gbachhaus@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: Flickering JList List "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: Flickering JList List "steve" <steve@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
      Re: Flickering JList List "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: Flickering JList List "steve" <steve@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: Flickering JList List "GBachhaus" <gbachhaus@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
    Re: Flickering JList List "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000

csiph-web