I am working in the NetBeans. For that when I am working in the new JFrame form or the design view if I add the panel and then add the label and rest of the contents, it makes no difference if I don't add a panel and add the contents like JLabel, JButton, etc. it makes no difference.
Is there any reason why panel should be added to the frame? I tried to close the application when the panel was inserted and when it was not inserted on the frame, the application closes both the times. (When I press run and try to close the application, both times it closes.)
Then what is the use of putting JPanel on a JFrame?
When you add components to the frame the components are added to the content pane of the frame which by default is a JPanel. Read the section from the Swing tutorial on Using Top Level Containers for more information.
By creating a separate panel and adding components to that panel you give yourself more flexibility when designing your application. For example you may want to use a CardLayout which allows you to swap out different panels on the frame. The tutorial also has a section on using CardLayout.
Related
How do i have the JPanels in the Mainframe to appear on all other GUI i've created?
Do i have to make the same JPanels in the Mainframe again or is there another way to make it as a fixed layout so i can only edit the content part?
You state in comment:
i'm creating an application with a fixed desgin. So i have everything fixed in the Mainframe (the navigaion bar), So how do i have the navigation bar to appear on other JPanels for other GUI? I only have one JFrame.
I wouldn't have the same navigation bar appear in multiple JPanes, but rather would place my navigationBar in the main JFrame's contentPane in a fixed position, say in the BorderLayout.PAGE_START spot, and then swap my JPanel "views" in the BorderLayout.CENTER position using a CardLayout. This way only one navigationBar need be made, and it would be visible all the time (unless you explicitly choose not to show it).
I know that JFrame is a top level container fro Swing GUI and that I can put a JPanel object inside a JFrame object.
But also JPanel is a container...so what is the difference from JFrame and JPanel.
I see that, in the GUI implementation, is implement directly a JPanel object, other times I see that is implemented a JFrame in which I put a JPanel.
What is the difference?
Tnx
Andrea
JFrame - Used to represent the features a like a window. This includes border,titlebar, different controls, and different event handlers.
refer : http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
JPanel - Most Generic class used as a container to gather other elements together. This is more important with working with the visual layout or one of the provided layout managers.
refer : http://docs.oracle.com/javase/6/docs/api/javax/swing/JPanel.html
EDIT: Some more
JPanel serves as a general purpose container, while JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window.
JPanel represents an area used for more complex operations or applications.
In JPanel, one panel can hold many operations, while in JFrame, it can have inner frames for a different purpose. Read more: Difference Between JPanel and JFrame | Difference Between | JPanel vs JFrame http://www.differencebetween.net/technology/difference-between-jpanel-and-jframe/#ixzz2g0DDAgAq
A JFrame rappresent a window, the jpanel is a part of the gui contained in a window.
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html
The similarity is both are containers.
JFrame is a contrainer which can hold JPanel.
The best use when I had used long days back, when I inherit JPanel
instead JFrame, so latter on, the component can be used in both
JFrame and JApplet.
You can add multiple JPanels inside a JFrame. You can set the
different layouts.
Basically, a JFrame represents a framed window and a JPanel represents some area in which controls (e.g., buttons, checkboxes, and textfields) and visuals (e.g., figures, pictures, and even text) can appear.
JPanel serves as a general purpose container, while JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window.
I want to display a JFrame ( made with the Netbeans GUI Editor ) that has an enclosed panel ( the panel encovers the entire JFrame ). The panel is twice as wide as the frame, so I want it so that when a button is pressed inside of the panel, the panel's visible area slides over ( over about 2 seconds) to the hidden area of the JPanel and the previously visible section of the JPanel becomes invisible. I couldn't find any function how to set the currently visible section of a JPanel, so the function and/or a different solution to this would be helpful.
I suggest that you put the JPanel in a JScrollPane, one that if you wish does not show its scrollbars. Then you could easily use the scrollpane's model and a Swing Timer to create an animation that shows the JPanel sliding.
The solutions is CardLayout based http://java-sl.com/tip_slider.html
You can add 2 (or more) panels into container and rotate them.
So basically, I am trying to get a JPanel window which will display all components inside dynamically. In other words, which will re-size the window, and display to fit its content.
I have been able to do it with help of JFrame and its pack() method which : "causes this Window to be sized to fit the preferred size and layouts of its subcomponents".
In my situation, I dont want to use JFrame because it will require much effort to make all changes.
Right now, I am able to make it work but only with the help of jscroll inside which wraps the text and or any new lines, so the window size is more static. So my JPanel is extending a TopComponent and am able to display it with:
jpanel.open();
jpanel.requestActive();
So the question is how to resize a window to fit its content upon actions in that window.
The JPanel has to be added to a Window in order to make sense. So I suggest you use layout managers correctly and you will get to a decent user interface.
When you add/remove components from a visible panel you need to use:
panel.revalidate();
panel.repaint();
Then the layout manager will lay out the components again.
I have a JFrame.
I also have a Box class which extends Component.
This box class has a paint method which makes a filled rectangle.
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.
I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.
(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).
Thanks in advance!
You have 2 choices.
You can change the layout of your frame:
JFrame frame;
frame.setLayout(new FlowLayout());
Now, if you add more than one box, it will show up on the frame.
The other option is to do what you said you tried. (Adding a panel to the frame)
JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')
Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.
You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.
And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.
You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.
EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.
Do your layout on paper first, then read up on Swing layout managers.
Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.
You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.
Thank you for all your answers.
Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.
I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.
If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.