Member Avatar for Search_not

How do I add HTML5 functionality to my browser?
Its still a work in progress, so ignore the event handlers and buttons that seem pointless...

package browser;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ReadPage extends JFrame {

    private JTextField address;
    private JEditorPane display;
    private JButton refresh;
    private JButton go;
    private JPanel pnlSearch;
    private String homepage;

    public void setHome(String home){
        homepage = home;
    }

    public ReadPage() throws HeadlessException {
        super("Venster Browser");
        setSize(600, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();

        go = new JButton("-->");
        go.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                loadCrap(address.getText());
            }
        });
        go.setSize(20, 35);
        Icon icon = new Icon() {

            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                g.drawOval(x, y, y, y);
            }

            @Override
            public int getIconWidth() {
                return 10;
            }

            @Override
            public int getIconHeight() {
                return 10;
            }
        };

        refresh = new JButton(icon);
        refresh.setSize(20, 35);

        address = new JTextField("www.google.com", 35);
        address.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                loadCrap(e.getActionCommand());
            }
        });

        pnlSearch = new JPanel();
        pnlSearch.setLayout(flo);
        pnlSearch.add(go);
        pnlSearch.add(refresh);
        pnlSearch.add(address);       
        pnlSearch.setSize(JFrame.WIDTH, 50);
        add(pnlSearch, BorderLayout.NORTH);

        display = new JEditorPane();
        display.setEditable(false);
        display.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
                    loadCrap(e.getURL().toString());
                }
            }
        });
        add(new JScrollPane(display), BorderLayout.CENTER);
        address.requestFocusInWindow();
        setVisible(true);
        getRootPane().setDefaultButton(go);

        ReadPage.super.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {

            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {
                try{
                    display.setPage(homepage);
                }catch(Exception ex){
                    ex.printStackTrace();
                    display.setText("Couldn't connect to the internet...");
                }
            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
    }

    private void loadCrap(String userText){
        try{
            //String newText = "http://" + userText;
            //display.setPage(newText);
            display.setPage(userText);
            address.setText(userText);
        }catch(Exception e){
            System.out.println("Crap!");
            display.setText("Could not connect to the internet...");
            e.printStackTrace();
        }
    }
}

Recommended Answers

All 8 Replies

that might be a mess.
writing your own browser from scratch might be a bit (or, a lot more than: 'a bit') difficult.
the rules for html5 are fixed, just like those of other versions.
have you already a sort of 'first draft' that is able to work with older versions, that require less support?

I doubt very much that the HTML support in Swing will ever be updated for HTML 5.
The future of rich graphical GUIs in Java is JavaFX, so maybe you should be doing this in JavaFX not Swing. The JavaFX docs do refer explicitly to HTML 5.

Member Avatar for Search_not

@stultuske
No, I do not know how to make my browser function with any form of HTML. I'm in the process of learning Java and I haven't found any tuturials on it yet. That is why I am asking for help

@JamesCherril
I have never used JavaFX... Is the layout of the GUI design similar to swing? e.g. When you declare a visual component: ComponentName object = new ComponentName(parameters)

Search_not: if you are in the process of learning Java, stop what you are doing, and learn Java. this is way beyond you.

even if you had a good grasp on the language, I would still recommend to start with a simpler browser, and not trying to write competition for the latest versions of the major browsers within a few hours.

Member Avatar for Search_not

This browser is one of the activities in the tutorials I'm learning from. The tutorial only showed a simple browser, but when it loads pages formatted in HTML, it leaves out vital parts of the webpage... I couldn't even sign in to Google. That's why I'm trying to get HTML5 functionality into the browser. Thanks for the advice though. I'll stick to the simple browser I have now and learn Java.

this is not the kind of tutorial you should follow if you are just starting to learn Java. copying code doesn't make you understand it.
have a go at a good textbook, or at the official tutorials

Member Avatar for Search_not

Its a step-by-step tutorial that explains the code and its purpose. I got the video's from Click Here. I have a good idea of what the code does and how to implement it without copying code

I agree with both, @stultuske and @James,
This isn't really something you should do while learning java, if you want to make GUI based projects you could implement basic ones to get an idea of how things works(which I assume you know by the way you talk about stuff) such as Calculator/Lock combination to unlock a button or a text something, basically play with basic GUI components and do the entire code yourself.

Anyway, as James mentioned if you still want to go after this, you' ll need javaFX, This is what I used to learn the basics, I am not saying its a great guide but it helped me a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.