It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes GUI components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "GUI components" Watch "GUI components" New topic
Author

GUI components

Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi,

I'm implementing GUI interface for B&S, i read Andrew's book and i find mayority of components are created in constructor (like buttons, labels, etc), but for textfields and another components reciving an input are declared as members of these classes . I didn't know if there is a pattern or a standard to do that in this way or where i can find more information about this. Thanks ranchers.


Gabriel Vargas
SCJP, SCJD, now studying for SCWCD and working to be a better person
Marcelo Camargo
Ranch Hand

Joined: Jul 05, 2007
Posts: 43
Hi Gabriel,

I read the same book, but I did it in a different form. I am using a controller and I leave the view just for the view, without any code for enable, disable, etc... The controller do all of that. And, I have to have a way to change the view state (enable, disable, etc) from the controller.

So, I did this way:

For example, my ServerView class has, for the modifieble controls, class instance variables. This is necessary, because I need to create public methods (getters and setters) for access them. Ex:

public class ServerView {
...

private JButton startServerButton = null;

public ServerView(final ServerControllerInterface controller) {
...
/* create the controls */
startServerButton = new JButton(START_SERVER_BUTTON_TEXT);
...
}

...

/** Enables the server start button in the view. */
public void enableStartServerButton() {
startServerButton.setEnabled(true);
}

/** Disables the server start button in the view. */
public void disableStartServerButton() {
startServerButton.setEnabled(false);
}

...
}

This way, from my controller, I can:

public class ServerController implements ServerControllerInterface {
...

public void startServer() {
log.finer("start server requested.");

/* starts the server */
model.startServer();

/* adjusts the view to the new state */
view.disableStartServerButton();

...
}

...
}

But, for the construction of the view, I have a method in each view called createView(), that is responsible for constructing all the view components, use the layout managers, etc.

public class ServerView { // the same class I shown before...
...

public void createView() {
...
/* construct the view */

mainFrame.pack();
mainFrame.setVisible(true);

...
}

...
}

I call this method from the controller to show the view. This way, I can control the view from the controller.

I hope I could help you,
Marcelo.


SCJP5(93%), SCJD(in progress...), SCEA(not started yet...)
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi Marcelo,

Thanks for your answer, it solves some doubts i have. But if the component is a label or a layout, it would be declared as members of your class or delcared an initialized in some method? Maybe declare label as a member and layout into a class method or both into the class method?
Marcelo Camargo
Ranch Hand

Joined: Jul 05, 2007
Posts: 43
I declare only the objects that I need to access outside the class as class members. For example, a button that I have to enable or disable from the controler (outside the class), I declare as a class member and I create an appropriate getter and setter for it. If I have a label that I need to change the text, I also declare as a class member.

But, for example, a layout manager that I will use only inside a method, I declare it inside the method and I add it to the frame.

The rule I used is: declare only what is really necessary as a class member, and the rest inside the methods.

Marcelo.
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Thanks Marcelo, you solve my doubt and another doubt of how implement view and controller for this assgiment. Again, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI components