Create and show a jRadioButton when I push a button - java

I'm writing a code that, when I push a jButton, create and show in a jPannel a jRadioButton. In NetBeans I have writted, in the action method of the jButton, this part of code:
javax.swing.JRadioButton birdButton = new javax.swing.JRadioButton("ciao");
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand("ciao");
birdButton.setSelected(true);
jPanel1.add(birdButton);
jPanel1.revalidate();
jPanel1.repaint();
but, when I push the button the jRadioButton doesn't appear. The jPanel1 there is. What's the trouble? Thanks.

when you drag and drop jpanel to jframe in netbeans from visual component section,they set your panel layout to somelayout for example group layout
so when you add component you will not see it probably because there is different way to add components to different layout .not just .add() .you should add appropriate layout according to your expected design.
for example if you set layout to flowlayout you will see the jradiobuton as you expect.
this is how you can set layout to a component in netbean ide.

Related

Java GUI. how to let Jpanel repaint without changing existing layout

I am working on a JAVA Gui development.
I have a Jpanel, and I can add a Jlabel to it and drag it to somewhere else inside the panel.
Then I can create a new Jlabel, and so on.
The problem I can't solve is that when a new JLabel is created and added to the panel, and the panel call repaint(). The existing labels and the new JLabel are lined up.
I want the existing labels to stay in their locations when a new label is added.
Any advice?
Thank you!
To position components at a random location you need to either:
use a null layout (not recommended)
use a custom layout manager
Check out the Drag Layout. It is a custom layout manager designed to provide layout management support while letting you manage the location of each component.

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

Enclose In option greyed out in Netbeans GUI builder

Building a Java GUI application using Netbeans IDE. I have created a JPanel in it. After adding a lot of fields, I want to also add a JscrollPane to it. I have found that if we right click on the pane we have an option to Enclose it in a container that can be Scroll pane or Split pane etc.
The problem is, that the enclosed button is greyed out when I right click on the panel. How should i fix this ?
I had the same issue. After experimentation, it appears you can only use Enclose In if the Layout is set to Free Design.
However, an alternative method I discovered to accomplish the same thing is to create the empty JPanel and the components you want inside it, all at the same level in the TopComponent. Then drag and drop the components you want into the JPanel, using the Navigator window that shows the component hierarchy.
For example, here I've created a JPanel and a JRadioButton as siblings, and then I drag and drop the JRadioButton inside the JPanel, so that the JPanel is the parent.
Result:

JFrame window with sub components

So I have created this JFrame window which has a lot of items like JLabel, JComboBox JTextField etc… At the bottom it has a "next" JButton.
I want that when a user clicks the next button, everything on the screen should be removed and replaced with stuffs from other class that I have created.
I only manage to open a new JFrame window whenever I click the next button. Can somebody please tell me how to remove all items from the screen and replace them with items from another class.
Thanks. I am a newbie so please give me the easiest way possible.
This sounds like a job for CardLayout
You could create a base panel in the BorderLayout.SOUTH position of your JFrame that would have your navigation buttons and have a number of panels added to your main panel being managed by CardLayout.
See Creating Wizard Dialogs with Java Swing
While the systematic thing for it is using CardLayout, you can imitate it if you don't want to learn how to use it!!
Create a Panel, add all items except the next button to this panel. Use BorderLayout to put the panel on top of the next button in the frame.
Now when the user press the next button you remove the panel (jframe.remove(panel)). create a new JPanel and add it using the BorderLayout again on top of the next button.

Is it possible to have 2 JPanels in a Border layout at the same location?

I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER. What I'd like to be able to do is sometimes hide this panel and replace it with another panel with different information. I added both to the container and set visibility of one of them to false.
Then later I try:
panel1.setVisible(false);
panel2.setVisible(true);
but this doesn't display the new panel. I just see gray. Any ideas?
TIA
Use a nested JPanel with a CardLayout for that.

Categories