Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

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
Avatar of mccarl
mccarl
Flag of Australia image

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
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

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
Avatar of James Hancock

ASKER

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();
            }

            
            
      }
}
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
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
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.
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);
            }
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

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
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
You can use a GridBagLayout to obtain the finest control.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It looks like (null) was the correct answer!

Thanks
:)