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.
Related
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);
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("");
}
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);
}
}
I tried to create a floating dialog box which contains a loader gif image and some text. I have got the following class:
public class InfoDialog extends JDialog {
public InfoDialog() {
setSize(200, 50);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
setUndecorated(true);
setLocationRelativeTo(null);
URL url = InfoDialog.class.getClassLoader().getResource("loader.gif");
ImageIcon loading = new ImageIcon(url);
getContentPane().add(new JLabel("Logging in ... ", loading, JLabel.CENTER));
}
}
However, when I call:
InfoDialog infoDialog = new InfoDialog()
infoDialog.setVisible(true);
An empty dialog is shown. The ImageIcon and the Label is not shown in the dialog box.
What did I do wrong in this code?
Many thanks.
Images are usually placed into a "resource" Source Folder,
and then accessed as a byte stream,
package com.foo;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Demo
{
private static final String IMAGE_URL = "/resource/bar.png";
public static void main(String[] args)
{
createAndShowGUI();
}
private static void createAndShowGUI()
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
try
{
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setTitle("Image Loading Demo");
dialog.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream(IMAGE_URL)))));
dialog.pack();
dialog.setLocationByPlatform(true);
dialog.setVisible(true);
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
to produce Morgan Freeman.
I would add the ImageIcon to a JLabel and then add JLabel to JDialog contentPane followed by a this.validate and this.repaint in the constructor.
To debug, sometimes you need actual code without which these are just suggestions based on assumptions