Solved

JList Rendering in a JApplet

Posted on 2004-09-08
5
373 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 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now