0

i have learnt core java and just started working with netbeans,but i am stuck at a point when i was trying to add the components like buttons,labels etc. at the run time in my project.I searched for it on google but the examples which i studied include some extra overhead of using panels in them,,,,but why can't i create the components at run time as i was creating them in the simple editor like notepad as follows

JButton b4=new JButton("ok");
add b4;

its not working.

nobalG
  • 4,544
  • 3
  • 34
  • 72
  • Which class have you subclassed? Do you get an error message? Can you add more code? – micha Jan 04 '13 at 18:28
  • [Here](http://stackoverflow.com/questions/14030124/how-to-dynamically-add-jlabels-to-jpanel/14031877#14031877) is another similar question and my answer, although its not using NetBeans GUI builder but I wouldnt suggest that if you really want to learn java, it does show you the correct logic needed to add components dynamically. – David Kroukamp Jan 04 '13 at 18:54
  • @DavidKroukamp: why not to do that? i was wandering that netbeans is going to ease my work now,as i jumped from a simple DUMB editor to this brilliant IDE.. :D – nobalG Jan 04 '13 at 19:04
  • 1
    @t3n The IDE is still dumb, it just adds some nice features that will make your index easier, but at runtime, the IDE is irrelevant – MadProgrammer Jan 04 '13 at 20:30
  • @MadProgrammer: doing in that simple editor,i don't have to use any panels...but every simple example i studied over the internet for adding componenets at runtime which uses netbeans,i noticed they have used panels, – nobalG Jan 04 '13 at 21:05
  • 1
    @t3n In the Netbeans designer, what are you adding you components to?? You MUST add them to a container of some sort – MadProgrammer Jan 04 '13 at 23:13

1 Answers1

0

To add Swing framework elements at runtime, you need to have a JFrame to add the elements to. A JFrame is just a window, like any other window you use (and just like NetBeans has), and it has a method called add(Component comp). The parameter is whichever Swing or AWT component you want to add to the JFrame. Here is some sample code to get you started:

// This line creates a new window to display the UI elements:
JFrame window = new JFrame("Window title goes here");

// Set a size for the window:
window.setSize(600, 400);

// Make the entire program close when the window closes:
// (Prevents unintentional background running)
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// This makes it so we can position elements freely:
window.setLayout(null);

// Create a new button:
JButton b4 = new JButton("ok");

// Set the location and size of the button:
b4.setLocation(10, 10);
b4.setSize(100, 26);

// Add the button to the window:
window.add(b4);

// Make the window visible (this is the only way to show the window):
window.setVisible(true);

Hopefully, this helps you out! I remember when I started Java, and I would highly recommend getting as good as possible at non-GUI related stuff first, but if you are ready for Swing, then the code above should work. Best of luck!

  • not, please to read comment, read linked thread, run those code, then to amend this post or delete that (no action by my side, acceptiong your 21points) – mKorbel Jan 04 '13 at 18:49
  • @mKorbel I apologize. Could you clarify? –  Jan 04 '13 at 18:59
  • @mKorbel:same from my side....clarifications please – nobalG Jan 04 '13 at 19:08
  • @t3n Did my answer make sense, or should I add to it for further explanation? –  Jan 04 '13 at 19:12
  • nopes you have explained your part well,but i was asking about adding components at runtime in netbeans IDE.. :)...that netbean thing you missed..:) – nobalG Jan 04 '13 at 19:16
  • @t3n at runtime, the development environment is irrelevant and you are required to simply use the available Java/Swing libraries – MadProgrammer Jan 04 '13 at 20:28
  • [here is answer to your question, both](http://stackoverflow.com/questions/6988317/dynamically-add-components-to-a-jdialog), all JComponents are managed by LayoutManager, but every don't know that you add a new Object, have to call (re)validate a repaint for JComponent that is already visible, there is another way hide a show container, but don't works for all LayoutManagers, don't do it don't to use this dirty hack.... – mKorbel Jan 04 '13 at 21:46
  • @mKorbel Are you talking about the line `window.setLayout(null);` when you say "dirty hack"? –  Jan 04 '13 at 21:52
  • not `AbsoluteLayout` doesn't know about added `JComponents` on `Runtime` somehow too, have to call for `revalidate` & `repaint` too :-), don't to search for shortcut, you'll create only throubles – mKorbel Jan 04 '13 at 21:56
  • @mKorbel I just ran this code and it works just fine without a call to revalidate or repaint. If we were playing directly with the JFrame's graphics context, then we should call `repaint()`, but Swing handles the redrawing of the UI for us when we run `window.setVisible(true);`. And revalidate is unnecessary since we are not adding the JButton to the root pane of the window, but directly into the window itself. –  Jan 04 '13 at 22:07
  • valid for Java6 and minor version, in Java7 is revalidate added to JFrames ContentPane too – mKorbel Jan 04 '13 at 22:57
  • @mKorbel: GOT it from the reference given by you,but can we add the components dynamically without using the panels to the frame similarly??? – nobalG Jan 05 '13 at 00:41
  • this is code example, btw both are containers, JPanel is better, but there in implemented FlowLayout – mKorbel Jan 05 '13 at 06:38