Solved

JEditorPane in JList items does not display animated Gifs

Posted on 2004-08-11
10
988 Views
Last Modified: 2011-10-03
I am storing a JEditorPane in a JList item, and then I store HTML in the JEditorPane of the JList.  Everything works fine, but if I display an animated gif in my html my animated GIFs just stop there.  How can I get my viewport to be all of the JEditorPanes, I can set it to one, but then my JList only displays one of the JEditorPanes.  

These are just pieces of my code:

EditableJList.EditableListModel listModel = new EditableJList.EditableListModel();

JScrollPane jScrollPane1 = new JScrollPane();
JList output;
jScrollPane1.getViewport().add(output, null);

     JEditorPane tc = new JEditorPane("text/html", null);
     tc.setText("<html><body>"+temp_ac+"</body></html>");
     listModel.addElement(tc);

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
  • 6
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11779776
Not a simple thing to resolve, problem is that the list renderer is only used as a rubber stamp to paint the cell.
0
 

Author Comment

by:kesea
ID: 11779790
class EditableJList extends JList
{
   public EditableJList(EditableListModel listModel)
   {
      setModel(listModel);
      addMouseListener();
      // set our custom cell renderer
      setCellRenderer(new TextComponentCellRenderer());
   }

   private void addMouseListener()
   {
      MouseListener mouseListener = new MouseAdapter()
      {
         public void mouseClicked(MouseEvent e)
         {
            if (e.getClickCount() == 2)
            {
               EditableListModel listModel = (EditableListModel) getModel();

               // determine the clicked item
               int index = locationToIndex(e.getPoint());
               Object value = listModel.getElementAt(index);
               final JTextComponent tc = (JTextComponent) value;

               // set the appropriate border for editing
               String classname = tc.getClass().getName().toString();
               classname = classname.substring(classname.lastIndexOf("."));
               tc.setBorder(UIManager.getBorder(classname + "border"));

               // make this border appear in the JList
               listModel.updateItem(index);

               tc.addFocusListener(new FocusListener()
               {
                  public void focusGained(FocusEvent fe)
                  {
                     // no selection color desired when editing a JTextComponent
                     clearSelection();
                  }

                  public void focusLost(FocusEvent fe)
                  {
                     // remove the border again when stopped editing
                     tc.setBorder(null);
                  }
               });

               // request the focus on this component to be able to edit it
               tc.requestFocus();

               // listen to all key events on the JTextComponent and update the
               // JList item every time.  Without this, you won't see the changes.
               tc.getDocument().addDocumentListener(new UpdateListDocumentListener(listModel, index));
            }
         }
      };

      addMouseListener(mouseListener);
   }

   // DocumentListener that takes care of updating a JList item
   // when editing it.  Calls updateItem on our custom EditableListModel
   private class UpdateListDocumentListener implements DocumentListener {
      private EditableListModel elm;
      private int index;

      public UpdateListDocumentListener(EditableListModel elm, int index) {
         this.elm = elm;
         this.index = index;
      }

      public void insertUpdate(DocumentEvent e) {
         elm.updateItem(index);
      }

      public void removeUpdate(DocumentEvent e) {
         elm.updateItem(index);
      }

      public void changedUpdate(DocumentEvent e) {
         elm.updateItem(index);
      }
   }

   private class TextComponentCellRenderer implements ListCellRenderer {
      public Component getListCellRendererComponent(
         JList list,
         Object value,            // value to display
         int index,               // cell index
         boolean isSelected,      // is the cell selected
         boolean cellHasFocus)    // the list and the cell have the focus
      {
         JTextComponent tc = (JTextComponent) value;
         if (isSelected) {
            tc.setBackground(list.getSelectionBackground());
            tc.setForeground(list.getSelectionForeground());
         }
         else {
            tc.setBackground(list.getBackground());
            tc.setForeground(list.getForeground());
         }

         tc.setEnabled(list.isEnabled());
         tc.setFont(list.getFont());
         tc.setBorder(null);

         return (Component) value;
      }
   }

