开发者

Can i add a component in jframe in netbeans at runtime?

I am having a form built up in netbeans and开发者_StackOverflow中文版 want to add or remove a component with an actionperformed event of a button or a combobox is it possible?

if yes, how?


You can add components at run time, but you have to call paint() method of jframe to show the added component.


Create a JPanel where you want to add dynamic components and then use add/remove and setLayout() methods to control components on it.


The general code for adding components at runtime is:

panel.add( someComponent );
panel.revalidate();
panel.repaint();

However, I believe NetBeans uses the GroupLayout which will cause a problem. You need to understand how all the constraints work and then specify the proper constraints when using the add(...) method.

So my suggestion is to NOT use NetBeans to design your form and to learn to use LayoutManagers on your own, then you will be in full control of the layout and adding components will be as easy as the code above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