Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Swing - Utilisation d'un JEditorPane pour afficher une page HTML

Utilisation de cases à cocher Utilisation de composants JSplitPane



Utilisation du composant JEditorPane

Comme son nom l'indique, le composant javax.swing.JEditorPane permet de créer un éditeur riche pour votre application Java. Cet éditeur supporte le format HTML. En configurant un JEditorPane comme étant non éditable, on obtient alors un composant d'affichage de page HTML.

La méthode setPage permet de charger un contenu à partir de différentes sources.

Exemple de code

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class JEditorPaneSample extends JFrame {
    
    private static final long serialVersionUID = 8880534690644326693L;

    // --- Construction de l'interface graphique ---
    public JTabbedPaneSample() throws Exception {
        super( "JEditorPane sample" );
        this.setSize( 800, 500 );
        this.setLocationRelativeTo( null );
        this.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        
        // --- On crée le composant d'affichage JEditorPane ---
        JEditorPane helpPane = new JEditorPane();
        helpPane.setEditable( false );
        helpPane.setPage( "file:docs/index.html" );
        JScrollPane scrollPane = new JScrollPane( helpPane );
                
        // --- On récupère le contentPane et on y ajoute notre composant ---
        JPanel contentPane = (JPanel) getContentPane();
        contentPane.add( scrollPane, BorderLayout.CENTER );
    }
    
    // --- Point d'entrée du programme ---
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel( new NimbusLookAndFeel() );
        JEditorPaneSample frame = new JEditorPaneSample();
        frame.setVisible(true);
    }
}
Exemple d'utilisation d'un composant JTabbedPane


Utilisation de cases à cocher Utilisation de composants JSplitPane