Reputation: 23
I'm trying to create a web browser using swing and awt only. I have used actionlistener and actionevent too. But it is not working. Please help me with this. It runs without any error but doesn't load the web page.
import java.awt.*;
import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class img extends JFrame
{
private TextField field=new TextField();
private JButton b=new JButton("go");
private JEditorPane display=new JEditorPane();
private JScrollPane panee=new JScrollPane(display);
public static void main(String args[])
{
img file=new img();
file.frameHandler();
}
public void frameHandler() {
setTitle("Browser");
setSize(1200,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
setResizable(false);
setLocationRelativeTo(null);
addComponentsToFrame(getContentPane());
}
public void addComponentsToFrame(Container pane) {
Insets insets=getInsets();
pane.add(field);
pane.add(panee);
Font font=new Font("STENCIL",Font.ITALIC,10);
field.setFont(font);
field.setBounds(8-insets.left, 30-insets.top,1160, 20);
b.setBounds(1150-insets.left, 30-insets.top,80, 20);
panee.setBounds(8-insets.left, 52-insets.top, 1200, 830);
pane.add(b);
}
private void actionListenerCalls()
{
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
loadData("http://"+e.getActionCommand());
}
});
display.addHyperlinkListener(new HyperlinkListener(){
@Override
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadData(e.getURL().toString());
}
}
});
}
private void loadData(String text)
{
try{
display.setPage(text);
}
catch(Exception ee)
{
System.out.println("error");
}
}
}
Upvotes: 0
Views: 2041
Reputation:
You have not invoked the method actionListenerCalls
to bind listener
Possibly put it here:
public void frameHandler() {
....
addComponentsToFrame(getContentPane());
actionListenerCalls();//<-- invoke here
setVisible(true);
}
P.S. kindly avoid using null layout.
EDIT
From the docs:
We don't support the full CSS spec. Refer to the javadoc of the CSS class to see what properties we support. The two major CSS parsing related concepts we do not currently support are pseudo selectors, such as A:link { color: red }, and the important modifier.
Note: This implementation is currently incomplete. It can be replaced with alternative implementations that are complete. Future versions of this class will provide better CSS support.
You may try this to load external css:
StyleSheet ss = new StyleSheet();
ss.importStyleSheet(styleSheetURL);
HTMLEditorKit kit = (HTMLEditorKit)jEditorPane.getEditorKit();
kit.setStyleSheet(ss);
To open default browser
You may try using Desktop browse()
From docs
The browse(uri) method can throw a variety of exceptions, including a NullPointerException if the URI is null, and an UnsupportedOperationException if the BROWSE action is unsupported. This method can throw an IOException if the default browser or application cannot be found or launched, and a SecurityException if a security manager denies the invocation.
private void onLaunchBrowser(ActionEvent evt) {
URI uri = null;
try {
uri = new URI(txtBrowserURI.getText());
desktop.browse(uri);
} catch(IOException ioe) {
System.out.println("The system cannot find the " + uri +
" file specified");
//ioe.printStackTrace();
} catch(URISyntaxException use) {
System.out.println("Illegal character in path");
//use.printStackTrace();
}
}
Upvotes: 1