Questions tagged [jframe]

A JFrame is a component and top-level container of the JFC/Swing framework.

A JFrame is a component and top-level container of the JFC/Swing framework. A JFrame is a Frame element in Java Swing but is slightly incompatible with Frame. It is normally the outermost component, has a Frame and a title, and is usually filled with menu and JPanel(s) which contain(s) further components.

JFrame is heavyweight, since it's based on creating a "heavyweight" AWT window. Lightweight components can replace internal widgets with java-based implementation that doesn't require the use of JNI (Java Native Interface), but windows are the special case. JFrame does let you do custom rendering, via it's heavy window. Also, if you're using other lightweight stuff, all of them will be added to the contentPane. Using JFrame makes the rendering more efficient overall than mixing light and heavy components. JFrame itself is a top-level container and contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case.

JFrame contains several layers. The main layer where all Swing components are added is the content pane:

frame.getContentPane().add(new JButton("Ok"), BorderLayout.CENTER);

As a convenience, the add method and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write

frame.add(new JButton("OK"), BorderLayout.CENTER);

and the JButton will be added to the contentPane.

If you are adding components just to the JFrame itself, you are actually adding them to the content pane (same about removal, etc).

On the top of it, there is glass pane component (it is not a container). It is painted on the top of all components in the content pane. It is invisible by default you need to call setVisible(true) to show it:

JFrame frame = new JFrame();    
frame.getContentPane().add(new JButton("Ok"), BorderLayout.CENTER);
frame.setGlassPane(new JLabel("-------------------------"));
frame.getGlassPane().setVisible(true);
frame.setSize(100,100);
frame.setVisible(true);

Screen shot, demonstrating the use of JFrame

The size and location of JFrame are specified in screen coordinates.

Reference: Class JFrame

9961 questions
2
votes
2 answers

java JFrame graphics

I have the following simple code in a JFrame constructor super(name); setBounds(0,0,1100,750); setLayout(null); setVisible(true); g = this.getGraphics(); int[] x =new int[]{65, 122, 77, 20, }; int[] y =new…
aditya parikh
  • 595
  • 4
  • 11
  • 30
2
votes
2 answers

How to close a previous Frame when a new Frame is opened

I am coding MVC. In my controller i am listening to the action and if the user clicks a "SHOW-LIST" button i am making an instance of my view class which extends from JFrame and therein the view class i create a new JFrame. My problem is that if i…
mu_sa
  • 2,685
  • 10
  • 39
  • 58
2
votes
2 answers

How to set the default icon in a JFrame?

So I was making Java, and made a nice little program. Here's the code: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import…
user1766728
2
votes
2 answers

How to add a class that extends JPanel to JFrame?

For my assignment, I am given this piece of code: // This class/method uses a global variable that MUST be set before calling/using // note: You can not call the paint routine directly, it is called when frame/window is shown // look up the…
stepup.stepout
  • 61
  • 1
  • 1
  • 9
2
votes
2 answers

Dynamically updating markers in JMapViewer

Hello Stack Overflow community, I am a Java newbie and I am doing a simple java project where I take coordinates (lat and lon) from a (dynamic) source and use JMapViewer (Yes, not JXMapViewer) to display the markers on a map. I have put all the…
Kale Bamman
  • 25
  • 1
  • 5
2
votes
4 answers

How to create customize title bar with close button on jFrame?

I want to create a customised title bar for my JFrame. I can remove the default title bar with JFrame.setUndecorated(true) Now i need to create a customised title bar for my JFrame with a close button?
boopathy
  • 427
  • 2
  • 9
  • 20
2
votes
1 answer

Set the visibility of components within a transparent JFrame

Question: To hide a JPanel which has been added to a transparent JFrame when the button is clicked. Problem: The JPanel is not correctly hidden, but is still shown with darker colour. Without the alpha channel enabled, it hides ok. Thanks for your…
Jon
  • 193
  • 2
  • 10
2
votes
1 answer

Multiple JFrames for Game scenario

I'm sorry if the title is worded a bit incorrectly. I have recently started a new Game project, and have thought about the use of multiple JFrames in Java. The reason being is my game has 2 JFrames: 1) Main Menu ( which is a singleton) 2)…
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
2
votes
1 answer

Pop Up Text Field Java

I have a button and on click I want it to load a text field (like a pop up). Is this possible? I've looked into this.setVisible(false); new className().setVisible(true); But this means I'd need a new JFrame. I'm not against the idea - I'm just…
Steven Mclaren
  • 275
  • 2
  • 6
  • 13
2
votes
5 answers

JPanels don't appear in the JFame

I'm new to java. I do have a java class but I want to get ahead. I really like it! This is my problem. I'm trying to draw the two paddles needed for the game. I did create 2 objects for them, and they both "Show" but the following happens: Main…
2
votes
4 answers

Creating a JFrame from another JFrames constructor

I have 3 objects that extend JFrame let's call them FrameA FrameB FrameC. FrameA is my main application window. From FrameA's constructor if the application is not registered I create FrameB and FrameC. They are just popup's that indicate trial…
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
2
votes
1 answer

closing the parent window in swing

Hi I am using JFrame to design swing application on clicking a menu in JFrame my requirement is to add JInternal frame on the JFrame and its working . Now the problem is that i want to refresh the Jframe according to the button clicked on…
adesh
  • 1,157
  • 3
  • 18
  • 28
2
votes
2 answers

Java Swing Blank JFrame coming up?

I'm new to swing, and was wondering why sometimes my application comes up as blank, and sometimes it displays the components. It seems to be sporadic. There are no exceptions thrown or anything like that. It just frequently comes up as a blank…
Jake Byman
  • 540
  • 12
  • 29
2
votes
5 answers

Do something before window closes after user presses [x]

I'm building an instant messenger application.I need to add an action event on the default closing button of the Swing JFrame(The little "x"). When the client presses the X button,I need to tell the server that he goes offline and only after that…
Adrian Stamin
  • 687
  • 2
  • 8
  • 21
2
votes
1 answer

Can any one help Printing a JFrame with all its component in Java program

Hello everyone, Now a days i am facing a serious problem. I have made a java program and one of the JFrame of this program needs to be printed. But i can't do that. I have searched on the web but the code i have found only prints the…
Shimul Das
  • 21
  • 1
1 2 3
99
100