dynamicly create JButtons

Posted on 2006-05-22
Last Modified: 2012-06-27

For a UI I need to create a row of JButtons. At times this row will need to be 10 JButtons long, at other times, in may need to be 28 JButtons long. I do not want to hard code all these JButtons, is there a way to create them in a for loop dynamicly?


Question by:waffe
    LVL 86

    Accepted Solution

    /* ( JButton[] ) */ buttons = new JButton[NUM_REQUIRED];
    for(int i = 0;i < NUM_REQUIRED;i++) {
          buttons[i] = new JButton("Button " + (i + 1));

    Author Comment



    LVL 86

    Expert Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 69
    MaxSpan challenge 9 52
    scoresIncreasing challenge 10 49
    Core Java. What output will be and why ? 6 41
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now