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
20
votes
3 answers

Get Any/All Active JFrames in Java Application?

Is there a way from within a Java application to list all of the currently open/active (I'm not sure the terminology here) JFrames that are visible on screen? Thanks for your help.
Connor Neville
  • 1,028
  • 2
  • 9
  • 13
20
votes
4 answers

How to make a JFrame button open another JFrame class in Netbeans?

I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that? I have tried: JFrame frame = new JFrame(); But I…
Ultrabyte
  • 227
  • 1
  • 4
  • 6
20
votes
1 answer

Which icon sizes to use with a JFrame's setIconImages() method?

Does anyone know which icon sizes to use with the setIconImages() (PLURAL) method for a jFrame so that my app icons display well on all platforms and in all contexts (e.g., window icon, taskbar icon, alt-tab icon, etc.)? I've found an example that…
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
19
votes
4 answers

How to show/hide JPanels in a JFrame?

The application I am developing is a game. What I want to do is have JPanels that appear in the JFrame, like a text or message window, and then disappear when they are no longer used. I have designed these JPanels in Netbeans as external classes…
aharlow
  • 267
  • 1
  • 4
  • 9
19
votes
8 answers

Java get JPanel Components

I have a JPanel full of JTextFields... for (int i=0; i
Devoted
  • 177,705
  • 43
  • 90
  • 110
19
votes
1 answer

How to auto resize JFrame according to content

I have created a custom JPanel that displays images (lets call it MyPanel), I have added this to JFrame's contentPane. I want JFrame to be resized automatically to fit the MyPanel when image changes. It looks like I need to call frame.pack() to do…
nimcap
  • 10,062
  • 15
  • 61
  • 69
19
votes
4 answers

How can I display a BufferedImage in a JFrame?

I want to display variations of the same image in the same JFrame, for example display an image in JFrame, then replace it with gray scale of the same image.
anon
  • 697
  • 3
  • 14
  • 19
18
votes
4 answers

Java - Vertical "FlowLayout" with Horizontal Scrolling

As described in the title, I've been trying to set up sort of a vertical flow layout with horizontal scrolling. The components within the layout will be JLabels. Let me draw a picture: +-------------------------+ <--- window |Label1 Label4 …
RyanB
  • 707
  • 1
  • 5
  • 18
18
votes
4 answers

Best practice for setting JFrame locations

I have a (somewhat philosophical) question relatively to Swing, or to GUI programming in general. Are there recognized best practices on where to locate the JFrame instances used in the application? Where should the first and main frame be…
Scherrer Vincent
  • 244
  • 1
  • 3
  • 9
18
votes
8 answers

Get a Swing component by name

I have in a JFrame some components that I want to refer into another JFrame and I want to get them by name and not do public get/set methods for each. Is there a way from Swing to get a component reference by its name like do c#? e.g.…
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
18
votes
2 answers

Java Swing on high-DPI screen

I have a Java Swing program that uses the System Look And Feel: UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ()); The problem is that on a high-DPI system, the fonts in the frames are way too small. How can I make the text on…
user1828108
  • 447
  • 1
  • 4
  • 11
18
votes
4 answers

How do I remove the maximize and minimize buttons from a JFrame?

I need to remove the maximize and minimize buttons from a JFrame. Please suggest how to do this.
silverkid
  • 9,291
  • 22
  • 66
  • 92
18
votes
4 answers

Unable to import javax.swing.JFrame

I've been looking all over the internet and Can't find an answer. I'm using Eclipse and need to import JFrame from javax.swing. But hovering over the the declaration (which in Eclipse should give you an option to import it) the import option does…
UNEVERIS
  • 249
  • 1
  • 2
  • 8
18
votes
2 answers

How do I draw an image to a JPanel or JFrame?

How do I draw an Image to a JPanel or JFrame, I have already read oracle's tutorial on this but I can't seem to get it right. I need the image "BeachRoad.png" to be displayed on a specific set of coordinates. Here is what I have so far. public class…
Saucymeatman
  • 587
  • 3
  • 6
  • 8
18
votes
4 answers

Java GUI: about getContentPane( ) method and content

In this piece of code: JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); I can see it makes a new label and adds it to the JFrame object frame.…
Bersan
  • 1,032
  • 1
  • 17
  • 28