Difference from JFrame and JPanel object int Java Swing? - java

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.

Related

Putting JPanel on JFrame of any use?

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.

Make a mainWindow for JAVA/SWING application

When i started developping my application, I just developped Seprate JFrame frames and test them one by one. Now, i want to make a main window for my app. I read a lot, but until now, it's difficult for me to do this in java and swing. I tried this by creating a main window as an instance of JFrame, but i got errors that shows i can't show JFrame inside another JFrame.
public class MainWindow extends JFrame{
private JFrame frame1;
private JFrame frame2;
public MainWindow(){
frame1 = new JFrame();
frame2 = new JFrame();
setLayout(new BorderLayout());
add(frame1,BorderLayout.CENTER);
add(frame2,BorderLayout.NORTH);
pack();
}
}
This is one of the reasons why it is generally discouraged to extend directly from top level containers like JFrame, they lock you into a single use.
You can't add window based components to other containers. You will have to separate each of your current frames into a more basic container, like JPanel, only then can you add them to another window.
You may consider using a CardLayout or JTabbedPane or even a JDesktopPane or other layout manager to make your individual views available to your users depending on your needs.
See...
How to Use CardLayout
How to Use Tabbed Panes
How to Use Internal Frames
Laying Out Components Within a Container
for some more ideas
A JFrame is a window.
You can't put a window inside a window.
You might be looking for JPanel. A JPanel is a fairly simple container for other components (which could include more JPanels). You can add JPanels to a JFrame.
There are different ways to handle this
First of all you can't use Jframe inside of JFrame like you noticed so instead use JPanel
panel = new JPanel();
add(panel,BorderLayout.Center);
JPanel is an other container in which you can pack a lot of swing components.
If you want to open a new Window make anoter JFrame and make it visible. Note you can't add it to your Jframe with the method add. but you can save it in a variable and handle what you see in the other frame from the same class.
As you wanna have a mainwindow add this code into the mainwindow:
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this makes that if you close your main Frame the application will end (in the other frames you shouldn't implement this normally)

Clarify the difference and connection of canvas, frame, pane and panel

I'm quite confused and I when I try to find an answer with google I get bombarded with tutorials.
What is the purpose of each one?
How are they connected?
What is the purpose of the connection?
In java canvas is area used to draw something by java graphics. For ex. drawing an image or rectangle.
Frame is used as JFrame(swing), a top level container which can contain canvas, panels, pane(DesktopPane, ScrollPane) etc..
Panel or JPanel is a subcontainer used to contain textboxes, buttons, canvas etc.
Jframe can contain multiple panels, but panel can't contain JFrame.
Textboxes, buttons can directly be added to Jframe but it decreases flexibility, Suppose we want to hide a set of buttons from ui, then we need to hide them one by one from JFrame. If those text boxes are added to panel then we just need to hide that panel only. There are so many cases about using panel in jframe.

Sliding hidden sections of a JPanel into view

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.

How to make a JFrame blurred in java?

In my java program, when the user clicks on a button in the main JFrame another JFrame becomes visible on top of the main frame. Now how to make the previous frame blurred?
Exactly like the background in the image :
Now how to make the previous frame blurred?
have look at JLayer(Java7) based on JXLayer(Java6),
similair effect is possible with GlassPane, or with JViewport (not proper of ways, but good Swing GUI contains JScrollPane as 1st. JComponent in JFrame / JDialog / JWindow)
I'm assuming that you are coding using Java Swing, and the picture is the effect you want.
You would use a JDialog to create the top window, rather than a JFrame. A JDialog is designed to be modal. In other words, lock the JFrame until the JDialog is closed.
The default behavior of a JDialog is to not blur or darken the underlying JFrame. In order to darken the underlying JFrame, you're going to have to paint a black image on the glass pane of the JFrame.
This tutorial shows you how to manipulate the root panes of a JFrame.
There is the setOpacity(float opacity) method that is available in JFrame (inherited from Frame actually).

Categories

Resources