wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes swing questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "swing questions" Watch "swing questions" New topic
Author

swing questions

Greg Walker
Greenhorn

Joined: Sep 07, 2006
Posts: 24
1. How do you format buttons, fields, areas, etc... to been seen where you want them in the container?

2.When running a program with a GUI class and a backcode class of methods to manipulate data, how do you keep from having to constantly instantiate new instances of the classes while being able to access their methods?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Greg Walker:
1. How do you format buttons, fields, areas, etc... to been seen where you want them in the container?
You use layout managers to control where components go. Here is a link to part of the Swing tutorial that talks about that.
2.When running a program with a GUI class and a backcode class of methods to manipulate data, how do you keep from having to constantly instantiate new instances of the classes while being able to access their methods?
It's hard to understand what you mean by that. Normally the reason you'd want to access the method of an instance is because you want to get information from the instance or you want to affect its state. If you want to do that repeatedly, then keep a reference to the object.
[ September 19, 2006: Message edited by: Paul Clapham ]
Greg Walker
Greenhorn

Joined: Sep 07, 2006
Posts: 24
Originally posted by Paul Clapham:
It's hard to understand what you mean by that. Normally the reason you'd want to access the method of an instance is because you want to get information from the instance or you want to affect its state. If you want to do that repeatedly, then keep a reference to the object.

[ September 19, 2006: Message edited by: Paul Clapham ]


What I mean is in my GUI class when a user presses the "GO!" button that calls a method located in the BackCode class. The BackCode class then, for example, opens a file and displays that info in a textArea located in GUI class. To access the GUI class I need to have an instance of it. Every time I declare an instance it creates a whole new containier and I wind up with 20 containers after 20 button presses.

When I try to declare a global variable in the BackCode class my GUI freaks out and crashes.
[ September 19, 2006: Message edited by: Greg Walker ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> When I try to declare a global variable in the BackCode class my GUI freaks out and crashes.

from your description, here's a simple demo
click the button to update the textArea with the data (in this case random numbers)

Greg Walker
Greenhorn

Joined: Sep 07, 2006
Posts: 24
Well that's just one example. What if I want to activate an option pane from teh BackCode class. The way I ahve it set up is in GUIclass there is a method

example: public void subscribeFailed(String newsgroup) {
//display option pane that says subscription failed
}

But to call that I need an instance of GUIclass...
GUIclass object = new GUIclass();

...to call the method to display the option pane

object.subscribeFailed("sci.physics");

The instantiation creates a brand new container (JFrame) every time it's created. So usually to avoid having to keep creating objects we declare them globably so all the methods can use them, but when I do this I get a initiation error message at the terminal prompt and it keeps displaying it as if in an infinite loop.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Well that's just one example.

notice how you were able to copy/paste/compile/run and see how it all fit together.

now it's your turn.
 
jQuery in Action, 2nd edition
 
subject: swing questions
 
Similar Threads
Polymorphism
Static methods in Java Interfaces?
Overriding??!
ClassCastException
Overriding methods in java