[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Preventing Text Wrapping does not work in JEditorPane !!

Posted on 2004-04-05
6
Medium Priority
?
340 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
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10755620
>> 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
ID: 10755875
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
ID: 10771847
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:Mayank S
ID: 10771871
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
ID: 10988416
PAQed, with points refunded (400)

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

829 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