thockit
asked on
Preventing Text Wrapping does not work in JEditorPane !!
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 getScrollableTracksViewpor tWidth() 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.Component UI;
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(fal se);
linksPanel.setResizable(fa lse);
scrollPane.getViewport().a dd(linksPa nel);
getContentPane().add(scrol lPane, BorderLayout.CENTER);
SimpleLinkListener linksListener = new SimpleLinkListener(linksPa nel);
linksPanel.addHyperlinkLis tener(link sListener) ;
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
((JFrame)e.getSource()).di spose();
}
});
try {
header = readXMLFile(headerFileName );
String links = readXMLFile(linksFileName) ;
footer = readXMLFile(footerFileName );
linksPanel.setText(header + links + footer);
}
catch (Exception e){
e.printStackTrace(System.o ut);
}
}
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(NonWrap pingEditor Pane 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(HyperlinkE vent he) {
// System.out.print("Hyperlin k event started...");
HyperlinkEvent.EventType type = he.getEventType();
// Ok. Decide which event we got...
if (type == HyperlinkEvent.EventType.E NTERED) {
// Enter event. Go the the "hand" cursor and fill in the status bar
// System.out.println("entere d");
pane.setCursor(Cursor.getP redefinedC ursor(Curs or.HAND_CU RSOR));
}
else if (type == HyperlinkEvent.EventType.E XITED) {
// Exit event. Go back to the default cursor and clear the status bar
// System.out.println("exited ");
pane.setCursor(Cursor.getD efaultCurs or());
}
else {
try {
String description = he.getDescription();
int linkId = 0;
try {
linkId = new Integer(description.substr ing(0, 2)).intValue();
}
catch (Exception e){
linkId = new Integer(description.substr ing(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 getScrollableTracksViewpor tWidth(){
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:]  [Set ID: 1]</b><br>
    [Desc ription: N/A]<br>
    [Next Set: N/A]<br>
    [Link s:]<br>
footer.html
-------------
<b>[End of Set]</b></font>
</html>
links.html
-----------
     & nbsp & nbsp;<a href="0">Link Set 2</a><br>
     & nbsp & nbsp;<a href="1">Link Set 3</a><br>
links1.html
------------
     & nbsp & nbsp;<a href="0">Links Set 2</a><br>   &n bsp &n bsp &n bsp;<a href="1">Links Set 3</a><br>   &n bsp &n bsp &n bsp &n bsp;Descri ption:&nbs p;<font color=#8B3E2F>map obtained values to the appropriate variables</font><br> & nbsp & nbsp & nbsp & nbsp & nbsp;Notes : <fo nt color=#8B3E2F>null</font>< br> &n bsp &n bsp &n bsp &n bsp &n bsp;Mapped Variables:<br>  &n bsp &n bsp &n bsp &n bsp & nbsp & nbsp <font color=#8B3E2F>variables/AB S_0100_CCB /cust_id</ font>  ;=> < font color=#8B3E2F>variables/cu st_id</fon t><br>&nbs p &nbs p &nbs p &nbs p &nbs p &nb sp &nb sp <f ont color=#8B3E2F>variables/AB S_0100_CCB /msg_type< /font>&nbs p;=> <font color=#8B3E2F>variables/in coming_msg _type</fon t><br>&nbs p &nbs p &nbs p &nbs p &nbs p &nb sp &nb sp <f ont color=#8B3E2F>variables/AB S_0100_CCB /timestamp </font>&nb sp;=>  ;<font color=#8B3E2F>variables/sc _timestamp </font><br > &nbs p &nbs p &nbs p &nbs p &nbs p; &nb sp &nb sp;<font color=#8B3E2F>variables/AB S_0100_CCB /cust_id</ font>  ;=> < font color=#8B3E2F>variables/BA S_CUST_ID_ H</font><b r> &nb sp &nb sp &nb sp &nb sp &nb sp; &n bsp &n bsp;<font color=#8B3E2F>variables/AB S_0100_CCB /cust_id</ font>  ;=> < font color=#8B3E2F>variables/BA S_CUST_ID< /font><br>                ; &nbs p &nbs p;<font color=#8B3E2F>variables/AB S_0100_CCB /timestamp </font>&nb sp;=>  ;<font color=#8B3E2F>variables/AB S_SC_PT_TI ME_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
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 getScrollableTracksViewpor
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.Component
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
linksPanel.setContentType(
linksPanel.setEditable(fal
linksPanel.setResizable(fa
scrollPane.getViewport().a
getContentPane().add(scrol
SimpleLinkListener linksListener = new SimpleLinkListener(linksPa
linksPanel.addHyperlinkLis
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
((JFrame)e.getSource()).di
}
});
try {
header = readXMLFile(headerFileName
String links = readXMLFile(linksFileName)
footer = readXMLFile(footerFileName
linksPanel.setText(header + links + footer);
}
catch (Exception e){
e.printStackTrace(System.o
}
}
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(NonWrap
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(HyperlinkE
// System.out.print("Hyperlin
HyperlinkEvent.EventType type = he.getEventType();
// Ok. Decide which event we got...
if (type == HyperlinkEvent.EventType.E
// Enter event. Go the the "hand" cursor and fill in the status bar
// System.out.println("entere
pane.setCursor(Cursor.getP
}
else if (type == HyperlinkEvent.EventType.E
// Exit event. Go back to the default cursor and clear the status bar
// System.out.println("exited
pane.setCursor(Cursor.getD
}
else {
try {
String description = he.getDescription();
int linkId = 0;
try {
linkId = new Integer(description.substr
}
catch (Exception e){
linkId = new Integer(description.substr
}
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 getScrollableTracksViewpor
Component parent = getParent();
ComponentUI ui = getUI();
int width = ui.getPreferredSize(this).
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:]  [Set ID: 1]</b><br>
    [Desc
    [Next
    [Link
footer.html
-------------
<b>[End of Set]</b></font>
</html>
links.html
-----------
     &
     &
links1.html
------------
     &
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
ASKER
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
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
ASKER
The best plausible solution is to use a JPanel within which you can embed the JEditorPane. This seems to work just fine.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is it that initially the link looks the way you want it to look and on clicking, it wraps up?