Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

non-static method...static context

Thread view: 
Lex - 01 Dec 2003 16:42 GMT
Hi

this code is for a subtitle pane in our GUI.

when we try to call

Subtitle.setSubtitleText("whatever");

from another class we get this error:

Game2.java:198: non-static method setSubtitleText(java.lang.String) cannot
be re
ferenced from a static context
       Subtitle.setSubtitleText("Woohoo!");
               ^
1 error

Does anyone know what the problem is? Thanks

Leo

code:

import javax.swing.*;

public class Subtitle extends JPanel {
       // Set up the components in the frame.

   public Subtitle(int gameNumber, String text){
       super();
       setLayout(null);

       // Create the text label.
       labelText = new JLabel("<html><left><font color=white size=-1>" +
text +
               "</font></left></html>");
       add(labelText);
       labelText.setBounds(10, 5, 580, 24);

       // Create the subtitle label.
       ImageIcon iconSubtitle = Brainbug.createImageIcon("Game" +
gameNumber +
               "/Images/Subtitle.jpg", "Subtitle");
       JLabel labelSubtitle = new JLabel(iconSubtitle);
       add(labelSubtitle);
       labelSubtitle.setBounds(0, 0, 600, 36);
   }
       // Set the text on the text label.
   public void setSubtitleText(String text){
       labelText.setText("<html><left><font color=white size=-1>" + text
               + "</font></left></html>");
   }

   // Text label.
   private JLabel labelText;
}
Tor Iver Wilhelmsen - 01 Dec 2003 18:14 GMT
> Subtitle.setSubtitleText("whatever");

You need an instance:

Subtitle aSubtitle = new Subtitle();
aSubtitle.setSubtitle("whatever");
Steve Horsley - 01 Dec 2003 21:35 GMT
> Hi
>
[quoted text clipped - 16 lines]
>
> Leo
<snip>

http://www.google.com/search?q=java+non+static+method+static+context
Fahd Shariff - 03 Dec 2003 09:50 GMT
...or change the method into a static method:

public static void setSubtitleText(String text){
       labelText.setText("<html><left><font color=white size=-1>" + text
               + "</font></left></html>");
   }

u will also need to declare the label static:

   // Text label.
   private static JLabel labelText;

----
Fahd
Christian Kaufhold - 03 Dec 2003 14:29 GMT
>        super();
>        setLayout(null);

        super(null);

>        // Create the text label.
>        labelText = new JLabel("<html><left><font color=white size=-1>" +
                                      ^^^^^^

???

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.