This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ]
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.
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?
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.
Joined: Dec 10, 2001
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?
Joined: Apr 27, 2002
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.
Joined: Dec 10, 2001
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 ]