Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why do JFrames make java components indiscernible?

Posted on 2013-07-01
15
Medium Priority
?
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 36

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
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 …
Introduction to Processes

604 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