Solved

JList Rendering in a JApplet

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

726 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