Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

methods in java swing not working in eclipse  RSS feed

 
Jeff Karr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've been trying to create gui's in java for days now but I keep running into problems, a lot of the methods get underlined with a red line because apparently there are some type of error, the problem is I'm not really sure what the error means so I took a pic of it, the goal here is to create a contact book, but first I'm going to try to do something easy and that is to create a simple frame that has a text field that allows you to enter your name and email address, has a check box that ask the user if they want there name to be revealed when the email is sent and when you hit the send button it sends a message to the email you entered. I could probably use this as a "send a comment to the administrator" on a website or something like that.



here's a print screen image, direct link is -> http://i42.tinypic.com/332mi5c.png



This code works, but when I try to add panels and buttons it doesn't, what about inheritance, creating objects/classes/methods/constructors/polymorphism, final keyword this keyword and extends/abstract and final classes and variable types do I need to know about in order to successfully program a gui?
 
Jeff Karr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code, fallowed by the stack trace.
THE CODE




STACK TRACE

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The method setDefaultCloseOperation(int) is undefined for the type MainFrame

This method belongs to the JFrame class. You are invoking it on your MainFrame class. It is as good as saying dog.meow(); The dog doesn't meow now does it? You need to invoke it on your JFrame instance.
Hint: You are already invoking methods on your JFrame instance frame.setVisible(true);
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading your code more carefully, I noticed you are making the classic beginner mistakes.
Never ever, explicitly set bounds. Always use a layout manager.
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Also, the call to set visible should be made at the end after you are done adding all the children to the frame
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

yes, I consider it to be the biggest advantage of using IDE's like Eclipse and NetBeans: they show you these
errors at typing time, instead of at compiling time (but some people around here "might" be of different
opinion ;))

Your class MainFrame starts with the following line:



For Java, that means you are creating a subclass of the class Object.

In line 27 you write



So, what Java tries to do now, is to look up the method "SetDefaultCloseOperation" in the definition of
the Object class. Well, it can't find it there, so that's why you get this error report. Eclipse is warning you
for this, that's why you see a red circle on the left and a red line under your text.

The same holds for your next few lines.

Look at lines 25 and 26. Here you say that frame is an instance of the JFrame class, and that it should be visible (line 26).
What you want next is to set the default close operation for this frame. So, just like line 26, you should do this
by


and

and later


Then you write:
This code works, but when I try to add panels and buttons it doesn't, what about inheritance, creating
objects/classes/methods/constructors/polymorphism, final keyword this keyword and extends/abstract
and final classes and variable types do I need to know about in order to successfully program a gui?


Now, that is a lot of questions! It shows that you are at the beginning of a long and interesting road leading to Mastership in Java.
My advice: take small steps, and give yourself all the time you need. But most important: read the Java Tutorials as often as you can,
and many people around here can advise you on some good books.

So, add in line 32 this:


and that's it! Run the program and enjoy the view of your frame on the screen. Then, add the panel to the frame, see if your code is still
running, and then decide how to go on. As I said, taking very little steps

Greetings,
Piet
 
Jeff Karr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeff Karr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the replies guys I really appreciate it, so I was able to fix the problem as Maneesh Godbole pointed out, I had the methods pointing towards the java src file name and not the class name "JFrame". I was able to fix the problem by creating a non-static method using the object name, and to my surprise it worked. But shortly after I ran into another problem, but first, the updated/fixed code and screenshot, I don't normally code in this style but eclipse auto-generates a load of fiddle jabber...speaking of IDE's... what are your guys's favorite IDE? Jcreator vs netbeans vs eclipse, thoughts/opinions?

edit 1: I also initlized private instance variables and a constructor using this keywords to refer to the instance variables in the parameters section of the construct0rZ0rzZZzzzz

//code


//img

- direct link ->http://i40.tinypic.com/mj6agl.png



The problem is is that putting the object name before the method "centerWindow(this)" does not work. I get a "cannot use in static context error". now I understand this, I imagine that it is because this keyword refers to objects that are put in the parenthesis that holds the arguments and parameters correct? and that static methods directly go to the class without referring to any object, as in ClassName.methodName(arguments); as opposed to objectName.methodName(arguments/parameters); ??? that is why I ran into the error because when I tried to use the "this" keyword I realized that I can't put a method in the constructors parameter/argument section. Even still, I don't know my way around this problem, all I want to do is center the window, then after that set up some buttons and panels, two text fields, one for email, one for a full name, a comment box, and a couple of radio/check boxes, I want the small application to send information to a designated source (such as an email address) in a text file.



Also, how did you guys implement java coding onto the forums? how does that work? I'm also a web developer I had a website but I recently gave it up, so I'm also interested in that. ): ): ): ):

god bless,

-Jeff.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is centerWindow method? Is it yours? If yes, please show us the code. It certainly isn't a method available in the standard API for JFrame or any of it's ancestors.

In case you want a built in method to center your frame, check out setLocationRelativeTo()

Considering you are a noob, I would advise to stay away from IDEs. When I started off, I used Text Pad. Others have shared their experiences with JEdit
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!