Solved

JList Rendering in a JApplet

Posted on 2004-09-08
5
386 Views
Last Modified: 2013-11-23
Hi there, I seem to be having a problem with my JList Cell Rendering.  When I populate my JList, it works great initially, but if I hit refresh and reload the JApplet, the list sometimes (1 out of 3 times) comes up totally blank.  I am loading images into the JList, here is my rendering.  Thanks.

class ListEntryCellRenderer extends JLabel implements ListCellRenderer
{
   private JLabel label;

   public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
   {
      ListEntry entry = (ListEntry) value;
      setText("  " + value.toString());
      entry.getIcon().setImageObserver(list);
      setIcon(entry.getIcon());

      if (isSelected)
      {
         setBackground(list.getSelectionBackground());
         setForeground(list.getSelectionForeground());
      }
      else
      {
         setBackground(list.getBackground());
         setForeground(list.getForeground());
      }

      setEnabled(list.isEnabled());
      setFont(list.getFont());
      setOpaque(true);
      setBorder(new EtchedBorder(EtchedBorder.RAISED));

      return this;
   }
}
0
Comment
Question by:kesea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12012064
The problem is more likely with your applet not reloading correctly.
Can you post your applet code (if its not too long).
0
 

Author Comment

by:kesea
ID: 12012628
The code is 2720 lines of code.  :s  What are the stages for when you hit refresh?  I have a socket connection to a server and I have a thread processing the socket messages.  I will post the pertanent parts.  Thanks.

  protected DataInputStream dataInput;
  protected DataOutputStream dataOutput;

  public void run ()
  {
    try
    {
      String host = getParameter ("host");
      if (host == null)
         host = getCodeBase ().getHost ();
      String port = "8080";

      Socket s = new Socket (host, Integer.parseInt (port));
      dataInput = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
      dataOutput = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
      execute();
    }
    catch (IOException ex)
    {
      ByteArrayOutputStream out = new ByteArrayOutputStream ();
      ex.printStackTrace (new PrintStream (out));
    }
  }

 public boolean handleEvent (Event e)
  {
    if ((e.target == input) && (e.id == Event.ACTION_EVENT))
    {
       writeMessage((String) e.arg);
       input.setText ("");
      return true;
    }
    else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY))
    {
      if (listener != null)
      {
         listener.stop();
      }
      hide ();
      return true;
    }
    return super.handleEvent (e);
  }

  public void start ()
  {
    listener = new Thread (this);
    listener.start ();
  }

  public void stop ()
  {
    if (dataOutput !=null)
    {
      try
      {
         dataOutput.close();
      }
      catch (IOException ex)
      {
      }
    }
    if (listener != null)
    {
       listener.stop();
    }
    listener = null;
  }

  public void execute ()
  {
    try
    {
      while (true)
      {
         String received_ac = dataInput.readUTF ();
         handleReceivedMessage(received_ac);
      }
    }
    catch (IOException ex)
    {
      ByteArrayOutputStream out = new ByteArrayOutputStream ();
      ex.printStackTrace (new PrintStream (out));
    }
    finally
    {
      listener = null;
      input.hide ();
      validate ();
      try
      {
        dataOutput.close ();
      }
      catch (IOException ex)
      {
        ex.printStackTrace ();
      }
    }
  }
0
 
LVL 92

Expert Comment

by:objects
ID: 12012657
When a refresh is done, your applet is restarted.

One thing to be careful of is updating your gui from a thread other than the event dispatch thread, Swing is single threaded and accessing it from a seperate thread can cause indeterminate behaviour.

can you create a simple applet that reproduces the problem?
0
 

Author Comment

by:kesea
ID: 12013090
How do you update from the event dispatch thread?  The other thing is do I have to do anything with a JList when I stop the applet, or will the JList clean itself up?  
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12013135
> How do you update from the event dispatch thread?  

Use EventQueue.invokeLater() and invokeAndWait() methods

> The other thing is do I have to do anything with a JList when I stop the applet

no
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question