Creating an text area

I would like to create a simple window that has a text area
where people cannot edit text area, and there should be a scroll bar for it also.
On the top right corner above the text area should be a button that says “Print Out Config”

the result is just a simple widow that has a blank text area with a scroll bar next to it, a button somewhere that says Print Out Config.

I have tried the following, but nothing shows up when I run the program.

so far this is what I have came up with




import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;

public class ConfigTester {
	
	private static final int FRAME_WIDTH = 800;
	private static final int FRAME_HEIGHT = 600;
	

	
	public static void main(String[] args) {
		
		JFrame myFrame = new JFrame("Config Editor 1.0");
		
		
		myFrame.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
		myFrame.setVisible(true);
		
		myFrame.addWindowListener( new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}	});
		
		final int AREA_ROWS = 10;
		final int AREA_COLUMNS= 30;
		
		
		final JTextArea resultsArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
		JScrollPane scrollPane = new JScrollPane(resultsArea);
		
		resultsArea.setEditable(false);
		

		
		JButton printAllButton = new JButton("Print Out Config");
		
		JPanel northPanel = new JPanel();
		northPanel.add(printAllButton);
		
		myFrame.add(northPanel, BorderLayout.NORTH);
		myFrame.add(scrollPane);
		
		
		
		
	}
}


  1. Layout containers, add listeners, and add components /before/ you make the frame visible. Any changes you make to the layout of a Swing component while it is visible do not get made unless the container is laid out agin. That is, resized, or hidden and shown again.
  2. When you add things to a JFrame, you should add them to the content pane, not the JFrame itself. For example:

You have:
myFrame.add(northPanel, BorderLayout.NORTH);

Should (probably) be:
myFrame.getContentPane().add(northPanel, BorderLayout.NORTH);

  1. For cleanliness reasons, consider explicitly setting the layout of your containers before adding anything to them.

  2. Don’t set the bounds of your frame - set the bounds of the components instead. In this case, the ScrollPane.

  3. Before you make the frame visible, make the following call on your frame (check it out in the Java Docs):

myFrame.pack();

  1. Don’t use JFrame.setVisible(boolean visible) to make it visible. Use JFrame.show() and JFrame.hide(). These methods are actually members of the java.awt.Window class, but JFrame gets this functionality by extension.

  2. Which version of the JDK are you coding in?

Hope that helps.

Oh I am using JDK 1.5 right now and eclipse to write my programs.

Thank you for all your help!! I will try to fix my program right now.

2) When you add things to a JFrame, you should add them to the content pane, not the JFrame itself. For example:

I think that in 1.5 the add method of JFrame adds directly to the content pane (new “feature”). Could be my memory acting up though.

EDIT: This is definitely the case:

[quote] The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame 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. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

   frame.add(child);

[/quote]

Ah - adds to the content pane for you - that’s a welcome change.

I have revised my .java and decided to only learn how to make text fields first. Right now I am having trouble trying to set the size and the position of my textbox. I would like it to be only half the size of the window positioned and the bottom however, right now the textbox seems to take up the whole window.

Here is my .java

Thank you very much for your help!!





import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;


public class ConfigTester {
	
	private static final int FRAME_WIDTH = 800;
	private static final int FRAME_HEIGHT = 600;
	

	
	public static void main(String[] args) {
		
		Color frameBackgroundColour = new Color(76,88,68);
		
		
		JFrame myFrame = new JFrame("Config Editor 1.0");
		myFrame.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
		myFrame.setBackground(frameBackgroundColour);
		myFrame.addWindowListener( new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}	});
		
		
		
		final int AREA_ROWS = 10;
		final int AREA_COLUMNS= 30;
		final JTextArea resultsArea = new JTextArea();
		resultsArea.setEditable(false);
		
		
		
		JScrollPane scrollPane = new JScrollPane(resultsArea);
		
		scrollPane.setBounds(0, 0, AREA_ROWS, AREA_COLUMNS);
		scrollPane.setBackground(frameBackgroundColour);
		

		

		
		
		myFrame.getContentPane().add(scrollPane);
		
		
		
		myFrame.setVisible(true);
		
		
	}

}


Maybe Im just blind but I dont see a “setLayout()” anywhere.

You really want to set a layout manager on the content pane. I recommend BorderLayout as the easiest one to learn to use.

Otherwise you need to manually set the actual pixel location adn size of each compoenent AND it will not chnage if you resize the window.

Right now my problem is that the textbox area takes up the whole window :frowning: :frowning: how would i be able to make it half its size and have it positioned on the bottom? Also i would like to fix the window so it cannot be resized by the user.

Thanks for your help!

Actually they are part of java.awt.Component and setVisible(boolean) is the right method to use. show() and hide() have been deprecated as of JDK 1.1.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#setVisible(boolean)

You should read the java tutorial before continuing, it will make more sense… : http://java.sun.com/docs/books/tutorial/uiswing/index.html
Than, go and search for the Jgoodies forms layout, which is the best (IMHO) layout out there, if you don’t loke it, then you’ll have to use the hard-to-learn GridBagLayout.

Until then, I suggest you do the following :
frame.setLayout(new GridLayout(2,1); // 2 rows, 1 column
frame.add(Box.createGlue()); // an invisible component on top (first row)
frame.add(scrollPane); // your scrollpane under it

warning : don’t rely on gridlayout as all cells end up with the same size. But in that case, that’s what you want to acheive. Usually you only use that layout to have a row of buttons of the same size.

Oh and about your other problem : frame.setResizeable(false);

Lilian

+1. Excellent advice.

You could also go to the Swing specific section of the Java Desktop community here on java.net.

JK