Accueil > > > HTML BROWSER AVEC LES FONCTINO DE BASES DE JAVA
HTML BROWSER AVEC LES FONCTINO DE BASES DE JAVA
Information sur la source
Description
C est un ptt browser html qui permet de voir la plus part des pages. Exemple de simpliciter ;o) et d utilisation de JEditorPane evec les class java par defaut Personnelement j utilise une class derivée de celle ci pour afficher mes aides dans mes applications... ;o); Elle est tre facil a adapter et surtou reagie bcp plus vite qu un appele au browser par defaut de Windo.. j ai aussi fait le Main d exemple d utilisation avec une page Htlm en argument... bonne prog...
Source
- import java.awt.*;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- import javax.swing.*;
- import javax.swing.event.HyperlinkEvent;
- import javax.swing.event.HyperlinkListener;
- import javax.swing.text.html.HTMLDocument;
- import javax.swing.text.html.HTMLFrameHyperlinkEvent;
- /**
- * @author GodConan
- *
- */
- public class HtmlBrowser extends JFrame
- {
- JEditorPane html;
- String path = "index.html";
- Vector oldPages = new Vector(); // sauve les path des page deja passer
- int currentPageIdx = 0;
-
- public HtmlBrowser() throws HeadlessException
- {
- super();
- setSize( 500, 500 );
- JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
- JPanel2.add(JToolBar1);
- JToolBar1.add(btnPrev);
- JToolBar1.add(btnNext);
- btnPrev.setText("Page precédente");
- btnNext.setText("Page suivante");
-
- Actions aIcareAction = new Actions();
- btnPrev.addActionListener( aIcareAction );
- btnNext.addActionListener( aIcareAction );
- btnNext.setFocusPainted( false );
- btnPrev.setFocusPainted( false );
- btnNext.setEnabled( false );
- btnPrev.setEnabled( false );
-
- try
- {
- URL url = getURL( path );
- html = new JEditorPane( url );
- html.setEditable(false);
- html.addHyperlinkListener( createHyperLinkListener() );
- html.setBounds( 0, 0, 500, 500 );
- JScrollPane scroller = new JScrollPane();
- JViewport vp = scroller.getViewport();
- vp.add( html );
- getContentPane().add( JPanel2, BorderLayout.NORTH );
- getContentPane().add( scroller, BorderLayout.CENTER);
- html.setBackground( this.getBackground() );
- }
- catch (Exception e)
- {
- System.out.println("eror : " + e );
- }
- }
- JPanel JPanel2 = new JPanel();
- JToolBar JToolBar1 = new JToolBar();
- JButton btnPrev = new JButton();
- JButton btnNext = new JButton();
-
- class Actions implements java.awt.event.ActionListener
- {
- public void actionPerformed(java.awt.event.ActionEvent event)
- {
- Object object = event.getSource();
- if (object == btnNext )
- btnNext_actionPerformed(event);
- else if (object == btnPrev)
- btnPrev_actionPerformed(event);
- }
- }
-
- private void btnNext_actionPerformed(java.awt.event.ActionEvent event)
- {
- try {
- if( currentPageIdx++ < oldPages.size() )
- {
- String p = (String)oldPages.elementAt( currentPageIdx );
- URL url = getURL( p );
- html.setPage( url );
- }
- checkBtn();
- } catch (Exception e)
- {
- System.out.println( " btnNext_actionPerformed : " + e );
- }
- }
-
- private void btnPrev_actionPerformed(java.awt.event.ActionEvent event)
- {
- try {
- if( currentPageIdx > 0 && !oldPages.isEmpty() )
- {
- String p = (String)oldPages.elementAt( --currentPageIdx );
- URL url = getURL( p );
- html.setPage( url );
- }
- checkBtn();
- } catch (Exception e)
- {
- System.out.println( " btnNext_actionPerformed : " + e );
- }
- }
-
- private void initTitre( String p )
- {
- String sep = "?";
- if ( p.indexOf( "/" ) > 0 ) sep = "/";
- else if ( p.indexOf( "\\" ) > 0 ) sep = "\\";
- StringTokenizer st = new StringTokenizer( p, sep );
- int n = st.countTokens();
- String s = "";
- while( st.hasMoreTokens() ) { s = st.nextToken(); }
- if ( s.indexOf(".") > 0 ) s = s.substring( 0, s.indexOf( "." ) );
- this.setTitle( "Page : " + s );
- }
-
- public HtmlBrowser( String titre ) throws HeadlessException
- {
- this();
- this.setTitle( titre );
- }
-
- private void checkBtn()
- {
- if ( oldPages == null || oldPages.size() == 0 )
- {
- btnNext.setEnabled( false );
- btnPrev.setEnabled( false );
- }
- else
- {
- if ( currentPageIdx > 0 )
- btnPrev.setEnabled( true );
- else
- btnPrev.setEnabled( false );
- if ( (currentPageIdx+1) < oldPages.size() )
- btnNext.setEnabled( true );
- else
- btnNext.setEnabled( false );
- }
- }
-
- private HyperlinkListener createHyperLinkListener()
- {
- return new HyperlinkListener()
- {
- public void hyperlinkUpdate(HyperlinkEvent e)
- {
- if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
- {
- if (e instanceof HTMLFrameHyperlinkEvent)
- {
- ((HTMLDocument) html
- .getDocument())
- .processHTMLFrameHyperlinkEvent(
- (HTMLFrameHyperlinkEvent) e);
- }
- else
- {
- try
- {
- path = e.getURL().getPath();
- html.setPage( e.getURL() );
- addHisto( path );
- initTitre( path );
- }
- catch (IOException ioe)
- {
- System.out.println("IOE: " + ioe);
- }
- }
- }
- }
- };
- }
-
- private void addHisto( String path )
- {
- oldPages.add( path );
- currentPageIdx = oldPages.size() - 1;
- if ( oldPages.size() > 25 )
- {
- oldPages = new Vector();
- oldPages.add( path );
- currentPageIdx = oldPages.size() - 1;
- }
- checkBtn();
- }
-
- /**
- * Returns le path de la page en cour de lecture.
- * @return String
- */
- public String getPath()
- {
- return path;
- }
-
- /**
- * Sets le path de la page html a lire.
- * @param path de la page html
- */
- public void setPath(String path)
- {
- try
- {
- this.path = path;
- URL url = getURL( path );
- html.setPage( url );
- addHisto( path );
- }
- catch (Exception e)
- {
- System.out.println("setPath : " + e );
- }
-
- }
-
- public void setDocument( String doc )
- {
- html.setText( doc );
- }
-
- public URL getURL( String file ) throws MalformedURLException
- {
- URL documentBase = new URL("file:///" + System.getProperty("user.dir") + "/");
- return new URL( documentBase, file );
- }
-
- public static void main(String[] args)
- {
- HtmlBrowser b = new HtmlBrowser();
- if ( args.length > 0 ) b.setPath( args[ 0 ] );
- b.setLocation( new Point( 0, 0 ) );
- b.setVisible( true );
- }
-
- }
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
/**
* @author GodConan
*
*/
public class HtmlBrowser extends JFrame
{
JEditorPane html;
String path = "index.html";
Vector oldPages = new Vector(); // sauve les path des page deja passer
int currentPageIdx = 0;
public HtmlBrowser() throws HeadlessException
{
super();
setSize( 500, 500 );
JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel2.add(JToolBar1);
JToolBar1.add(btnPrev);
JToolBar1.add(btnNext);
btnPrev.setText("Page precédente");
btnNext.setText("Page suivante");
Actions aIcareAction = new Actions();
btnPrev.addActionListener( aIcareAction );
btnNext.addActionListener( aIcareAction );
btnNext.setFocusPainted( false );
btnPrev.setFocusPainted( false );
btnNext.setEnabled( false );
btnPrev.setEnabled( false );
try
{
URL url = getURL( path );
html = new JEditorPane( url );
html.setEditable(false);
html.addHyperlinkListener( createHyperLinkListener() );
html.setBounds( 0, 0, 500, 500 );
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add( html );
getContentPane().add( JPanel2, BorderLayout.NORTH );
getContentPane().add( scroller, BorderLayout.CENTER);
html.setBackground( this.getBackground() );
}
catch (Exception e)
{
System.out.println("eror : " + e );
}
}
JPanel JPanel2 = new JPanel();
JToolBar JToolBar1 = new JToolBar();
JButton btnPrev = new JButton();
JButton btnNext = new JButton();
class Actions implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == btnNext )
btnNext_actionPerformed(event);
else if (object == btnPrev)
btnPrev_actionPerformed(event);
}
}
private void btnNext_actionPerformed(java.awt.event.ActionEvent event)
{
try {
if( currentPageIdx++ < oldPages.size() )
{
String p = (String)oldPages.elementAt( currentPageIdx );
URL url = getURL( p );
html.setPage( url );
}
checkBtn();
} catch (Exception e)
{
System.out.println( " btnNext_actionPerformed : " + e );
}
}
private void btnPrev_actionPerformed(java.awt.event.ActionEvent event)
{
try {
if( currentPageIdx > 0 && !oldPages.isEmpty() )
{
String p = (String)oldPages.elementAt( --currentPageIdx );
URL url = getURL( p );
html.setPage( url );
}
checkBtn();
} catch (Exception e)
{
System.out.println( " btnNext_actionPerformed : " + e );
}
}
private void initTitre( String p )
{
String sep = "?";
if ( p.indexOf( "/" ) > 0 ) sep = "/";
else if ( p.indexOf( "\\" ) > 0 ) sep = "\\";
StringTokenizer st = new StringTokenizer( p, sep );
int n = st.countTokens();
String s = "";
while( st.hasMoreTokens() ) { s = st.nextToken(); }
if ( s.indexOf(".") > 0 ) s = s.substring( 0, s.indexOf( "." ) );
this.setTitle( "Page : " + s );
}
public HtmlBrowser( String titre ) throws HeadlessException
{
this();
this.setTitle( titre );
}
private void checkBtn()
{
if ( oldPages == null || oldPages.size() == 0 )
{
btnNext.setEnabled( false );
btnPrev.setEnabled( false );
}
else
{
if ( currentPageIdx > 0 )
btnPrev.setEnabled( true );
else
btnPrev.setEnabled( false );
if ( (currentPageIdx+1) < oldPages.size() )
btnNext.setEnabled( true );
else
btnNext.setEnabled( false );
}
}
private HyperlinkListener createHyperLinkListener()
{
return new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
{
if (e instanceof HTMLFrameHyperlinkEvent)
{
((HTMLDocument) html
.getDocument())
.processHTMLFrameHyperlinkEvent(
(HTMLFrameHyperlinkEvent) e);
}
else
{
try
{
path = e.getURL().getPath();
html.setPage( e.getURL() );
addHisto( path );
initTitre( path );
}
catch (IOException ioe)
{
System.out.println("IOE: " + ioe);
}
}
}
}
};
}
private void addHisto( String path )
{
oldPages.add( path );
currentPageIdx = oldPages.size() - 1;
if ( oldPages.size() > 25 )
{
oldPages = new Vector();
oldPages.add( path );
currentPageIdx = oldPages.size() - 1;
}
checkBtn();
}
/**
* Returns le path de la page en cour de lecture.
* @return String
*/
public String getPath()
{
return path;
}
/**
* Sets le path de la page html a lire.
* @param path de la page html
*/
public void setPath(String path)
{
try
{
this.path = path;
URL url = getURL( path );
html.setPage( url );
addHisto( path );
}
catch (Exception e)
{
System.out.println("setPath : " + e );
}
}
public void setDocument( String doc )
{
html.setText( doc );
}
public URL getURL( String file ) throws MalformedURLException
{
URL documentBase = new URL("file:///" + System.getProperty("user.dir") + "/");
return new URL( documentBase, file );
}
public static void main(String[] args)
{
HtmlBrowser b = new HtmlBrowser();
if ( args.length > 0 ) b.setPath( args[ 0 ] );
b.setLocation( new Point( 0, 0 ) );
b.setVisible( true );
}
}
Conclusion
bon c vrai que chui pas un fou des commentaires mais je suis tout 'ouï' ;o) ... et puis ce pett code reste trés lisible... ;o)
Fichier Zip
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|