Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Help with JWebBrowser

Help
J.R.
2009-11-26
2012-09-23
  • J.R.
    J.R.
    2009-11-26

    Hi, I'm new to java and I need some help with JWebBrowser. Here goes my code:

    Main.java

    /
    To change this template, choose Tools | Templates
    and open the template in the editor.
    /

    package navegador;

    import javax.swing.JFrame;
    /

    @author Silva
    */
    public class Main {

    /
    @param args the command line arguments
    /
    public static void main(String args) {
    NavFrame navFrame1 = new NavFrame();
    navFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    navFrame1.setSize(800,600);
    navFrame1.setVisible(true);
    // TODO code application logic here
    }

    }

    NavFrame.java

    /
    To change this template, choose Tools | Templates
    and open the template in the editor.
    /

    package navegador;

    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JPanel;

    import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
    /

    @author Silva
    */
    public class NavFrame extends JFrame implements ActionListener {
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    private JButton button2;

    private JPanel webBrowserPanel = new JPanel(new FlowLayout());
    private JWebBrowser webBrowser = new JWebBrowser();

    public NavFrame(){
    super("UFTzilla Navigator");
    setLayout(new FlowLayout());

    button2 = new JButton("<");
    button2.setToolTipText("Voltar");
    add(button2);

    button1 = new JButton(">");
    button1.setToolTipText("Avançar");
    add(button1);

    textField1 = new JTextField(35);
    textField1.addActionListener(this);
    add(textField1);

    textField2 = new JTextField(10);
    textField2.setToolTipText("Pesquisar no Google");
    textField2.addActionListener(this);
    add(textField2);

    webBrowser.navigate("http://www.google.com");
    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
    add(webBrowserPanel, BorderLayout.CENTER);
    }
    public void actionPerformed(ActionEvent event){
    String string = "";

    if(event.getSource() == textField1)
    string = String.format("%s");

    else if(event.getSource() == textField2)
    string = String.format("http://www.google.com.br/search?q=%s");

    webBrowser.navigate(string);
    }
    }

    And it's givin' this error:

    --------------------Configuration: Navegador - JDK version 1.6.0_17 <Default> - <Default>--------------------
    Exception in thread "main" java.lang.NoClassDefFoundError:
    org/eclipse/swt/events/MouseListener
    at chrriis.dj.nativeswing.swtimpl.components.JWebBrowser.<init>(JWebBrowser.ja
    va:395)
    at navegador.NavFrame.<init>(NavFrame.java:30)
    at navegador.Main.main(Main.java:19)
    Caused by: java.lang.ClassNotFoundException:
    org.eclipse.swt.events.MouseListener
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
    ... 3 more

    Process completed.

    Can someone help me please? It's a homework for my college xD.

     
  • Hi,

    Have you added the necessary libraries to your classpath (the SWT libraries)?
    Note all libraries are needed but some are mandatory. All the possible
    libraries are shipped in the distribution and can be found in the lib folder.

    Note that SWT is only shipped for Windows and you need to download the right
    version for other OSes.

    I also notice that you main method does not follow the necessary requirements
    for Native Swing to work. Have a look at any sample from the demo application,
    they all have a main method so you can copy the approach.

    -Christopher

     
  • J.R.
    J.R.
    2009-11-26

    Thanks, I made it similar to the examples and now it works, thanks.

    But I want to add new things to the GUI, like a search bar (similar to
    firefox), and other buttons. How can I add them? Can you tell me please?

    Thanks.

     
  • But I want to add new things to the GUI, like a search bar (similar to
    firefox), and other buttons.

    You have to modify the web browser decorator. Either extend the current one (I
    think this is the easiest) or define your own from scratch. The demo
    application shows how to change the decorator to add your buttons.

    -Christopher