   public static class EditableListModel extends DefaultListModel {
      public void updateItem(int index) {
         fireContentsChanged(this, index, index);
      }

      public void addElement(JTextComponent tc) {
         super.addElement(tc);
      }
   }
}

myclass
{
..
  JScrollPane jScrollPane1 = new JScrollPane();
  EditableJList.EditableListModel listModel = new EditableJList.EditableListModel();
  EditableJList output = new EditableJList(listModel);

  public void DisplayMessage (String I_Message_ac)
  {
     StringBuffer temp_ac = new StringBuffer(I_Message_ac);
     JEditorPane tc = new JEditorPane("text/html", null);
     mPostingMessage_l=true;
     tc.setText("<html><body>"+temp_ac+"</body></html>");
     listModel.addElement(tc);

}
0
 

Author Comment

by:kesea
ID: 11779812
Oops, I hit enter and it got submitted before I was ready.  Ok that last part the DisplayMessage function is how I am populating my EditableJList with html code.  The problem is that when I put animated GIFs in the HTML code (as images), they only partially load. I used to have 1 JEditorPane which all I had to do was say:

     jScrollPane1.getViewport().setView(output);

but now I have several JEditorPanes and if I set one of them to the viewport, ie.

     jScrollPane1.getViewport().add(tc)

Then the only thing my JList shows is the one JEditor with the Animated GIF moving.   Is there a simple way I can get each JEditorPane in my JList to be in the viewport, constantly updating?   Thanks experts.

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Expert Comment

by:objects
ID: 11779818
Here's an example for displaying animated icons in a table cell:

http://www.objects.com.au/java/examples/swing/AnimatedTableCell.do

Concept would be similiar for a JList, though the JEditorPane could add additional complexity.
0
 

Author Comment

by:kesea
ID: 11780011
The problem is I don't have icons.  What I have are HTML links to icons, and they are embedded HTML.  So there is no way to override the JScrollPane to display HTML?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11780035
> The problem is I don't have icons.

Shouldn't make a difference

> So there is no way to override the JScrollPane to display HTML?

scroll pane has nothing to do with it.
the problem is with how renderers works.
0
 

Author Comment

by:kesea
ID: 11780208
Hmmm, ok I will give it a try and get back to you.
0
 

Author Comment

by:kesea
ID: 11780255
Ok I tried that code, but it didn't refresh the icons, it displayed them, but they didn't move.  Strange....
0
 

Author Comment

by:kesea
ID: 11780494
Ok I have discovered some things, if I add a repaint() call into my List Cell Rendering, everything works, but as soon as I minimize explorer and then go back into it, everything but the scrollpane is all screwed up with whatever was behind it.  Is there a way I can repaint the whole applet from this ListCellRenderer?  ie. getting the parent of the JList?  How can I get the applet to repaint it?  Thanks.

   private class TextComponentCellRenderer implements ListCellRenderer {
      public Component getListCellRendererComponent(
         JList list,
         Object value,            // value to display
         int index,               // cell index
         boolean isSelected,      // is the cell selected
         boolean cellHasFocus)    // the list and the cell have the focus
      {
         JTextComponent tc = (JTextComponent) value;
         if (isSelected) {
            tc.setBackground(list.getSelectionBackground());
            tc.setForeground(list.getSelectionForeground());
         }
         else {
            tc.setBackground(list.getBackground());
            tc.setForeground(list.getForeground());
         }

         tc.setEnabled(list.isEnabled());
         tc.setFont(list.getFont());
         tc.setBorder(null);

         repaint(); //Repaint it all.   <---------------------------- I added this line.
         

         return (Component) value;
      }
   }
0
 
LVL 92

Expert Comment

by:objects
ID: 11780741
>         repaint(); //Repaint it all.   <---------------------------- I added this line.

Is that an inner class? O/wise I don't see how that would compile.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

626 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