Solved

JEditorPane in JList items does not display animated Gifs

Posted on 2004-08-11
10
978 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
Weekend adv creator 3 36
diffSum example 4 36
how to install java on RHEL image on EC2 4 26
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 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

10 Experts available now in Live!

Get 1:1 Help Now