Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Create buttons at runtime

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create buttons at runtime

    Hello all I am trying to read for example 4 html files from a directory then at runtime the buttons are created (this is important as at runtime there could be 5 html files etc) which will correspond to each file so when button 1 is pressed html file 1 is loaded etc. Any ideas anyone? thanks.

    Oh on a side note I am poor at creating gui's so i used netbeans to create my GUI but i am unable to add my own code to then create buttons at runtime.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Create buttons at runtime

    Quote Originally Posted by rtumatt View Post
    Hello all I am trying to read for example 4 html files from a directory then at runtime the buttons are created (this is important as at runtime there could be 5 html files etc) which will correspond to each file so when button 1 is pressed html file 1 is loaded etc. Any ideas anyone? thanks.

    Oh on a side note I am poor at creating gui's so i used netbeans to create my GUI but i am unable to add my own code to then create buttons at runtime.
    Create the buttons based upon the number of html files in a for loop. If you need to keep references then store them in a List.
    Object[] htmlFiles;
    //load html files
    JButton[] buttons = new JButton[htmlFiles.length];
    for ( int i = 0; i < buttons.length; i++ ){
    buttons[i] = new JButton();
    //add button to gui
    }


    If it were me I'd ditch the buttons and use a JComboBox, from there you can add the items dynamically and have the GUI look a but more fluid - in this case you can use NetBeans to arrange your combo box in the GUI, then dynamically add the items to the JComboBox reference
    Object[] htmlFiles;
    //load html files
    for ( int i = 0; i < htmlFiles.length; i++ ){
        comboBox.addItem(htmlFiles[i]);
    }

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create buttons at runtime

    That helped me out more than you can imagine, thankyou! i have the problem of the positioning of the buttons now all being ontop of each other :S

Similar Threads

  1. create buttons in a JEditorPane
    By seeker in forum AWT / Java Swing
    Replies: 4
    Last Post: December 5th, 2009, 08:01 AM
  2. JRadio buttons
    By chronoz13 in forum AWT / Java Swing
    Replies: 0
    Last Post: November 29th, 2009, 12:08 AM
  3. How to Navigate to a URL with Runtime.getRuntime().exec()
    By Flash in forum Java SE API Tutorials
    Replies: 4
    Last Post: October 5th, 2009, 07:18 PM
  4. Need more buttons!
    By GotWankel? in forum AWT / Java Swing
    Replies: 0
    Last Post: October 5th, 2009, 01:08 AM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM