?
Solved

to call method

Posted on 2010-01-02
6
Medium Priority
?
237 Views
Last Modified: 2012-06-27
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
Comment
Question by:obad62
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
gsx1022 earned 2000 total points
ID: 26163700
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
 
LVL 92

Expert Comment

by:objects
ID: 26163719
0
 

Author Comment

by:obad62
ID: 26163929
many many thanks gsx1022
I will try to do it
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:obad62
ID: 26164011
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
 

Author Comment

by:obad62
ID: 26164013
sorry in line 31
0
 
LVL 4

Assisted Solution

by:gsx1022
gsx1022 earned 2000 total points
ID: 26165015
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question