I made an application to read html files. I use JEditorPane
.
My problem is the consumption of the memory, when I load different files, the memory only increases and never decreases:
I can not use my "application" as is, with a consumption of 1go quickly reached.
I read things from 2004 on the setPage
method, but it's old.
Have you got some idea?
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.commons.lang3.StringUtils;
public class MyRP extends JFrame {
private static final long serialVersionUID = 8649207663033200521L;
GridBagConstraints c = new GridBagConstraints();
JEditorPane editorPane = new JEditorPane();
public MyRP() {
setTitle("My RP");
// Modifier la taille
setSize(1500, 800);
// Taille non modifiable par l'utilisateur
setResizable(true);
// Un clic sur croix ferme la fenetre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Centrer la fenetre par rapport à l'écran
setLocationRelativeTo(null);
getContentPane().setLayout(new GridBagLayout());
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 0;
c.ipady = 1500;
c.ipadx = 1200;
c.fill = GridBagConstraints.VERTICAL;
c.insets = new Insets(2, 0, 2, 2);
getContentPane().add(new JScrollPane(editorPane), c);
// Build the panel of controls
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BorderLayout());
// Ajout de l'arbre hierarchique
JTree tree = new JTree(addNodes(null, new File(".")));
JScrollPane scrollPane = new JScrollPane(tree);
upperPanel.add(scrollPane, "Center"); // Add the tree
Box controlPanel = new Box(BoxLayout.Y_AXIS);
// Add a button to install the text
JButton bouton = new JButton("Ouvrir un fichier");
controlPanel.add(bouton);
upperPanel.add(controlPanel, "South");
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.ipadx = 50;
c.weighty = 1;
c.insets = new Insets(2, 10, 2, 2);
getContentPane().add(upperPanel, c);
// Add a listener to the button
bouton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser dialogue = new JFileChooser(new File("."));
File fichier = new File("");
if (dialogue.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fichier = dialogue.getSelectedFile();
}
if (fichier.toString().contains("MyRP")) {
readSelectedFile(fichier);
} else if(StringUtils.isNotBlank(fichier.toString())){
JOptionPane.showMessageDialog(new JFrame(), "Le fichier n'est pas au bon format : MyRP - Mois - Année.html");
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
String chemin = "";
DefaultMutableTreeNode node2 = node;
while (node2.getParent() != null) {
if (node2.getParent() != null) {
chemin = node2.getUserObject() + "\\" + chemin;
}
node2 = (DefaultMutableTreeNode) node2.getParent();
}
chemin = chemin.substring(0, chemin.length() - 1);
if (chemin.toString().contains("MyRP")) {
readSelectedFile(new File(System.getProperty("user.dir") + "\\" + chemin));
} else {
JOptionPane.showMessageDialog(new JFrame(), "Le fichier n'est pas au bon format : MyRP - Mois - Année.html");
}
}
});
}
private void readSelectedFile(File fichier) {
try {
//File file = new File(fichier.toString());
//editorPane.getEditorKit().createDefaultDocument();
//editorPane.setPage(file.toURI().toURL());
editorPane.setPage("file:\\" + fichier.toString());
editorPane.setContentType("text/html;charset=UTF-8");
editorPane.setEditable(false);
} catch (Exception exception) {
exception.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) {
curTop.add(curDir);
}
Vector ol = new Vector();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector files = new Vector();
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}
}