• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to solve the error "non-static method can not be referenced from static context"  RSS feed

 
danish shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I m creating n Text Editor using JEditorPane().
I want to change the color of the text as i type ne new thing in editorpane.
For that i have used the folloeing code:--
set=new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.RED);
StyleConstants.setUnderline(set, true);StyledEditorKit.StyledTextAction.setCharacterAttributes(editor,set,true);

but it's showing me following error:-

non-static method setCharacterAttributes(javax.swing.JEditorPane,javax.swing.text.AttributeSet,boolean) cannot be referenced from a static context.

What does it means n how to solve it.

Plz help me.
Thanks in Advance.
Regards,
Danish Shaikh.
 
Michael Parmeley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe you have given us enough of your code to point the exact problem for your situation.

However, if you have one method that is not static, you cannot call it from another method which is static.



If you made myMethod() static in the above example it would be legal.

[ November 22, 2004: Message edited by: Michael Parmeley ]
[ November 22, 2004: Message edited by: Michael Parmeley ]
 
Gloria McCarthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I found this old post and it sounds like the same problem I am having. I created two classes that extend JFrame and when I try to run a method or update a variable from one class to another, I get an error that says "non-static method can not be referenced from static context"

I am pretty new to Java, so this is probably something basic I am not understanding. Below is the class file that I am getting the error in.

Hope someone can tell me what I am missing, thanks
---------------------------------------------------

package main;
import orders.OrdersUI;

public class Menu extends javax.swing.JFrame {
public Menu() {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Menu().setVisible(true);
}
});
}

private void navOrdersButtonActionPerformed
(java.awt.event.ActionEvent evt){
OrdersUI.setVisible(true);
//the above line of code causes the error
}

---------------------------------------------------
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by danish shaikh:
I m creating n Text Editor using JEditorPane().
I want to change the color of the text as i type ne new thing in editorpane.
For that i have used the folloeing code:--
set=new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.RED);
StyleConstants.setUnderline(set, true);
StyledEditorKit.StyledTextAction.setCharacterAttributes(editor,set,true);


Well you've got a couple of problems here. The one the compiler is complaining about is that setCharacterAttributes() is not a static method in StyledEditorKit.StyledTextAction. I guess that's why this thread was moved to the beginner forum.

In addition, StyledEditorKit.StyledTextAction is an abstract class and it presumes that the JEditorPane's EditorKit is a StyledEditorKit. If your EditorKit isn't a StyledEditorKit (or a subclass, such as HTMLEditorKit) then StyledEditorKit.StyledTextAction.setCharacterAttributes() will throw a IllegalArgumentException.

On the other hand, if you know that it is a StyledEditorKit, then you don't need to use StyledTextAction. You could call getInputAttributes().addAttributes(set) directly on your StyledEditorKit.


btw, if you would use a JTextPane instead of a JEditorPane things might be easier. Not only would that guarantee that you were using a StyledEditorKit, but JTextPane defines setCharacterAttributes() and getInputAttributes() methods that you can use directly without messing with EditorKits.



Is there a reason you need to use JEditorPane instead of JTextPane?
[ January 26, 2007: Message edited by: Brian Cole ]
 
Gloria McCarthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, thanks for your reply, but you replied to the question from Nov, 2004. I guess I should of started a new post.

My question is. I created two classes that extend JFrame and when I try to run a method or update a variable from one class to another, I get an error that says "non-static method can not be referenced from static context" Or in other words, now do you create two classes and set a variable or run another method without getting this error?

I am pretty new to Java, so this is probably something basic I am not understanding. Below is the class file that I am getting the error in.

Hope someone can tell me what I am missing, thanks
---------------------------------------------------

package main;
import orders.OrdersUI;

public class Menu extends javax.swing.JFrame {
public Menu() {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Menu().setVisible(true);
}
});
}

private void navOrdersButtonActionPerformed
(java.awt.event.ActionEvent evt){
OrdersUI.setVisible(true);
//the above line of code causes the error
}
 
Ryan Giomi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, just going to take a stab at this (I'm relatively new also).

Try this:


Also, is the above method intended to be included in the Menu() class?(Just checking as there is one brace missing at the end if it is indeed supposed to be included.)

BTW, I entered your code into my editor, and added the aforementioned brace; and I did indeed get a graphical frame (which I know you did as well). So if my suggestion is incorrect and you want to share the OrdersUI file I would be happy to look further into this from my end (provided you did not find a resolution elsewhere).
[ January 27, 2007: Message edited by: Ryan Giomi ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables and methods exists whether an object is created or not, and are shared across all instances. Non-static variables only exist once an object has been created, and then only for that specific object.

You need to create an OrdersUI object, then you can set that objects property to true.

Think about it this way... without creating the object, WHAT exactly is going to be visible?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!