• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI components

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gabriel Vargas
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marcelo, you solve my doubt and another doubt of how implement view and controller for this assgiment. Again, thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic