We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Preventing Text Wrapping does not work in JEditorPane !!

thockit
thockit asked
on
Medium Priority
367 Views
Last Modified: 2008-01-16
Hi,

I have an application where I have a number of hyperlinks displayed on a JEditorPane. The html data is manipulated at runtime i.e. when the user clicks on a hyperlink, the data that corresponds to that link, is displayed under the appropriate link.

I am using a subclassed JEditorPane which is called NonWrappingEditorPane. Inside this class, I have overridden the method getScrollableTracksViewportWidth() to return false, whenever the view is bigger than the viewport, so that I can get the Horizontal Scrollbar to appear.

However, I find that when I click a link who's data makes the view bigger than the current viewport, the Horizontal Scrollbar appears, and the view is appropriately sized, but the HTML still continues to wrap around, within the viewport.


Below is a sample source code that you can use to replicate the problem. Please click on the 2nd hyperlink that is displayed in the panel, since this is just sample code and the first one will not work.

HyperlinkPanel.java
----------------------

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import java.io.*;

import java.awt.Component;
import javax.swing.plaf.ComponentUI;

public class HyperlinkPanel extends JFrame {

     String headerFileName = "/temp/header.html";
     String linksFileName = "/temp/links.html";
     String footerFileName = "/temp/footer.html";
     
     NonWrappingEditorPane linksPanel = new NonWrappingEditorPane();
     JScrollPane scrollPane = new JScrollPane();
     
     BorderLayout layout = new BorderLayout();
     
     String header = "";
     String footer = "";
     
     public HyperlinkPanel(){
          super("Links Panel");
         
          getContentPane().setLayout(layout);
         
          linksPanel.setContentType("text/html");
          linksPanel.setEditable(false);
          linksPanel.setResizable(false);
         
          scrollPane.getViewport().add(linksPanel);
          getContentPane().add(scrollPane, BorderLayout.CENTER);
         
          SimpleLinkListener linksListener = new SimpleLinkListener(linksPanel);
          linksPanel.addHyperlinkListener(linksListener);
         
          addWindowListener(new WindowAdapter(){
               public void windowClosing(WindowEvent e){
                    ((JFrame)e.getSource()).dispose();
               }
          });
         
          try {
               header = readXMLFile(headerFileName);
               String links  = readXMLFile(linksFileName);
               footer = readXMLFile(footerFileName);
               
               linksPanel.setText(header + links + footer);
          }
          catch (Exception e){
               e.printStackTrace(System.out);
          }
     }
     
     public String readXMLFile(String fileName) throws Exception {
         
          File file = new File(fileName);
          BufferedReader reader = new BufferedReader(new FileReader(file));
          StringBuffer buffer = new StringBuffer();
          String line = null;
          while ((line = reader.readLine()) != null)
               buffer.append(line);
          return buffer.toString();
     }
     
     public static void main(String args[]){
         
          HyperlinkPanel panel = new HyperlinkPanel();
         
          panel.setLocation(300, 300);
          panel.setSize(400, 300);
          panel.setVisible(true);
     }
     
     class SimpleLinkListener implements HyperlinkListener {
     
       private NonWrappingEditorPane pane; // The pane we're using to display HTML
     
       private JTextField  urlField;   // An optional textfield for showing
                                       // the current URL being displayed
     
       private JScrollPane scrollPane;
       private JFrame       parent;
       
       private JLabel actionDetails;   // An action label for showing the
                                       // information on the action.
     
       private JPanel toolTipPanel;
       
       private String nodeType = null;
       private String nodeID       = null;
       
       private boolean[] openLinksList = new boolean[2000];
       
       public SimpleLinkListener(NonWrappingEditorPane jep) {
         pane = jep;
       }
       
       public void setCurrentNodeId(String id){
            nodeID = id;
       }
     
       public int getCurrentNodeId() {
            return Integer.parseInt(nodeID);
       }
           
       public void setCurrentNodeType(String type){
            nodeType = type;
       }
       
       public void resetOpenLinksList(){
          openLinksList = new boolean[2000];
       }
     
