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
18
votes
4 answers

Run function on JFrame close

void terminate() {} protected JFrame frame = new JFrame(); How can I get frame to run the terminate function when I press the close button? Edit: I tried to run this, but for some reason it doesn't print test (however, the program closes). Does…
Mattias
  • 1,110
  • 1
  • 11
  • 26
18
votes
2 answers

How to layout multiple panels on a jFrame? (java)

I am in the process of making my own java socket game. My game is painting alright to the full screen (where it says "paint graphics here", but im painting to the whole jframe at the moment). I want to add a textbox with a scroll bar for displaying…
Code Doggo
  • 2,146
  • 6
  • 33
  • 58
18
votes
3 answers

JFrame On Close Operation

I was wondering if there is a way, by clicking on the "X", to let the program perform some code before closing the JFrame. The setDefaultCloseOperation() method takes only an integer. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The JAVA Noob
  • 367
  • 4
  • 8
  • 21
17
votes
6 answers

Is it possible to bring JFrame to front but NOT focus?

I'm writing a Java app (Swing GUI) that periodically pops up a JFrame. Is it possible somehow to bring the window to front (foo.setAlwaysOnTop(true) would be even better) but without having it focus? Some people move their eyes away from the screen…
Johann Fridriksson
  • 173
  • 1
  • 1
  • 6
17
votes
4 answers

How to use KeyListener with JFrame?

So, I was trying to make a rectangle move with a KeyEvent (KeyListener) and whenever I try to hit the key, the rectangle doesn't move. The rectangle is drawn, but whenever I hit the left and right keys, nothing happens. I have two classes, one is my…
user3183679
  • 245
  • 1
  • 4
  • 8
17
votes
2 answers

How to open a new window by clicking a button

As a part of my program, I need to have a button that when the user click on it, it opens a new window. Well I guess I should have a class that make the frame and call it by the button. but I don't have any idea to start. I just got my button in the…
Alex Jj
  • 1,343
  • 10
  • 19
  • 30
17
votes
4 answers

JavaFX 2.2 Stage always on top

I have a normal JFrame (one part of the app) and a second JavaFX window (I can't use a JFrame for the JavaFX stage). The problem is that the JavaFX window should always be on top of all other windows. I have no idea how I should solve this problem!…
user1706051
  • 221
  • 1
  • 2
  • 6
16
votes
6 answers

How do you open web pages in Java?

Is there a simple way to open a web page within a GUI's JPanel? If not, how do you open a web page with the computer's default web browser? I am hoping for something that I can do with under 20 lines of code, and at most would need to create one…
GA Tech Mike
  • 163
  • 1
  • 1
  • 5
16
votes
6 answers

Building a GUI for a Sudoku Solver (Complete with ASCII Example)

. OVERVIEW, SAMPLE Hello everyone, I have created a basic Sudoku solver that can solve most problems fairly quickly. I still have a lot of work ahead of me to make it solve even the hardest problems, but I'd like to try to implement a basic JFrame…
Justian Meyer
  • 3,623
  • 9
  • 35
  • 53
16
votes
5 answers

Center Swing Windows

I'm developing a Java Swing application. How can I make it so that when the program itself and any other windows open they come up in the center of the screen?
Travis
  • 161
  • 1
  • 1
  • 3
16
votes
5 answers

How to disable main JFrame when open new JFrame

Example now I have a main frame contains jtable display all the customer information, and there was a create button to open up a new JFrame that allow user to create new customer. I don't want the user can open more than one create frame. Any swing…
user236501
  • 8,538
  • 24
  • 85
  • 119
16
votes
1 answer

Import existing java project in eclipse but design view not exist for JFrame?

I import existing java project in eclipse: in eclipse: 1. go to File -> Import 2. General -> Existing Projects into Workspace and click Next 3.click on Browse and select project then click on Finish then i open a JFrame , but i see source…
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
16
votes
4 answers

Java Bouncing Ball

I am trying to write a Java application which draws multiple balls on screen which bounce off of the edges of the frame. I can successfully draw one ball. However when I add the second ball it overwrites the initial ball that I have drawn. The code…
nomad2986
  • 185
  • 1
  • 1
  • 6
15
votes
4 answers

Something seems wrong with the layout, JButton showing unexpected behaviour at resize of the window

JRE Version 1.7 Update 3 EXPECTED BEHAVIOUR As I run the program, it works as expected, everything works smoothly. As when I click on STOP JButton the animation stops and the text on the same JButton changes to START. Now when i click on BALL COLOUR…
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
15
votes
6 answers

set JFrame Orientation from right to left!

To align my JFrame from righ-to-left, I use: setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); but this works only if I use the following style (decoration) of the JFrame: public class RightToLeft { public static void main(String…
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417