wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Wrong font size in JEditorpane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wrong font size in JEditorpane" Watch "Wrong font size in JEditorpane" New topic
Author

Wrong font size in JEditorpane

Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
I am facing a problem in java swing. We have a JEditorPane for display of a HTML file. The problem is the font size in the Editor Pane is appearing in reduced size. Even if I increase the font size proportionately it is appearing in reduced size. How to get the exact font size as it appears in Internet Explorer. Can you give me some hint regarding this issue.
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
Hi,

Originally posted by Anupam Bhatt:
The problem is the font size in the Editor Pane is appearing in reduced size. Even if I increase the font size proportionately it is appearing in reduced size.


Could you post a little snippet of your code to see how you are doing things. Maybe there is something obvious that you have over-looked.

However, in my experience using the JEditorPane for displaying HTML is far from perfect. It is not as capable at rendering to the same quality as a dedicated browser like IE.

If you are expecting the same look that you get with IE, then you are in for a long uphill battle. However, I am surprised that you can't get different font sizes appearing in the editor at the same time (or did I miss understand your post?).

Cheers, Jared.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Originally posted by Jared Cope:
Hi,



Could you post a little snippet of your code to see how you are doing things. Maybe there is something obvious that you have over-looked.

However, in my experience using the JEditorPane for displaying HTML is far from perfect. It is not as capable at rendering to the same quality as a dedicated browser like IE.

If you are expecting the same look that you get with IE, then you are in for a long uphill battle. However, I am surprised that you can't get different font sizes appearing in the editor at the same time (or did I miss understand your post?).

Cheers, Jared.




Thanks Jared for replying

Here is the code>>


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Stack;

/**
*
* "DummyBrowser" is a very simple minimalist web browser, written in Java.
* It uses the JEditPane swing component to render HTML pages,
* and to follow links to other pages.

public class DummyBrowser extends JFrame implements HyperlinkListener,
ActionListener {

private JButton homeButton, backButton, quitButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String startURL;
private Stack history;


public static void main(String[] args) {
if (args.length == 0)
new DummyBrowser("Dummy.html");
else
new DummyBrowser(args[0]);
}

public DummyBrowser(String file) {
super("Dummy Browser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// setup initial file URL and history stack
startURL = this.getClass().getResource(file).toExternalForm();
history = new Stack();
history.push(startURL);

// build the GUI components for DummyBrowser
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.lightGray);
homeButton = new JButton("Home");
homeButton.addActionListener(this);
backButton = new JButton("Back");
backButton.addActionListener(this);
quitButton = new JButton("Quit");
quitButton.addActionListener(this);
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(startURL);
urlField.addActionListener(this);
topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);
topPanel.add(backButton);
topPanel.add(quitButton);
getContentPane().add(topPanel, BorderLayout.NORTH);

htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
try {
htmlPane.setPage(startURL);
} catch(IOException ioe) {
warnUser("Error rendering initial HTML page " + startURL + ": " + ioe);
}
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane, BorderLayout.CENTER);

Dimension screenSize = getToolkit().getScreenSize();
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String url = startURL;
Object source = event.getSource();
if (source == urlField) {
url = urlField.getText();
} else if (source == backButton) {
if (!history.empty()) url = (String)history.pop(); // pop current page
if (!history.empty()) url = (String)history.pop(); // pop previous page
} else if (source == quitButton) {
System.exit(0);
}
// assume Home button pressed if none of above
try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
history.push(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
}
public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
htmlPane.setPage(event.getURL());
urlField.setText(event.getURL().toExternalForm());
history.push(event.getURL().toExternalForm());
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalForm() + ": " + ioe);
}
}
}
private void warnUser(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
}
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Wrong font size in JEditorpane