Hyperlink event type activated error - java

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.

Related

Java, Errors with JButton

I am trying to make a Login Form with Java. I Cant get it working.
I have looked all over then internet for how to fix this, I can't find anything.
Code:
LoginFrame.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginFrame extends JFrame
{
JPanel pane = new JPanel();
static JFormattedTextField username = new JFormattedTextField(16);
static JFormattedTextField password = new JFormattedTextField(16);
static JButton loginButton = new JButton("Login!");
static String input[];
public LoginFrame() throws IOException
{
super("Login");
setSize(300,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(pane);
pane.add(new JLabel("Username"));
pane.add(username);
pane.add(new JLabel("Password"));
pane.add(password);
pane.add(loginButton);
#Override
IEventHandler eHandler = new IEventHandler();
#Override
loginButton.addActionListener(eHandler);
setVisible(true);
}
static String[] getInput()
{
return input;
}
}
IEventHandler.java:
import java.awt.event.*;
class IEventHandler implements ActionListener
{
public void actionPreformed(ActionEvent e)
{
if(e.getSource() == LoginFrame.loginButton){
LoginFrame.loginButton.setEnabled(false);
new AuthLIB().authenticate(LoginFrame.getInput());
}
}
public IEventHandler()
{
System.out.println("Event Handler Hooked");
}
}
You aren't overriding anything with-in the method body. These
#Override
IEventHandler eHandler = new IEventHandler();
#Override
loginButton.addActionListener(eHandler);
should just be
IEventHandler eHandler = new IEventHandler();
loginButton.addActionListener(eHandler);
and assuming you want ActionListener.actionPerformed(ActionEvent)
public void actionPreformed(ActionEvent e)
should use that annotation. You'd see the spelling mistake quicker anyway.
#Override
public void actionPerformed(ActionEvent e)

Webpage isn't displaying in my simple web browser (written in Java)

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);

Clear JTextField Contents on Click

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("");
}

JButton executing an exe file in directory

let's say I have a JButton called "Play", and when I click this, it should launch C:/play.exe. How do I manage to do this? I'd love to see an example.
Take a look at the javadoc for ProcessBuilder which contains an example of how to create a process on the underlying system.
From there it's a simple matter of hooking it up to the button's ActionEvent
Have a look at the Runtime.getRuntime().exec() method. See this example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label = new JLabel("Click here: ");
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Process process = null;
System.exit(0);
try {
process = Runtime.getRuntime().exec("C:/play.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}

JEditorPane using getPage() method for webpage

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.

Categories

Resources