PDA

View Full Version : Text area displays one line


Stephandeye
07-23-2009, 10:25 PM
Can anyone tell me why my text area displays only one line.

//import packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;


public class Calc_App extends JFrame
{
//construct and intialize labels, fields, buttons and menu
JLabel titleLabel = new JLabel ("Mortgage Calculator");
JLabel dollarsPrompt = new JLabel ("Enter amount of loan");
JTextField dollarsField = new JTextField (30);
JLabel termsPrompt = new JLabel ("Choose loan terms from menu");
JComboBox termsMenu = new JComboBox ();
JLabel monthPayLabel = new JLabel ("The monthly payment for this loan is");
JLabel monthPayOutput = new JLabel ("XXXXXXX");
JButton calcButton = new JButton ("Calculate");
JButton resetButton = new JButton ("Reset");
JButton cancelButton = new JButton ("Cancel and Exit");
JTextArea listPayment = new JTextArea (60,60);
JScrollPane scrollList = new JScrollPane (listPayment,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

public Calc_App()
{
//add items to menu
termsMenu.addItem ("7 years at 5.35%");
termsMenu.addItem ("15 years at 5.5%");
termsMenu.addItem ("30 years at 5.75%");

//create and format frame
JFrame CalcFrame = new JFrame();
CalcFrame.setBounds(400, 100, 500, 300);
CalcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CalcFrame.setTitle("McBride Financial Mortgage Calculator");
CalcFrame.setVisible(true);
CalcFrame.setLayout(new GridBagLayout());

//add objects to frame and format using gridbag layout
CalcFrame.add (titleLabel, gc(0,0,6,1, GridBagConstraints.NONE, GridBagConstraints.NORTH, 0.0, 0.0));
CalcFrame.add (dollarsPrompt, gc(0,1,3,1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0.0, 0.0));
CalcFrame.add (dollarsField, gc(3,1,2,1, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0.0, 0.0));
CalcFrame.add (termsPrompt, gc(0,2,3,1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0.0, 0.0));
CalcFrame.add (termsMenu, gc(3,2,3,1, GridBagConstraints.NONE, GridBagConstraints.WEST, 0.0, 0.0));
CalcFrame.add (monthPayLabel, gc(0,3,4,1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0.0, 0.0));
CalcFrame.add (monthPayOutput, gc(4,3,2,1, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0.0, 0.0));
CalcFrame.add (resetButton, gc(0,4,2,1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0));
CalcFrame.add (calcButton, gc(2,4,2,1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0));
CalcFrame.add (cancelButton, gc(4,4,2,1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0));
CalcFrame.add (listPayment, gc(0,5,6,4, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0));
}

//helper method for gidbag layout, returns variable gc
static GridBagConstraints gc (int x, int y, int width, int height, int fill, int anchor, double weightx, double weighty)
{
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.fill = fill;
gc.anchor = anchor;
gc.weightx = weightx;
gc.weighty = weighty;
gc.insets = new Insets(3, 3, 3, 3);
return gc;
}

//main method
public static void main(String args[])
{
Calc_App calc = new Calc_App();
}
}


Later in the program I pass string text to the text area at which point in becomes the size I designated when it was initialized. However, I would like the text area to take that size when the program is initailly opened, otherwise the display changes when the text displays. Any advice would be greatly appreciated. Thanks

Stephandeye
07-25-2009, 10:18 PM
I solved my problem. I found some random java tutoial online that demonstrated setting the prefered size of the scrollpane vs. seting the size of the textarea. Contrary to almost every resourceI can find, this apprach worked and now my textarea is the size that I want it to be on execution, even though it is empty.

Can anyone tell me why I needed to do that?