java - Creating JButtons dynamically -


hello have set up

private jbutton btnfoo, btnbar; 

and need each button following

        btnfoo = new jbutton("foo");     btnfoo.addactionlistener(this);     add(btnfoo); 

is possible in java create dynamically each button declare? because when have 5 buttons don't want 3x5 = 15 lines of code few lines dynamically created buttons.

write little loop , store buttons in array:

private jbutton buttons[] = new jbutton[5];  string names[] = {"foo", "bar", "baz", "fob", "bao"}; (int = 0; < buttons.length; ++i) {     jbutton btn = new jbutton(names[i]);     btn.addactionlistener(this);     add(btn);     buttons[i] = btn; } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -