I'm using NetBeans, and I've a JFrame
where I added a JPanel
to it using the NetBeans's palette.
I want to add a JRadioButton
manually to that JPanel
, so this is the code I tried in the constructor :
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
JPanel1.add(btn1);
But when I run that JFrame
I don't see that JRadioButton
anywhere, but it works when I add it using the NetBens's
palette.
How can I solve this problem ?
revalidate()
and repaint()
on the JPanel after adding a component, if you are adding the component after the GUI has been rendered, such as on a button push.The problem with NetBeans GUI Builder is that it initializes everything for you, where you can't alter the code unless you open the file on some other platform. In which case you have the risk of totally messing up the code.
What I can suggest is to maybe attempt something like this
JPanel
with a preferred size that you set in the property
pane. You may also want to set the layout also, depending on your requirements.After the initComponent()
then add the JRadioButtons
public MyGUI(){
initComponents();
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
jPanel1.add(btn1);
jpanel1.revalidate(); // as @Hovercraft Full Of Eels suggested
jPanel1.repaint();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With