1

I would like to display a webpage inside a java swing application. Similar to a when using HTML, but in java Swing. I have able to show webpage inside swing application. But the problem is webpage is not looks good. For example below output when I used the below code :

JEditorPane website = new JEditorPane();
website.setEditable(false);
website.setPage("http://www.google.com/");


JFrame frame = new JFrame("Google");
frame.add(new JScrollPane(website));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();

enter image description here

Even searching is also not working. I am new in this section. What I missed or what is other way to do this?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

2 Answers2

3

With plain Swing you're out of luck for that. However, if you'd consider switching your technology stack there might be alternatives:

For others you might want to check out Pure Java HTML viewer/renderer for use in a Scrollable pane

Community
  • 1
  • 1
Jan
  • 13,738
  • 3
  • 30
  • 55
3

try using JavaFX. it work perfectly.

   JFXPanel fxPanel;
   WebView wv;
   JFrame frame;


  try {

        fxPanel = new JFXPanel ();

        // create JavaFX scene
        com.sun.javafx.application.PlatformImpl.runLater ( new Runnable () {
            @Override
            public void run () {
                wv = new WebView ();
                wv.getEngine ().load ( "http://www.google.com/" );
                fxPanel.setScene ( new Scene ( wv, 1000, 750 ) );
                frame = new JFrame ( "Google" );
                frame.add ( new JScrollPane ( fxPanel ) );
                frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
                frame.setVisible ( true );
                frame.pack ();
            }
        } );

    } catch ( Exception ex ) {

    }
Billydan
  • 395
  • 1
  • 7
  • 25