Why do JFrames make java components indiscernible?

Hi
Awt Java frames seemed seamless.
Create component, set bounds, add it, create listener, done,

but JFrames always seem to have errors in design and construction and look and feel.

In my attached code, I try and make a normal frame and add a text area on a blue window in certain bounds.
When I set it to visible, I can't see the Text area. I can click on the frame and locate the text area, but the text is writable only off in one corner, not within its bounds? It explodes to the whole window? Why don't JFrame components behave as expected? I want the text area to be framed and discernable from the start, and have text writable within that area only.
Code...

import java.awt.Color;
import javax.swing.*;

public class controlFrame extends JFrame {
      
      JTextArea textarea;
            
      public controlFrame() {
            
            this.setSize(900, 600);
            this.setBackground(Color.PINK);
            
            this.setVisible(true);
            textarea = new JTextArea();
            textarea.setSize(500, 100);
            textarea.setLocation(100, 100);
            textarea.setText("Text Area");
            
            textarea.setVisible(true);
            add(textarea);
            
      }
      public static void main(String[] args) {
            
            new controlFrame();                        
      }
}

I want it to look like a normal window with components that would appear in any professional application. Are JFrames only intended for heavily graphical applications?
Must I go back to AWT ?
Thanks
beavoidAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
So, if I'd like specific sizes, positioning for my textFields and labels for aesthetic needs, I can't place them at x,y, with xSize , ySize ?
Yes you can. The standard and simplest way is to use a null layout [ getContentPane().setLayout(null) ] but that should be avoided, as, for one thing, any resizing of the window (either explicit or owing to a different graphics context) will require you to size all its child components accordingly (unless you don't mind it looking suddenly weird)
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
In general, the problems you are facing are because Swing uses Layout Managers to position components within their parent containers. Check the tutorials within this link...

http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
0
 
krakatoaCommented:
public controlFrame() {
            
            this.setSize(900, 600);
            this.setBackground(Color.PINK);
            
            this.setVisible(true);
            textarea = new JTextArea();
           textarea.setSize(500, 100);
            textarea.setLocation(100, 100);
            textarea.setText("Text Area");
            
            textarea.setVisible(true);
            add(textarea);
	    this.pack();
 

	
            
      }

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
CEHJCommented:
In general, the problems you are facing are because Swing uses Layout Managers to position components within their parent containers.
Yes, but of course AWT is no different in that respect. Layout manages are AWT
0
 
beavoidAuthor Commented:
Now, it seems that once I have instantiated the frame, it vanishes. I added Sleeps around to help lengthen the time of the program before it ends. The frame still vanishes, at once!
Is the layout manager system simple?
It creates a textArea out of the frame, over in the top left corner in its own window, not added inside the frame! ! ?


import java.awt.Color;

import javax.swing.*;


public class controlFrame extends JFrame {
      
      JTextArea textarea;
      
      
      
      public controlFrame() {
            
            this.setSize(900, 600);
            this.setBackground(Color.PINK);
            
            this.setVisible(true);
            textarea = new JTextArea();
            textarea.setSize(200, 80);
            textarea.setLocation(100, 100);
            textarea.setText("Text Area");
            
            textarea.setVisible(true);
            add(textarea);
            this.pack();
            
            try {
                  Thread.sleep(5000);
            } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            
      }

      
      
      public static void main(String[] args) {
            
            JFrame f = new controlFrame();

            System.out.println("After new controlframe");
            try {
                  Thread.sleep(15000);
            } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }

            
            
      }
}
0
 
CEHJCommented:
Your ctor isn't making a lot of sense at the moment. This makes a bit more:
    public controlFrame() {
        setSize(900, 600);
        setBackground(Color.PINK);

        textarea = new JTextArea();
        textarea.setText("Text Area");

        add(textarea);
        setVisible(true);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Open in new window

Though i'm not sure what the sleeping is about
0
 
beavoidAuthor Commented:
The sleep is to try and make the JVM stay alive long enough, so that it doesn't let the frame disappear.
Does the frame die because it has no listeners?
Thx
0
 
krakatoaCommented:
The sleeps() don't do anything, except delay your disappointment.

A JFrame will stay resident until you either stop the JVM or close the window by calling the static JFrame dispose.
0
 
beavoidAuthor Commented:
Good, thanks

THis works. The window stays alive, but the pink background is all covered by the blue textArea. The textArea cant seem to be resized. It explodes, no matter what. Also, pack() seemed to be responsible for shrinking the window. If I take it out, the window is permanent and the right size, but all blue.
?

      public controlFrame() {
            
            this.setSize(900, 600);
            this.setBackground(Color.PINK);
            
            textarea= new JTextArea();
            textarea.setBackground(Color.BLUE);
            textarea.setSize(200, 140);
            add(textarea);
            textarea.setText("setText");
            
            this.setVisible(true);
            }
0
 
krakatoaCommented:
Also, pack() seemed to be responsible for shrinking the window.


That's because your complaint was the fact that nothing fitted.

Back to the main point :

import java.awt.Color;
import javax.swing.*;
import java.awt.BorderLayout;

public class controlFrame extends JFrame {
      
      JTextArea textarea;
            
      public controlFrame() {
            
this.setLayout(new BorderLayout());

            this.setSize(700, 400);
           // this.setBackground(Color.PINK);
this.getContentPane().setBackground(Color.PINK);
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            this.setVisible(true);
            textarea = new JTextArea();
	this.getContentPane().add(textarea,BorderLayout.NORTH);
           textarea.setSize(500, 100);
           // textarea.setLocation(100, 100);
            textarea.setText("Text Area");
            this.setVisible(true);
            //textarea.setVisible(true);
            
	 

	
            
      }
      public static void main(String[] args) {
            
           controlFrame f =  new controlFrame();

		                        
      }
}

Open in new window

0
 
CEHJCommented:
THis works. The window stays alive, but the pink background is all covered by the blue textArea. The textArea cant seem to be resized. It explodes, no matter what.
Because the frame uses a BorderLayout by default. If there is one component added, it's added BorderLayout.CENTER. That sizes the component to the whole available window area. Calling setSize on the added component is ignored.
Also, pack() seemed to be responsible for shrinking the window.
It sizes a window just large enough to accommodate the preferred size of its children, and no larger.
If I take it out, the window is permanent and the right size, but all blue.
See first paragraph above
0
 
beavoidAuthor Commented:
So, if I'd like specific sizes, positioning for my textFields and labels for aesthetic needs, I can't place them at x,y, with xSize , ySize ?

I'm sure I could once design a frame precisely. Was that only AWT

?
Thanks
0
 
krakatoaCommented:
You can use a GridBagLayout to obtain the finest control.
0
 
beavoidAuthor Commented:
It looks like (null) was the correct answer!

Thanks
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.