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
47
votes
8 answers

JFrame: How to disable window resizing?

I am creating a JFrame and I call the method setSize(500, 500). Now the desired behaviour is that JFrame should not be resized by user in any condition. Either by maximizing or by dragging the borders. It should be 500x500. How can I do it? I have…
Junejo
  • 717
  • 1
  • 7
  • 14
46
votes
5 answers

How to maximize a JFrame through code?

How to maximize a JFrame through code?
Tony Ogrewall
  • 471
  • 1
  • 4
  • 5
45
votes
6 answers

Java - How to create a custom dialog box?

I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how to do this but I keep on getting more confused. Can anyone help?
Corrie
44
votes
3 answers

How do I save preference user settings in Java?

For example, I have a window with a preference button. I want to make it so that when user press the preference button and checks his/her appropriate options and press ok, it saves the preference, then when user presses run on the main window, it…
js0823
  • 1,843
  • 8
  • 24
  • 36
43
votes
2 answers

JPanel vs JFrame in Java

I am learning Java gui. The way I learnt to create a window is to inherit or Extend JFrame class and it is good to use it, as JFrame contains all the properties of a Window. Now If I want to add something to this window, I need to use add() method.…
Naruto
  • 1,710
  • 7
  • 28
  • 39
41
votes
4 answers

Setting background images in JFrame

Are any methods available to set an image as background in a JFrame?
Shruthi
40
votes
4 answers

KeyListener, keyPressed versus keyTyped

I have a JFrame (well, a class which extends JFrame) and I want to do an action when I press the F5 key. So, I made the class implement KeyListener. And with that, came three methods, keyPressed, keyReleased, and keyTyped. Which of these methods…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
38
votes
3 answers

How to hide a JFrame in system tray of taskbar

I have created a JFrame and want to hide it in the taskbar in windows, but, it should not be visible in the bottom right corner, but hidden in the tray menu items. Can anybody tell me how to do this? Do I need to make some changes in system settings…
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
37
votes
9 answers

How can I remove just the Maximize button from a JFrame?

I have a JFrame and want to remove the maximize button from that. I wrote the code below, but it removed maximize, minimize, and close from my JFrame. JFrame frame = new…
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
37
votes
6 answers

JFrame.dispose() vs System.exit()

What is the difference between these two methods - System.exit() and JFrame.dispose()? If we want to close a Java Swing application when a button is clicked, which method should I use?
Adil
  • 4,503
  • 10
  • 46
  • 63
36
votes
6 answers

What is the relation between ContentPane and JPanel?

I found one example in which buttons are added to panels (instances of JPanel) then panels are added to the the containers (instances generated by getContentPane()) and then containers are, by the construction, included into the JFrame (the…
Roman
  • 124,451
  • 167
  • 349
  • 456
35
votes
5 answers

Center JDialog over parent

I have a Java swing application with a button that produces a popup window when a certain action is performed. I'd like to align the center point of the popup window with the center point of the parent window when it is rendered. How can I…
Chris Drappier
  • 5,280
  • 10
  • 40
  • 64
34
votes
6 answers

How to minimize a JFrame window from Java?

In my Java app, I have a JFrame window, how can I minimize it from my Java program ?
Frank
  • 30,590
  • 58
  • 161
  • 244
34
votes
4 answers

Java Swing adding Action Listener for EXIT_ON_CLOSE

I have a simple GUI: public class MyGUI extends JFrame{ public MyGUI(){ run(); } void run(){ setSize(100, 100); setVisible(true); …
hamid
  • 2,033
  • 4
  • 22
  • 42
33
votes
3 answers

How to remove all components from a JFrame in Java?

I'm writing a program where I have a JFrame and I want to remove all components from it, then add just one component to it and repaint the frame. What I have so far is something like the code below (called in an object that implements JFrame, where…
scaevity
  • 3,991
  • 13
  • 39
  • 54