Solved

JEditorPane in JList items does not display animated Gifs

Posted on 2004-08-11
10
982 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
sql import cannot be resolved jsp 3 50
jsp error 6 27
What is the use of Forwarding Class in java 1 18
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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

821 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