Solved

Preventing Text Wrapping does not work in JEditorPane !!

Posted on 2004-04-05
6
326 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
0
Comment
Question by:thockit
  • 2
  • 2
6 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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?
0
 

Author Comment

by:thockit
Comment Utility
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
0
 

Author Comment

by:thockit
Comment Utility
The best plausible solution is to use a JPanel within which you can embed the JEditorPane. This seems to work just fine.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
Comment Utility
PAQed, with points refunded (400)

ee_ai_construct - (re-order part number #xm34)
Community Support Moderator
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

8 Experts available now in Live!

Get 1:1 Help Now