How to set JScrollPane to invisible? - java

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.

Related

focusing on a textfield in a panel which is in a tabbed pane that contains many panels

I am working on a barcode application which has in its main GUI a tabbed pane that contains many panels where one of this panels is for the cashier, the problem is that when the cashier panel is displayed i need to always set focus on the textfield where the barcode must be inserted.
requestFocusInWindow() or requestFocus() does not work since I am in the same jframe and in the same jTabbedPane but i am choosing panels inside this tabbedpane
what i need is when i move to a specific panel which is "cashier panel" is always to set focus on a certain textfield
thanks.
private void MainTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
if(CashierPanel.isShowing()){
Barcode_txt.requestFocus();
}
}
This may also work,
when you specifying barcodeText's properties i.e. Height,Width etc set also focusable true as
barcodeText.setFocusable(true);

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.

How do I create a JScrollPane within a JTabbedPane?

I am trying to create a JScrollPane within one of the tabs to my JTabbedPane. I tried what I though would work which was this:
pane.add("Main", mainGame);// These are my other tabs
pane.add("Upgrades", upgradeScreen); //the JTabbedPane
pane.add("Credits", creditsTab);
upgradeScreen.setLayout(null); //The null layout
lblMoney2.setBounds(10, 11, 277, 22);
upgradeScreen.add(lblMoney2); // A simple JLabel
scrollPane.add(upgradeScreen); //my JScrollPane
Where pane is my JTabbedPane and scrollPane is my JScrollPane. This simply got rid of my upgradesScreen tab. I kind of expected this but I did not know what else to do. If more code is needed for you to figure it out, tell me and i'll put it in, otherwise, thanks for the help!
Don't us JScrollPane#add, instead you want to use JScrollPane#setViewportView
Check out How to use ScrollPane more details.
Advise- Don't use null layouts, they limit the ability for your application to run on multiple platforms. Instead take the time to learn how layout managers work
This simply got rid of my upgradesScreen tab.'
yes, because no component can have two parents at once. You added upgradeScreen to JTabbedPane first and then again added it to a JScrollPane. The Component's add(component) function will eventually call the addImpl(component) function: which will remove the component from it's old parent and add it to the new parent.
However:
You need to add the JScrollPane to the JTabbedPane instance.
The component which you wish to scroll set it as a view to JScrollPane using the setViewportView(component) function. for your context it is the upgradeScreen

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().

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.

Categories