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
33
votes
6 answers

Sizes of frame icons used in Swing

We can use a list to initialise the window icons using Window.setIconImages(List). What are the different sizes of icons typically used for in a JFrame? Code This code turns 64 different sized images (from 16x16, incrementing by 2)…
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
32
votes
2 answers

Setting minimum size limit for a window in java swing

I have a JFrame which has 3 JPanels in GridBagLayout.. Now, when I minimize a windows, after a certain limit, the third JPanel tends to disappear. I tried setting minimizing size of JFrame using setMinimumSize(new Dimension(int,int)) but no success.…
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
30
votes
4 answers

What is the difference between a JFrame and a JDialog?

What is the difference between a JFrame and a JDialog? Why can't we use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); for a JDialog?
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
30
votes
9 answers

Limiting the number of characters in a JTextField

I want to set the maximum length of a JTextField, so that you can't enter more characters than the limit. This is the code I have so far... textField = new JTextField(); textField.setBounds(40, 39, 105, 20); contentPane.add(textField); …
user1326088
28
votes
2 answers

How to remove title bar in JFrame

I'm using the following code for practising, http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java I also add frame.setSize(frame.getMaximumSize()); in createAndShowGUI()…
CanCeylan
  • 2,890
  • 8
  • 41
  • 51
28
votes
5 answers

How can I refresh or reload the JFrame?

I am doing project using Java and in that I need to reload whole JFrame after clicking particular button on that JFrame. How to do this?
SL_User
  • 1,934
  • 5
  • 24
  • 45
28
votes
4 answers

JFrame: get size without borders?

In Java, is it possible to get the Width and Height of the JFrame without the title and other borders? frame.getWidth() and frame.getHeight()1 seems to return the width including the border. Thanks.
Tom
  • 8,536
  • 31
  • 133
  • 232
28
votes
2 answers

Is this the only way to teach a Java Frame something about the Aero Snap feature of Windows?

If I minimize a JFrame which was Aero-snapped to the left of the screen by clicking on the minimize-button of the Windows WindowDecoration and unminimize it by Alt-Tabbing or clicking it in the Windows TaskBar, the frame gets restored correctly…
bobndrew
  • 395
  • 10
  • 32
27
votes
13 answers

How to set Icon to JFrame

I tried this way, but it didnt changed? ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico"); frame.setIconImage(icon.getImage());
vijay
  • 1,129
  • 7
  • 22
  • 34
26
votes
3 answers

Swing: set JFrame content area size

I'm trying to make a JFrame with a usable content area of exactly 500x500. If I do this... public MyFrame() { super("Hello, world!"); setSize(500,500); } ... I get a window whose full size is 500x500, including the title bar, etc., where I…
igul222
  • 8,557
  • 14
  • 52
  • 60
26
votes
7 answers

Stop Eclipse/Java running multiple instances

I'm not a java expert or a eclipse expert. Currently I'm working on a project and i need to debug/test often. I use the eclipse run Button for it. But when I don't close the Program eclipse/java opens it again(second window). It's an gui app with…
arnoapp
  • 2,416
  • 4
  • 38
  • 70
24
votes
3 answers

refresh JFrame after adding new Components

I want to add some new Components to my JFrame during runtime when a button is pressed. This works so far, but i have to resize the window manually to see the new components. Is there any Action I can fire or a method to call to refresh the window?…
reox
  • 5,036
  • 11
  • 53
  • 98
22
votes
7 answers

How to place a JButton at a desired location in a JFrame using Java?

I want to put a JButton at a particular coordinate in a JFrame. I used setBounds() for the JPanel (which I placed on the JFrame) and also setBounds() for the JButton. However, they don't seem to function as expected. My Output: This is my…
Haxed
  • 2,887
  • 14
  • 47
  • 73
21
votes
6 answers

Why does the JFrame setSize() method not set the size correctly?

So I've been programming in java for a semester or so, and I've had this problem a few times and finally got around to asking. If I make a JFrame and then set the size, like setSize(400,800) for example. The frame is not actually 800 pixels long.…
convergedtarkus
  • 697
  • 2
  • 9
  • 19
21
votes
5 answers

How to make a JFrame really fullscreen?

In my Java application I try to make a JFrame really fullscreen by using this code: public class MainFrame extends JFrame { private static final long serialVersionUID = 1L; public MainFrame() { super(); this.setTitle(); …
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200