Results 1 to 11 of 11
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default repeated code help

    Hi,

    In my GUI which consists of a keyboard I have a lot of repeated code.
    for eg

    aBtn.addActionListener(this);
    bBtn.addActionListener(this);
    cBtn.addActionListener(this);
    dBtn.addActionListener(this);
    eBtn.addActionListener(this);
    fBtn.addActionListener(this);
    gBtn.addActionListener(this);
    hBtn.addActionListener(this);
    iBtn.addActionListener(this);
    jBtn.addActionListener(this);
    kBtn.addActionListener(this);
    lBtn.addActionListener(this);
    mBtn.addActionListener(this);
    nBtn.addActionListener(this);
    oBtn.addActionListener(this);
    pBtn.addActionListener(this);
    qBtn.addActionListener(this);
    rBtn.addActionListener(this);
    sBtn.addActionListener(this);
    tBtn.addActionListener(this);
    uBtn.addActionListener(this);
    vBtn.addActionListener(this);
    wBtn.addActionListener(this);
    xBtn.addActionListener(this);
    yBtn.addActionListener(this);
    zBtn.addActionListener(this);
    delBtn.addActionListener(this);
    retBtn.addActionListener(this);
    decBtn.addActionListener(this);
    oneBtn.addActionListener(this);
    twoBtn.addActionListener(this);
    threeBtn.addActionListener(this);
    fourBtn.addActionListener(this);
    fiveBtn.addActionListener(this);
    sixBtn.addActionListener(this);
    sevenBtn.addActionListener(this);
    eightBtn.addActionListener(this);
    nineBtn.addActionListener(this);
    zeroBtn.addActionListener(this);


    Can someone explain how I can shorten this, also can you explain and not give me a link because I understand methods and classes could be used to avoid repeated code, but I just don't know how.
    Thanks a lot, your help is much appreciated.

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

    Default Re: repeated code help

    Again, I recommend that you use arrays. I've given you an example of this in your other thread. If any of the code that I posted is confusing, please feel free to ask questions about it, but better in that thread since that is where my code resides.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: repeated code help

    how would I use other classes to shorten it?

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

    Default Re: repeated code help

    Quote Originally Posted by stuckonjava View Post
    how would I use other classes to shorten it?
    I'm not sure what you mean by "other classes". If you mean my example, I would advise you to read and study my code example, ask any questions about concepts in the code that confuse you, and then use the concepts to help you set up arrays that will allow you to compress your code.

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: repeated code help

    I understood you way fine, but I want to also know how it can be done through for eg, polymorphism using another class .

    Or using methods

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

    Default Re: repeated code help

    This doesn't appear to be an issue that requires inheritance or even should be approached in this way. In other words, I don't think that this is a problem best solved through polymorphism.

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: repeated code help

    Is there any other way to shorten without arrays?

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

    Default Re: repeated code help

    You could use collections such as ArrayLists or HashMaps, but this is just a generalization of the solution provided by arrays. You could perhaps use some type of XML set-up or configuration file that holds the GUI's component names and types, although I've never done this before.

  9. #9
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: repeated code help

    Ok, I think arrays is the best way than. Could you give me an example how I can ad my buttons to the actiona listener through an array. The numbers are fine but the alphabet I'm not sure of.

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

    Default Re: repeated code help

    Quote Originally Posted by stuckonjava View Post
    Ok, I think arrays is the best way than. Could you give me an example how I can ad my buttons to the actiona listener through an array. The numbers are fine but the alphabet I'm not sure of.
    An example can be found here. It doesn't matter if the Strings are numeric or letters. Another possible way is to create an array of char: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".asCharArray() and iterate through the array using a for loop.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: repeated code help

    I did a full qwerty keyboard once and got tremendous help in this thread. b.t.w. I put all repeating stuff in a simple properties file (easy to adjust, localize etc)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Use methods to perform the repeated statements
    By Hypnos in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 01:37 AM
  2. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  3. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 03:08 PM
  4. Stoping repeated entries in JComboBox
    By MasterDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 04-05-2009, 12:07 PM
  5. Remove repeated code
    By FraggleBoDiddly in forum New To Java
    Replies: 6
    Last Post: 10-26-2008, 02:28 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
  •