I just made a simple web browser. But the browser can't actually display any content. After I enter URL and press enter, nothing happens. The broswer window remains gray colored.
Where is the wrong?
package readfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ReadFile extends JFrame{
private JTextField addressbar;
private JEditorPane display;
public ReadFile(){
super("My Browser");
addressbar=new JTextField("Enter a hoss");
addressbar.addActionListener(
new ActionListener() {
//#Override
public void actionPerformed(ActionEvent event) {
//throw new UnsupportedOperationException("Not supported yet.");//To change body of generated methods, choose Tools | Templates.
loadCrap(event.getActionCommand());
}
}
);
add(addressbar,BorderLayout.NORTH);
display=new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener() {
//#Override
public void hyperlinkUpdate(HyperlinkEvent event) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//event.getEventType(event.);
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadCrap(event.getURL().toString());
}
}
}
);
add(new JScrollPane(),BorderLayout.CENTER);
//add(new JScrollPane(),BorderLayout.CENTER);
setSize(500,300);
setVisible(true);
}
private void loadCrap(String userText){
try {
display.setPage(userText);
addressbar.setText(userText);
} catch (Exception e) {
System.out.println("Crap");
}
}
}
Mian class:
package readfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JFrame;
public class ReadFileMain {
public static void main(String[] args) {
ReadFile rf=new ReadFile();
rf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You are never adding the JEditorPane to the JFrame. You are only adding the address bar and an empty JScrollPane.
Change
add(new JScrollPane(), BorderLayout.CENTER);
to
add(new JScrollPane(display), BorderLayout.CENTER);
Related
Hi i'm having a problem getting the URL from a JEditorPane that is setup in HTML using a HyperlinkListener, I've checked many stack questions on the problem but none of them seem to solve it, I've reduced the code down as far as I possibly can and still get the error. When I use the getURL() method it returns null and the getDescription() method returns the string "url" when I want it to return "https://www.google.com", I assume I'm just missing something very obvious
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class View extends JFrame {
private String urls = "https://www.google.com";
private JEditorPane textArea;
public View() {
super("Test");
setPreferredSize(new Dimension(400,400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JEditorPane();
textArea.setEditable(false);
textArea.setContentType("text/html");
textArea.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
textArea.setBackground(Color.lightGray);
textArea.setEnabled(true);
textArea.setText(urls);
textArea.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println(e.getURL());
System.out.println(e.getDescription());
}
}
});
add(textArea);
pack();
setVisible(true);
}
}
Thanks
The problem isn't with your HyperlinkListener or the information of the HyperlinkEvent
The problem is with your source HTML, take a moment to really look over it...
https://www.google.com
Can you see why it's printing url? The problem is, the HyperlinkEvent is giving you the href (and possible what would also be the description) attributes, not the text of the element.
So, based on that, your code is working fine.
However, if you actually want to return www.google.com, you need to change your source HTML to something more like Google me"
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Test extends JFrame {
private String urls = "Google me";
private JEditorPane textArea;
public static void main(String[] args) {
JFrame test = new Test();
test.pack();
test.setLocationRelativeTo(null);
test.setVisible(true);
}
public Test() {
super("Test");
setPreferredSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JEditorPane();
textArea.setEditable(false);
textArea.setContentType("text/html");
textArea.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
textArea.setBackground(Color.lightGray);
textArea.setEnabled(true);
textArea.setText(urls);
textArea.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println(e.getURL());
System.out.println(e.getDescription());
}
}
});
add(new JScrollPane(textArea));
}
}
This will print out
https://www.google.com
https://www.google.com
I have a simple web browser coded in java.
As you may know, the application can't display much.
For instance, google. It won't display the page the right way.
But that's not the problem I want to solve..
The problem is that it won't display pages like AGAR.io (don't misspell the "agar" word.. There's a Jeff the Killer jumpscare if you open this page : agor.io. So be careful :))
Here's the java code of the simple web browser:
package Gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.IDN;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
#SuppressWarnings("serial")
public class Frame extends JFrame implements HyperlinkListener {
private JTextField txtURL= new JTextField("");
JEditorPane ep = new JEditorPane();
private JLabel lblStatus= new JLabel(" ");
public Frame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pnlURL = new JPanel();
pnlURL.setLayout(new BorderLayout());
pnlURL.add(new JLabel("URL: "), BorderLayout.WEST);
pnlURL.add(txtURL, BorderLayout.CENTER);
getContentPane().add(pnlURL, BorderLayout.NORTH);
getContentPane().add( ep, BorderLayout.CENTER);
getContentPane().add(lblStatus, BorderLayout.SOUTH);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
String url = ae.getActionCommand().toLowerCase();
if (url.startsWith("http://"))
url = url.substring(7);
ep.setPage("http://" + IDN.toASCII(url));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(Frame.this, "Browser problem: " + e.getMessage());
}
}
};
txtURL.addActionListener(al);
setSize(300, 300);
setVisible(true);
}
public void hyperlinkUpdate(HyperlinkEvent hle) {
HyperlinkEvent.EventType evtype = hle.getEventType();
if (evtype == HyperlinkEvent.EventType.ENTERED)
lblStatus.setText(hle.getURL().toString());
else if (evtype == HyperlinkEvent.EventType.EXITED)
lblStatus.setText(" ");
}
public static void main(String[] args) {
new Frame();
}
}
But the program can't display pages such as agar.io.
Agar.io is a page with a simple game that seems to not use flashplayer..
Is there a way of making it possible?
I am trying to create a simple web browser but when i run it and hover over an URL the URL gets run even though i gave event.getEventType()==HyperlinkEvent.EventType.ACTIVATED
why does it behave like event.getEventType()==HyperlinkEvent.EventType.ENTERED
Here is the full Code
package gui;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
public class WebBrowser extends JFrame{
private JTextField addressbar;
private JEditorPane display;
public WebBrowser(){
super("Sagar Browser");
addressbar = new JTextField("Enter a URL");
addressbar.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
load(event.getActionCommand());
}
}
);
add(addressbar,BorderLayout.NORTH);
display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED);
load(event.getURL().toString());
}
}
);
add(new JScrollPane(display),BorderLayout.CENTER);
}
private void load(String usertext){
try{
display.setPage(usertext);
addressbar.setText(usertext);
}catch(Exception e){
System.out.println("Enter Full URL");
}
}
public static void main(String[] args){
WebBrowser w = new WebBrowser();
w.setSize(500,500);
w.setVisible(true);
}
}
Your listener ignores the relevant predicate. You probably meant this:
new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
load(event.getURL().toString());
}
}
}
Related examples are examined here and here.
So I've built a very basic Web browser - I'm trying desperately to remove the contents of the address bar when a user clicks on it (JTextField) this appears with some text in as default. Any advice is appreciated.
Have a great day!
MY CODE
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Web_Browser extends JFrame {
private final JTextField addressBar;
private final JEditorPane display;
// Constructor
public Web_Browser() {
super("Web Browser");
addressBar = new JTextField("Click & Type Web Address e.g. http://www.google.com");
addressBar.addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
loadGo(event.getActionCommand());
}
}
);
add(addressBar, BorderLayout.NORTH);
display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener(){
#Override
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadGo(event.getURL().toString());
}
}
}
);
add(new JScrollPane(display), BorderLayout.CENTER);
setSize(500,300);
setVisible(true);
}
// loadGo to sisplay on the screen
private void loadGo(String userText) {
try{
display.setPage(userText);
addressBar.setText(userText);
}catch(IOException e){
System.out.println("Invalid URL, try again");
}
}
}
Use a FocusListener. On focusGained, select all.
addressBar.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
JTextComponent textComponent = (JTextComponent) e.getSource();
textComponent.selectAll();
}
});
For example:
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.*;
import javax.swing.text.JTextComponent;
#SuppressWarnings("serial")
public class FocusExample extends JPanel {
private static final int TF_COUNT = 5;
private JTextField[] textFields = new JTextField[TF_COUNT];
public FocusExample() {
for (int i = 0; i < textFields.length; i++) {
textFields[i] = new JTextField("Foo " + (i + 1), 10);
textFields[i].addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
JTextComponent textComponent = (JTextComponent) e.getSource();
textComponent.selectAll();
}
});
add(textFields[i]);
}
}
private static void createAndShowGui() {
FocusExample mainPanel = new FocusExample();
JFrame frame = new JFrame("FocusExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
This gives the user the option of leaving the previous text in place, of adding to the previous text, or of simply over-writing it by typing.
new JTextField("Click & Type Web Address e.g. http://www.google.com");
Maybe you want the Text Prompt, which doesn't actually store any text in the text field. It just gives the user a hint what the text field is for.
This is beneficial so that you don't generate DocumentEvents etc., since you are not actually changing the Document.
Add a mouseListener instead of your actionListener method.
addressBar.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent e){
addressBar.setText("");
}
I have problem with getPage() method when i use it for JEditorPane , it dose not display a web page correctly and dose not suport every thing , means i want to display every thing like chrome and other browser. here is my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class browser extends JFrame {
private JTextField addressbar;
private JEditorPane display;
public browser(){
super("web browser");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
addressbar=new JTextField("Enter URL");
addressbar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
loadCrap(event.getActionCommand());
}
});
add(addressbar,BorderLayout.NORTH);
display=new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadCrap(event.getURL().toString());
}
}
});
add(new JScrollPane(display),BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
}
private void loadCrap(String Address){
try{
display.setPage(Address);
addressbar.setText(Address);
} catch(Exception e){
System.out.println("crap !");
}
}
public static void main(String[] args){
new browser();
}
}
You can use the Desktop class to display webpages. See the Swing tutorial on How to Integrate with the Desktop Class for more information and working examples.