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.
Re: Create buttons at runtime
Quote:
Originally Posted by
rtumatt
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.
Code :
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
Code :
Object[] htmlFiles;
//load html files
for ( int i = 0; i < htmlFiles.length; i++ ){
comboBox.addItem(htmlFiles[i]);
}
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