• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to dynamically add Jbuttons created as array to a jpanel  RSS feed

 
bhuvanesh kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i want to know how to add jbuttons dynamically which i have created as array,i want to know how to dynamically add actionlisteners for it??

it's like
Jbutton buttons[];


while starting the jpanel,i mean initialaising jpanel
i want to create buttons like
buttons=new Jbutton[buttonsCount];
where buttonsCount will be fetched from database
help me how to addActionlisteners for all these buttons dynamically.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are severe issues involved here, and it would help most to know just what you are stuck on. For instance the accepting container (here a JPanel) must use an appropriate layout manager so that it is able to accept the added buttons properly and display them. Since you're adding an array of buttons, a GridLayout comes to mind. Next, if you're adding the buttons to a panel that is already displayed, you'll need to call revalidate() on the JPanel after adding the new components so that its layout manager will layout the buttons and display them. Next, to initialize and create an array of buttons, you'll likely use a for loop and within the loop, create a new JButton and give it either an Action object or ActionListener object.

Your best bet here is to show us what you've tried (the code), and give us a paragraph or two on what it is that is currently not working for you.
 
bhuvanesh kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find much of what you need to learn in the Java Swing tutorials. For instance please check these out for starters:
How to Use Buttons, Check Boxes, and Radio Buttons
How to Write an Action Listener
Laying Out Components Within a Container
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see that you've added a bit of code. Here's a very simple example demo-ing what I've suggested. Please have a look, and hope it helps:
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhuvanesh,

Please read your private messages.

Rob
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!