?
Solved

JList Rendering in a JApplet

Posted on 2004-09-08
5
Medium Priority
?
403 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
  • 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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