Solved

JEditorPane in JList items does not display animated Gifs

Posted on 2004-08-11
10
987 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 76
ejb wildfly example 2 113
What's wrong with this code? 4 46
spring maven example issues 3 108
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses

751 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