       public void hyperlinkUpdate(HyperlinkEvent he) {
         
            // System.out.print("Hyperlink event started...");
     
         HyperlinkEvent.EventType type = he.getEventType();
         // Ok.  Decide which event we got...
         if (type == HyperlinkEvent.EventType.ENTERED) {
           // Enter event.  Go the the "hand" cursor and fill in the status bar
           // System.out.println("entered");
           pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
         }
         else if (type == HyperlinkEvent.EventType.EXITED) {
           // Exit event. Go back to the default cursor and clear the status bar
           // System.out.println("exited");
           pane.setCursor(Cursor.getDefaultCursor());
         }
         else {
               try {
                      String description = he.getDescription();          
                    int linkId = 0;
                   
                    try {
                         linkId = new Integer(description.substring(0, 2)).intValue();
                    }
                    catch (Exception e){
                         linkId = new Integer(description.substring(0, 1)).intValue();
                    }              
                   
                    String links = readXMLFile("/temp/links" + linkId + ".html");
                    pane.setText(header + links + footer);
               }
               catch (Exception e) {
                         e.printStackTrace();
               }
          }
       }
     }
     
     class NonWrappingEditorPane extends JEditorPane {
     
          public NonWrappingEditorPane(){
               super();
          }
         
          public boolean getScrollableTracksViewportWidth(){
         
               Component parent = getParent();
               ComponentUI ui = getUI();
               
               int width = ui.getPreferredSize(this).width;
               int parentWidth = parent.getWidth();
                   
               return parent != null ?
                     width <= parentWidth : true;
          }
     }
}


----------------------------------------------------------------------------------------------------

HTML Files (please put them in the /temp folder or change the code appropriately).
-----------------------------------------------------------------------------------------------

header.html
--------------

<html>
<font face=arial><b>[Link Set:]&nbsp&nbsp;[Set ID: 1]</b><br>
&nbsp&nbsp&nbsp&nbsp;[Description: N/A]<br>
&nbsp&nbsp&nbsp&nbsp;[Next Set: N/A]<br>
&nbsp&nbsp&nbsp&nbsp;[Links:]<br>

footer.html
-------------
<b>[End of Set]</b></font>
</html>

links.html
-----------
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="0">Link Set 2</a><br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="1">Link Set 3</a><br>

links1.html
------------
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="0">Links Set 2</a><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="1">Links Set 3</a><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;Description:&nbsp;<font color=#8B3E2F>map obtained values to the appropriate variables</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;Notes:&nbsp;<font color=#8B3E2F>null</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;Mapped Variables:<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/cust_id</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/cust_id</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/msg_type</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/incoming_msg_type</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/timestamp</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/sc_timestamp</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/cust_id</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/BAS_CUST_ID_H</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/cust_id</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/BAS_CUST_ID</font><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;&nbsp&nbsp&nbsp&nbsp;<font color=#8B3E2F>variables/ABS_0100_CCB/timestamp</font>&nbsp;=>&nbsp;<font color=#8B3E2F>variables/ABS_SC_PT_TIME_STAMP</font><br>


Note: Please click on the 2nd hyperlink to replicate the problem. The first one will not work. Also please note that I am using the latest version of the JDK (JDK 1.4.2_04).

thanks in advance
thockit
Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> when I click a link who's data makes the view bigger than the current viewport

Is it that initially the link looks the way you want it to look and on clicking, it wraps up?

Author

Commented:
Hi,

To answer your question, the HTML that pertains to the link is shown @ runtime when the user clicks on the link. If the info that pertains to the HTML link that has been clicked, extends beyond the viewport, then the scrollbars tracks re-adjust. But the new HTML that has been added to for the link that has been clicked, does not adjust to the new size of the JEditorPane.

BTW, Have you tried running the sample code that I have pasted ? If you haven't please do give that a try. It should help you understand what is happening.

thanks
thockit

Author

Commented:
The best plausible solution is to use a JPanel within which you can embed the JEditorPane. This seems to work just fine.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Well, when I compiled your code, it gave me a small compilation error. Anyway, might be due to some version difference (perhaps some method is not present in my version).

Well, yes, generally you should add it to a JPanel. That's the approach which I usually follow, but I couldn't suggest that for yours simply because I couldn't go through all your code to see if you have done it or not.

I don't mind if this question is deleted, now that you have solved it by yourself.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.