Results 1 to 4 of 4
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default GUI - is this bad practice?

    I have to write a GUI without an IDE.

    Is this bad practice: Make a new regular class for the GUI, define each component of the GUI as a variable. Set the properties in the constructor, then let methods access it easily.

    Or do I have to make things static.. as when I use the example at the bottom of this page: Java BorderLayout Example I'm not sure on how to divide it into multiple methods, instead of just one.

    Thanks for your help.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Either is probably fine, however; I prefer to go with a more oo approach and use variables that are instantiated by the constructor, then provide getters and setters so other classes can make changes and get the current states of components.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    there are many patterns how to create a gui. one i like is this one: the components used in the gui are declared as variables. inside the class there is method createGUI() that initialize/instatiate the variables and returns a jpanel with all components for the gui. inside the main() method there is only the call to new yourclass.createGUI(); which adds the jpanel to the top-level component. this pattern is very flexible. if the top-level component is a applet the jpanel will be added to the applet and if it is a frame the jpanel is added to the frame.
    Last edited by j2me64; 05-01-2011 at 09:51 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by keo View Post
    I have to write a GUI without an IDE.

    Is this bad practice: Make a new regular class for the GUI, define each component of the GUI as a variable. Set the properties in the constructor, then let methods access it easily.
    I'd only make as variables those fields that I need a reference to in the class. For instance, if I have a JTextField whose text changes depending on the state of the object, then it needs to be an instance field of the class. If I have a JLabel whose text never changes, then it is declared and added inside of a method or constructor and it is not visible to the class.

    Or do I have to make things static.. as when I use the example at the bottom of this page: Java BorderLayout Example I'm not sure on how to divide it into multiple methods, instead of just one.
    The only things static are things that should be static. For instance if I have a class Player, and I need a variable in this class to keep track of total number of Player objects, then this variable is static. Or if I have a constant, it is usually final static. Everything else should probably be instance. The example you show is just a quick and dirty example of how to use borders but and puts all in the main method, something you should not do in anything but the most simple of programs (say a 20 line program).

Similar Threads

  1. Suggestions on a Practice App for Java2ee
    By chizbox in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 06:36 AM
  2. java practice
    By miko5054 in forum New To Java
    Replies: 3
    Last Post: 03-22-2010, 05:31 PM
  3. Best practice for packaging add-ons
    By Goose in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2009, 10:51 PM
  4. SCJA - practice
    By Mihail Kravsun in forum Java Certification
    Replies: 2
    Last Post: 04-29-2009, 07:26 AM
  5. Best practice of IF loop with NOT
    By sathishranganathan in forum Advanced Java
    Replies: 13
    Last Post: 05-29-2008, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •