Solved

JList Rendering in a JApplet

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 50
session migration servlets 2 24
Way to decrease size of apk file 9 42
use lov values 2 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

15 Experts available now in Live!

Get 1:1 Help Now