Solved

Why do JFrames make java components indiscernible?

Posted on 2013-07-01
15
344 Views
Last Modified: 2013-07-03
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
0
Comment
Question by:beavoid
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39292171
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39292490
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39292623
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
 

Author Comment

by:beavoid
ID: 39293773
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39293822
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
 

Author Comment

by:beavoid
ID: 39294414
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39294440
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:beavoid
ID: 39294733
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39294892
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39294924
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
 

Author Comment

by:beavoid
ID: 39294959
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39294975
You can use a GridBagLayout to obtain the finest control.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39295881
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
 

Author Comment

by:beavoid
ID: 39297966
It looks like (null) was the correct answer!

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39298080
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now