This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Having problems making a simple GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having problems making a simple GUI" Watch "Having problems making a simple GUI" New topic
Author

Having problems making a simple GUI

Luther Adon
Ranch Hand

Joined: Apr 27, 2002
Posts: 51
Hi guys,
I am building my first GUI from scratch and I am not finding it all that simple. The problem lies in my program flow, I think. No matter what I do I seem unable to I do not seem to be able to change any of the textfields. When I check to see if the event is firing it seems fine. The error I am getting is non-static method getTf1() cannot be referenced from a static context. When I try to operate on the 'this' object it cannot resolve the symbol getTf1() method. I have been stuck here way too long, please help!

[ June 15, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Your last line of code is a problem:
String a = RightPanel.getTf1();
Here you're trying to use the method getTf1 as if it were a static member of the class RightPanel while in your class definition for RightPanel, you've defined getTf1 to be an instance method. So, you need an instance of the class RightPanel in order to use this method. You can instantiate RightPanel locally (within the method actionPerformed in the class MyCodeToClearData), and it will compile and run (a little bit), but you have other problems.
From what I understand you are trying to do with this program, it looks like you're instantiating new objects when you ought to be using existing instances.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Luther Adon
Ranch Hand

Joined: Apr 27, 2002
Posts: 51
Thank you for your response.
However I am unclear, shouldn't my main method start things up and each thing instanciates the next component? So the frame instanciates the panels which in turn instanciate the buttons. I hope I am right, I thought I was getting this!
What I really need to do is just access the text fields of the RightPanel named right from the MyCodeToClearData class. How do I do this?
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
Hi Luther,
I went through your code. Yes it is true that you are instantiating all the classes, but how would your action listener ( which is a seperate class ) would know about the the instances of various panels you have created. I suggest that you declare your action listner as the inner class in your main class. And then you can access the text field value and the error of non-static reference would also disapper.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
shouldn't my main method start things up and each thing instanciates the next component? So the frame instanciates the panels which in turn instanciate the buttons.
That is not unreasonable. That's not exactly what you've done, however. You are instantiating the LeftPanel, the RightPanel, and the ButtonPanel each twice - once in LCBOTest and once in MainPane. Perhaps that's what you want to do, though, I doubt it. I haven't actually run the code or tried to figure out exactly what you're trying to create.
What I really need to do is just access the text fields of the RightPanel named right from the MyCodeToClearData class. How do I do this?
Mohan's idea looks to be a decent one.
Any luck yet?
Luther Adon
Ranch Hand

Joined: Apr 27, 2002
Posts: 51
Actually I am not having much luck, I tried working from the Example Program in Peter van der Linden's book and looking at some Core Java examples. I've been referring to various sections to help me along and I did notice I was, as said before instanciating the panels twice so I fixed that. I also thought maybe the panels and their textboxes should be static as I only need one. So I did that and that solved the problem of calling non static data from a static method. Now the GUI does nothing and the button does nothing. So now I'm thinking of trying that suggestion of using an inner class. What's confusing me is the event was firing the now coded out system.out.println I just can't access that textbox.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This doesn't seem to be situation where making the Panels, TextFields, or Buttons static is appropriate or necessary (yes, you could, but you don't really need to).
Would it make sense to start with a smaller and simpler GUI in order to build your skills to better be able to developed a more complicated and involved GUI?
I'd recommend that you forget about Swing for a few days (or a few weeks), and work just with the AWT to practice using Buttons and TextFields and Labels and any appropriate Event handling.
If you'd like, I'm sure I could come up with a few things to get you started.
Good Luck.
[ June 22, 2002: Message edited by: Dirk Schreckmann ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having problems making a simple GUI
 
Similar Threads
Need assistant in placing random number generator for my codes. Help...please..
GridbagLayout help needed (this time with code tags :-) )
JTextArea with varied lengths
Lining up JTextFields next to JLabels
Getting sql error [Microsoft ][odbc microsoft access driver]numeric index out of range