JFrame window with sub components - java

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.

Related

Create and show a jRadioButton when I push a button

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.

How to set JScrollPane to invisible?

I have a JScrollPane that seats inside of a JTabbedPane and I want to set it to invisible when a non admin user is using the program.
I have tried to use the method setVisible(false) but it doesn't work. If I use the same method such as JTabbedPane.setVisible(false), it will hide the JTabbedPane but I don't want to hide the entire JTabbedPane I only need to hide few panels in it.
Any ideas on how to get this done?
Use a CardLayout as shown in this answer. One card would be the blank panel, the other the scroll-pane.

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.

Java multiple GUI windows creation

I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().

Get focus on a JTextField from another panel when a CardLayout changes

hi need help on a Swing application that I am doing. I have a dialog with two panels, the first panel has a CardLayout and the second has a FlowLayout. The first layout has buttons that change the card layout and the buttonclick is entered to a specific textfield on the second panel. Every time the card layout changes, the textfield on the second panel loses its focus. How to get the focus of a specific textfield of the second panel?
when you are clicking on a button to go anohter panel
write the code inside the "actionPerformed" of that button
actionPerformed(ActionListenet al)
{
//code..
textField.requestFocus();//what the textfield u wnat
}
refer this link Setting the focus to a text field

Categories