Solved

JEditorPane in JList items does not display animated Gifs

Posted on 2004-08-11
10
972 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

11 Experts available now in Live!

Get 1:1 Help Now