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
548
votes
9 answers

The Use of Multiple JFrames: Good or Bad Practice?

I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI. I'm just wondering whether it is good practice to use…
Peddler
  • 6,045
  • 4
  • 18
  • 22
292
votes
16 answers

How to programmatically close a JFrame?

What's the correct way to get a JFrame to close, the same as if the user had hit the X close button, or pressed Alt + F4 (on Windows)? I have my default close operation set the way I want, via: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); It…
JustJeff
  • 12,640
  • 5
  • 49
  • 63
237
votes
12 answers

How to set JFrame to appear centered, regardless of monitor resolution?

While working with Java, I find it hard to position my main window in the center of the screen when I start the application. Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal…
AmateurProgrammer
  • 2,609
  • 3
  • 19
  • 14
127
votes
8 answers

How to change JFrame icon

I have a JFrame that displays a Java icon on the title bar (left corner). I want to change that icon to my custom icon. How should I do it?
Anand
  • 10,310
  • 24
  • 90
  • 135
111
votes
5 answers

Java: Difference between the setPreferredSize() and setSize() methods in components

What is the main difference between setSize() and setPreferredSize(). Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other. What call should I use for JFrames and JPanels?
David Robles
  • 9,477
  • 8
  • 37
  • 47
103
votes
6 answers

How to capture a JFrame's close button click event?

I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked. How can I capture that event? I'd also like to prevent the window from closing if the user chooses not to proceed.
alxcyl
  • 2,722
  • 7
  • 31
  • 47
86
votes
4 answers

Java/Swing: Obtain Window/JFrame from inside a JPanel

How can I get the JFrame in which a JPanel is living? My current solution is to ask the panel for it's parent (and so on) until I find a Window: Container parent = this; // this is a JPanel do { parent = parent.getParent(); } while (!(parent…
scravy
  • 11,904
  • 14
  • 72
  • 127
82
votes
12 answers

Unresponsive KeyListener for JFrame

I'm trying to implement a KeyListener for my JFrame. On the constructor, I'm using this code: System.out.println("test"); addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { System.out.println( "tester"); } public void…
Tomek
  • 4,689
  • 15
  • 44
  • 52
70
votes
13 answers

How to make a JFrame Modal in Swing java

I have created one GUI in which I have used a JFrame. How should I make it Modal?
om.
  • 4,159
  • 11
  • 37
  • 36
66
votes
8 answers

JFrame Exit on close Java

I don't get how can I employ this code: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to close the program with the x button.
user983246
  • 1,429
  • 6
  • 19
  • 20
65
votes
1 answer

Swing: Obtain Image of JFrame

How do I obtain a java.awt.Image of a JFrame? I want to obtain a screen shot of a JFrame (for later use within my application). This is presently accomplished using the robot to take a screen shot specifying the coordinates and dimensions of the…
bguiz
  • 27,371
  • 47
  • 154
  • 243
56
votes
7 answers

Java - how do I prevent WindowClosing from actually closing the window

I seem to have the reverse problem to most people. I have the following pretty standard code to see if the user wants to do some saves before closing the window: frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); …
Paul Morrison
  • 1,694
  • 3
  • 20
  • 35
53
votes
2 answers

Close one JFrame without closing another?

I want to display two (or more) JFrames at the same time. When I close one of them (use the default close button), the other frames should still be visible. How can I do that?
Keating
  • 733
  • 2
  • 7
  • 12
50
votes
12 answers

Show JFrame in a specific screen in dual monitor configuration

I have a dual monitor config and I want to run my GUI in a specific monitor if it is found. I tried to create my JFrame window passing a GraphicConfiguration object of my screen device, but it doesn't work, frame still display on the main…
blow
  • 12,811
  • 24
  • 75
  • 112
47
votes
3 answers

Difference between JPanel, JFrame, JComponent, and JApplet

I'm making a physics simulator for fun and I was looking up graphics tutorials when I tried to figure out the difference between all these J's. Can somebody elaborate on them or perhaps provide a link to a helpful source?
Andyroo
  • 471
  • 1
  • 5
  • 3
1
2 3
99 100