0

i am trying to make a web browser and here is my code

CODE:

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

public class browserPannel extends JFrame{

    public static void main(String[] arg)
    {
        JFrame browser = new JFrame("A Nun In A Weelchair");
        browser.setSize(1000,700);
        browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        browser.setLocationRelativeTo(null);
        browser.pack();
        browser.setVisible(true);

        JPanel header = new JPanel();
        header.setBackground(Color.lightGray);
        header.setVisible(true);

        final JEditorPane htmlc = new JEditorPane();
        htmlc.setBackground(Color.red);
        htmlc.setEditable(true);
        htmlc.setContentType("text/html");
        htmlc.setVisible(true);

        final JTextField url = new JTextField(20);

        url.setSize(890,30);
        url.setVisible(true);

        url.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    loadHtml(htmlc, url, event.getActionCommand());
                    System.out.println("action performed");
                }
            }
            );

        JButton send = new JButton("Send");
        send.setSize(75,30);
        send.setVisible(true);


        header.add(url, BorderLayout.SOUTH);
        header.add(send);
        browser.getContentPane().add(header, BorderLayout.NORTH);
        browser.getContentPane().add(new JScrollPane(htmlc));
    }

    private void loadHtml(JEditorPane htmlc, JTextField url, String link)
    {
        try{
            htmlc.setPage(link);
            url.setText(link);
        }catch(Exception e){
            System.out.println("ops sorry could not fined Virgine Mobile");
            e.printStackTrace();
        }
    }

}

and here is my error message:

browserPannel.java:38: error: non-static method loadHtml(JEditorPane,JTextField,
String) cannot be referenced from a static context
                                        loadHtml(htmlc, url, event.getActionComm
and());
                                        ^
1 error

as you can tell it is getting errors from the loadHtml which is defined in a method at the buttom of my code, now if i remove the loadHtml, then it displays the println("action Performed");, but only when i refer the loadHtml it says that it that the non static method can not be preformed in a static method.

user207421
  • 305,947
  • 44
  • 307
  • 483
yoseph1998
  • 355
  • 2
  • 12
  • 1
    Did you bother to search for other questions about referencing non-static methods from a static context? This question has been asked SO MANY times! – Dawood ibn Kareem Sep 02 '14 at 04:00

2 Answers2

0

Correct. You need an instance of your class browserPannel to invoke that method,

browserPannel bp = new browserPannel();
bp.loadHtml(htmlc, url, event.getActionCommand());

or you can make

private void loadHtml(JEditorPane htmlc, JTextField url, String link)

static, like

private static void loadHtml(JEditorPane htmlc, JTextField url, String link)

Edit

Also, Java camel case conventions would name your class BrowserPanel (and I suggest you follow that convention).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

As you have called the "loadHtml" from main method you will have to do either of the below:-

1) use object of browserPannel class to call loadHtml or

2) you will need the loadHtml method to be static

remember the rule static and within the same class can be directly called from main method

cheers!

Vihar
  • 3,626
  • 2
  • 24
  • 47