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

Java Applet size behavior vs JFrame size behavior

* (Legend) Blue-mainpanel, Red-upanel, Black-ulpanel, Green-urpanel * So I have the same code for both the Applet and the JFrame. As you can see, with the `setsize() as same, both of them produce different results. I cannot figure out how to make…
user1265125
  • 2,608
  • 8
  • 42
  • 65
2
votes
2 answers

Why does a JOptionPane cause JFrame to disappear from taskbar?

I have a problem where I create two separate JFrames (one is my main application, the other shows task progress using console output...). However, subsequently bringing up a dialog box has a strange effect on the two taskbar icons (i.e. for the…
Nick
  • 366
  • 1
  • 4
  • 10
2
votes
3 answers

Java - graphics paint

I'm trying to develop a Java brick breaker (like DxBall) game and I want to make the Ball object with its own draw method. What I'm trying to do: public class Ball { private int x, y, diameter; public void Ball(){ x = 0; y = 0; …
2
votes
3 answers

How can I force a JFrame to have a certain amount of space for a component?

public class n00767255 { public static void main(String[] args) { CarFrame frame = new CarFrame(); frame.setSize(600,480); frame.setLocationRelativeTo(null); frame.setVisible(true); while(true) …
Ty_
  • 828
  • 1
  • 11
  • 21
2
votes
2 answers

searching but did not need a button, JFrame

I have a JFrame which there is a table and a search has been connected to a database. I want to search but to do so does not require a button. So, the search will be done right when I started to fill the name in textfield. Like ajax in html. Can…
dtnder
  • 381
  • 2
  • 10
  • 25
2
votes
1 answer

Why JFrame colour is changing by changing content pane colour?

If the frame is placing on top of the content pane the exterior colour to the user is colour of JFrame. Here even i'm painting the frame after content pane but content pane colour will be displayed. Why? public class GUI { public static void…
Bernard
  • 4,240
  • 18
  • 55
  • 88
2
votes
2 answers

How to set Scrollable TextArea in Java for group Layout?

I have a problem. Every time, when I add more Symbols(Numbers) to TextArea, it doesn't make it scrollable. EDIT: now it works as I want. I only needed to change 2 words. Thanks. class NumOnly extends KeyAdapter { private String Atlauts =…
Dawgora
  • 23
  • 1
  • 8
2
votes
2 answers

Adding JList to JPanel

I'm trying to add a JList to a JPanel. Specifically, I have two JPanels, a right one and a left one. The right one has two buttons. On the left one I want the JList mySpriteOptions (see code below). Unfortunately, when I run my code, this JList…
covertbob
  • 691
  • 2
  • 8
  • 15
2
votes
2 answers

actionPerformed() is executed multiple times after resizing JFrame

It seems that in my application, the amount of times a method is executed when clicking a JButton is related to the amount of times I resize the JFrame. In the example below, when I start the application and don't resize at all, the method…
Valentino Ru
  • 4,964
  • 12
  • 43
  • 78
2
votes
2 answers

My JMenu Bar is not showing up

My JMenuBar is not showing up when i run my App. How can I fix this?? So when I run my JFrame I need to see my JMenuBar on top. my Layout is Null Code: package view; import java.awt.BorderLayout; import java.awt.Container; import…
user1809035
  • 237
  • 1
  • 3
  • 9
2
votes
2 answers

Standalone JPanel

How can I create a JFrame that would look like a JPanel/JLabel? I need a splash screen that will be displayed while my program does some computing before showing the main window (I know there's a splash screen option in Java, but I need mine to show…
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
2
votes
1 answer

Gdx exit closing entire application - how to close only the gdx frame?

I have a game editor, in which it is possible to launch the game being edited in a separate window (but within the same VM). However, when the game is closed, I would like to close its gdx window, without bringing down the whole application (that…
tucuxi
  • 17,561
  • 2
  • 43
  • 74
2
votes
5 answers

JFrame is very tiny when restore down is pressed

My question is why when I press to the restore down (on a Windows platform) the JFrame is very tiny (see the bellow screenshot). I use this code regarding State of the JFrame: this.setExtendedState(View.MAXIMIZED_BOTH); I need to use…
2
votes
3 answers

Drawing over a JPanel and adding the JPanel to JFrame

I need to draw a graph over a JPanel by overriding the JPanel's paintComponent() method. While designing gui using netbeans when i drag/drop a JPanel over JFrame it generates code by creating a private variable, JPanel object. In such a case how…
PKay
  • 433
  • 3
  • 9
  • 20
2
votes
1 answer

Java JComponents not repainting after JDialog closed

I have a Java application that uses a JTable to display some data. I want to enter the data from a JDialog. The problem that I am facing is that the main window GUI won't refresh anymore after the JDialog opens. I have tried to change it to a JFrame…
Gabriel
  • 77
  • 2
  • 12
1 2 3
99
100