• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

to call method

Hi,
I have this code which has statment call of another method (progressBar)in another class (progBar)but it dosn't work and the error message asks me to change the method to static . but when I do that i have another problems


Thank you in advance

code attached
public class Gui extends Applet implements ActionListener
{
        private TextField URLText = null ;
        private Button URLButton = null;
        final static int interval = 1000;
        int i;
        JLabel label;
        JProgressBar pb;
        Timer timer;
        JButton button;

        public void init()
        {
                add(new Label("URL"));
                URLText = new TextField(30);
                add(URLText);
                URLButton = new Button("Go");
                add(URLButton);

                URLButton.addActionListener(this);
        }
  public void actionPerformed(ActionEvent e)
        {
                String actionCommand = e.getActionCommand();
                if (e.getSource() instanceof Button)
                        if (actionCommand.equals("Go"))
                        {
                                System.out.println("Go");
                                String text = URLText.getText();      
                        
                try
                {
                	  progBar.progressBar();
                    connection.conn(text);
                    AppletContext context = getAppletContext();
                    URL url = new URL(URLText.getText());
                    context.showDocument(url);
                }
                catch(Exception ex)
                {
                        showStatus("Error "+ex);
                }

                        }
        }
public static void main(String[] args){
            Gui gui = new Gui();
          }
       
}
 *********************************************************
package URL_reader;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

  			/**
         * This method to display the progress bar window and and to do sync with
         * reading the URL file .
         */
public class progBar{
	public JLabel label ;
    int i;
    JProgressBar pb;
    Timer timer;
    JButton button;

public void progressBar(){
    JFrame frame = new JFrame("Progress Bar For URL Reading ");
button = new JButton("Start");
button.addActionListener(new ButtonListener());

//String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
//"Downloading is in process......." + "</b>" + "</font>" + "</html>";
////label.setText(str);



timer = new Timer(i, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
            if (i == 20){
                            Toolkit.getDefaultToolkit().beep();
                            timer.stop();
                            button.setEnabled(true);
                            pb.setValue(0);
                            String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
                            "Downloading completed." + "</b>" + "</font>" + "</html>";
                            label.setText(str);
            }
            i = i + 1;
        pb.setValue(i);
    }
});

timer.start();

pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);

label = new JLabel("Reading in progress");

JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);

JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create a timer.

}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
    button.setEnabled(false);
    i = 0;
    String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
    "Downloading is in process......." + "</b>" + "</font>" + "</html>";
    label.setText(str);
    timer.start();
}
}
}

Open in new window

0
obad62
Asked:
obad62
  • 3
  • 2
2 Solutions
 
gsx1022Commented:
Hi,

ProgBar is a class. If you just call a method of it like ProgBar.method(), that method has to be static. If you wouldn't like it to be static, first you will have to instantiate the class, and invoke the method of an instance of that class. So I believe, something like
ProgBar prbr = new ProgBar();
prbr.progressBar();
will do it for you. Or you could change ProgBar.progressBar to static, and resolve the issues that come up.
Please feel free to post if you need further help or explanation.

gsx1022
0
 
obad62Author Commented:
many many thanks gsx1022
I will try to do it
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
obad62Author Commented:
Hi ,

I tryed to change the non-static to static and I changed all the variable to static but I find problem that is
in attachment

 
package URL_reader;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.sun.net.ssl.internal.ssl.Provider;

//import URL_reader.Gui.ButtonListener;

  			/**
         * This method to display the progress bar window and and to do sync with
         * reading the URL file .
         */
public class progBar{
	final static int interval = 1000;
	public static JLabel label ;
    static int i;
    static JProgressBar pb;
    static Timer timer;
    static JButton button;
    Provider prbr = new Provider();
    
    public static  void progressBar() {
    
    	
        JFrame frame = new JFrame("Progress Bar For URL Reading ");
        button = new JButton("Start");
        button.addActionListener(new ButtonListener());
        
//        String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
//        "Downloading is in process......." + "</b>" + "</font>" + "</html>";
//        //label.setText(str);
        
        
        timer = new Timer(interval, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    if (i == 20){
                                    Toolkit.getDefaultToolkit().beep();
                                    timer.stop();
                                    button.setEnabled(true);
                                    pb.setValue(0);
                                    String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
                                    "Downloading completed." + "</b>" + "</font>" + "</html>";
                                    label.setText(str);
                    }
                    i = i + 1;
                pb.setValue(i);
            }
        });
        
        timer.start();

        pb = new JProgressBar(0, 20);
        pb.setValue(0);
        pb.setStringPainted(true);

        label = new JLabel("Reading in progress");
   
        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(pb);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.add(panel, BorderLayout.NORTH);
        panel1.add(label, BorderLayout.CENTER);
        panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        frame.setContentPane(panel1);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create a timer.
        
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            button.setEnabled(false);
            i = 0;
            String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
            "Downloading is in process......." + "</b>" + "</font>" + "</html>";
            label.setText(str);
            timer.start();
        }
    }
}

Open in new window

0
 
obad62Author Commented:
sorry in line 31
0
 
gsx1022Commented:
Hi,

after taking a look at your code, I would not suggest using static methods, since what you are trying to achieve (a progressbar) is not something that typically need to be done using static methods. If possible try to avoid using static methods. In your case I would rather change line 33 of your *original* code to
new progBar().progressBar()

And also, please check the link object posted, and for an explanation of the difference between static and instance methods, this url: http://www.codestyle.org/java/faq-Static.shtml

gsx1022
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now