Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Unhappy Need help with JButton


    hi i am trying to make a method where it will make a new JButton(i want this method to be able to make multi JButtons because i need to make over 200 of them and they all need to do the same thing) and can set the symbol that it is called by, set an action command and set the action listener class but i can get it to work.
    Code:
    public addNewButton(String text, String name)
        {
            JButton j = new JButton(text);
            j.setName(name);
            j.addActionListener(new R4());
            j.setActionCommand(text);
            j.setVisible(false);
            new Recipe(text);
        }
    the new Recipe(text) works fine so that doesn't need to be fixed.
    then i want to add it to a list to be called to setVisible(true);
    Code:
    i don't know how to do this
    here is an example of what the method looks like when i call it
    Code:
    new addNewButton("CobbleStone", "Block.cobblestone");
    then i want to add it to a TabbedPane
    Code:
    panel4.add(CobbleStone);
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    A couple of things.

    Your method doesn't list a return type.

    Also you don't have a return statement.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    what the method looks like when i call it
    That call looks like a constructor, not a method. new is used when you create a new instance of a class.

    The new statement returns a reference to the newly created object. Your code should save that reference in a variable.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    That call looks like a constructor, not a method. new is used when you create a new instance of a class.

    The new statement returns a reference to the newly created object. Your code should save that reference in a variable.
    oh ok so like:
    Code:
    protected static JComponent addNewButton(String text, String name)
        {
            JButton j = new JButton("test");
            j.setName(name);
            j.addActionListener(new R4());
            j.setActionCommand(text);
            return new JButton(text);
        }
    also how can i add buttons to a group so i can just type one line to set them visible or not visible?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    I GOT THE METHOD TO WORK!
    i got it to add a new button but now i still need help with putting them in a group. so i can add them all at one time and set them visible with one line like:
    Code:
    panel4.add(<buttongroup>);
    or
    Code:
    <buttongroup>.setVisible(true);
    Code:
    <buttongruop>.setVisible(false);
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    add them all at one time
    I've never seen a method that does that. I always use a loop.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    I always use a loop.
    umm explain please
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    define an array that holds the components
    begin loop through the array
    get next component from array
    do whatever with the component
    end loop
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    define an array that holds the components
    begin loop through the array
    get next component from array
    do whatever with the component
    end loop
    can i have an example code please?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Code:
    define an array that holds the components:  Component[] compAry = {comp1, comp2};
    begin loop through the array:  for(int i=0; i<compAry.length; i++) {
    get next component from array :    Component aComp = compAry[i];
    do whatever with the component:  aComp.someCompMethod();
    end loop:                        }
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    Code:
    define an array that holds the components:  Component[] compAry = {comp1, comp2};
    begin loop through the array:  for(int i=0; i<compAry.length; i++) {
    get next component from array :    Component aComp = compAry[i];
    do whatever with the component:  aComp.someCompMethod();
    end loop:                        }
    how can that add them to a TabbedPane?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What is the code to add a component to a TabbedPane?

    Do you have a program that you are working on?
    Last edited by NormR; December 19th, 2012 at 06:28 PM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    What is the code to add a component to a TabbedPane?

    Do you have a program that you are working on?
    yes but if i posted my code i would feel bad because its a mess. if it is needed i will post it though.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0
    JButton

    public JButton()

    Creates a button with no set text or icon.

    JButton

    public JButton(Icon icon)

    Creates a button with an icon.

    Parameters:
    icon - the Icon image to display on the button
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by jhon marvi
    JButton

    public JButton()

    Creates a button with no set text or icon.

    JButton

    public JButton(Icon icon)

    Creates a button with an icon.

    Parameters:
    icon - the Icon image to display on the button
    i never said i didn't know how to make a JButton. please read more next time.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo