Results 1 to 11 of 11

Thread: which is better

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default which is better

    Hello,

    If I want to make a calculator, which is better to use

    Java Code:
    for (int i = 1; i <= 9; i++)
    {
       buttonPanel.add(new JButton(i));
    }
    or to create a JButton object for each number and put them in the panel.

    which saves memory and time?

    Thanks in advance

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    I think its better to create a separate button for each number.

    Not because it is more efficient, but for the sake of simplicity.

    As you would be required to add an ActionListener for each button, your current approach could pose problems then.

    The approach I am suggesting will add more lines of code, but will give you a better control over your program

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Might want a button for 0, though.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lovelesh View Post
    I think its better to create a separate button for each number.

    Not because it is more efficient, but for the sake of simplicity.

    As you would be required to add an ActionListener for each button, your current approach could pose problems then.

    The approach I am suggesting will add more lines of code, but will give you a better control over your program
    Ignoring the fact that there is no JButton(int) constructor I don't see why doing this in a loop would cause (more) problems for a separate ActionListeners to be added to each button.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    I was able to do it with an array of buttons and a loop to create them, put them in the right spot and add a listener to them each. The listener took an argument which specified the number and used that to set the field.

  6. #6
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    @lovelesh: I though so too, so that the problem would be more clear, but my friend told me its better to use the loop, thats why i asked the question
    as for the action listener i think still it might work i can use the action event object "getActionCommand()" method to see which number in write it

    @Iron Lion: Sure there is 0, but i use from 1 to 9 inclusive since they usually come after each other, and the 0 with the star and point alone

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    another question, in a grid layout is there a way for me to control the position in which a jcomponent is added, or is there a layout that makes the layout same as grid one but with me able to control the positions of the jcomponents?

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

    Default

    Grid bag layout is like grid layout with a lot more control. It is pretty complex to use though.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    in a grid layout is there a way for me to control the position in which a jcomponent is added
    There's an overload of add(...) that takes a Coponent and an int index.

    db

  10. #10
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Ahaa, I'll see them now and use them
    thanks guys for helps

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

    Default

    You are welcome. Please mark your thread solved with the thread tools at the top.

Posting Permissions

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