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
2
votes
1 answer

JFrame not showing a picture

Following is the code I have so far: All the imports are correct. I'm sure. :D When I run the program, all I get is a blank frame, without the picture. It should show up. public class WindowPractice extends JFrame { final static int width=…
Mizu
  • 113
  • 1
  • 5
2
votes
2 answers

Update frame or revalidate/repaint panel

How to update a Java frame with changed content? I want to update a frame or just the panel with updated content. What do I use for this Here is where I want to revalidate the frame or repaint mainpanel or whatever will work I have tried a number of…
user1516251
  • 35
  • 1
  • 6
2
votes
1 answer

How would I execute a function before you close JFrame

Possible Duplicate: Call a method when application closes I want to call a method just before I close my java application using setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); I have tried WindowListener with windowClosed/windowsClosing and it…
user629283
  • 329
  • 1
  • 8
  • 23
2
votes
3 answers

Strange JFrame Behavior

I have the following program which has some very strange and unwanted behavior when it runs. Its supposed to have two buttons, "Start" and "Stop, but when I click "Start" another button shows up right below "Start". Here's a print screen of what I'm…
Nico
  • 3,471
  • 2
  • 29
  • 40
2
votes
2 answers

Correct Layout to set the JFrame and JInternalFrame flexible with every screen size

Possible Duplicate: Example Program JMenubar on JInternalFrame when i Maximize the JInternalFrame i am working on a Swing application, and i am new with the Layouts, i want my JFrame and the included JInternalFrames to take the size of the screen…
Zakaria Marrah
  • 755
  • 6
  • 15
  • 28
2
votes
2 answers

JLabels (And other things) wont show up inside a JPanel in a JFrame (multiple JFrames)

I've created an app but I have a problem with my JLabels not showing up properly. The app currently looks like this: These are 2 JPanels inside a JFrame created with the following code: public JFrame window = new JFrame(); public JPanel top = new…
Marc
  • 1,094
  • 3
  • 17
  • 38
2
votes
2 answers

How to load Fonts from a jar?

I am using java.awt.Frame for my Java application window which is being refreshed from a loop inside main. The application behaves exactly as it should when it is run from Eclipse, but when I package it into a jar, It draws the first screen, but…
lufinkey
  • 342
  • 4
  • 15
2
votes
2 answers

Make JPanel size of JFrame?

I've been staring at my code for hours, and I've cut out almost everything down to the simplest bits and I don't know how to make a JPanel expand to the size of the frame. Ideally (in the end) I'd like a container panel with a north and south…
leigero
  • 3,233
  • 12
  • 42
  • 63
2
votes
1 answer

Hide frame instead of close when user click on close button of the window

I have an application which shows a window. Instead of closing window when user presses on "Close" button I want just to hide this window, how can I do this? Here is my code (it does not work, the app is closed on click): object UxMonitor { def…
Solvek
  • 5,158
  • 5
  • 41
  • 64
2
votes
1 answer

Make a JPanel transparent to see behind the frame?

I know how to do a shaped or a translucent JFrame, but I would like to have a rectangular zone in my frame which would be translucent so the user can see what is behind. I don't know how to do that and take a screen capture of what is behind the…
paranoia25
  • 626
  • 1
  • 5
  • 23
2
votes
3 answers

Getting text (read from a file) into a JFrame

very new to writing Java, so am grateful for any help I can get. I`m trying to create JFrame with text in it read from a separate text file (there are 5 paragraphs, each appearing one after the other). I need help making the text appear on the…
Tom Pengelly
  • 67
  • 2
  • 2
  • 9
2
votes
1 answer

Passing parameter from textfield in on one jframe to another jframe

I'm trying to simply create four jtextfields and a jbutton. Once the button is pushed, i want the text inputted into the jtextfields to be passed as parameters (p, var, s, f) to another window to which displays a mathematical function using the…
2
votes
3 answers

How to change the dimension of a component in a JFrame

Suppose I have a JPanel in a JFrame. When I invoke a method that changes the preferred size of that JPanel, it does not change. The code looks something like this: public class SomePanel extends JPanel{ public SomePanel(){ …
2
votes
2 answers

Can a component itself be used as a listener?

I have tried using this code in Java, where I used a JFrame as it's own ActionListener. Now, it is theoretically possible, because in Java, a class can both implement a number of interfaces and extend another class. import…
JavaNewbie_M107
  • 2,007
  • 3
  • 21
  • 36
2
votes
2 answers

Disable drag of JFrame

I want to ask how to make frame can not drag when the application is running? And then how to disable maximize, minimize button (title bar)? I'm using JFrame.
dtnder
  • 381
  • 2
  • 10
  • 25