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
Post a Comment