Link to home
Start Free TrialLog in
Avatar of charliebaby
charliebaby

asked on

I understand so much, but I know so little-swing

ok here is the code it works great, but now I want to add a scrollpane  with a large text feild that a user can immplment at the touch of a button, I got this far with a lot of help, only it was too much help and now that I am on my own I am soooooo confused. Please work with me so I can really learn how to do this for myself, if that could happen life would be good. I understand so much, but I know so little it seems when it comes to writing my own code, Thank you so much, Charlie




import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;  
import java.text.*;  
 
public class Hw_prog1 extends JFrame {

Hw_prog1Event TRON = new Hw_prog1Event(this);  
JButton Calculate;
JButton Clear;  
JButton Exit;

Panel File1  = new Panel();  
Label PrincipalLabel = new Label("$Enter Loan Amount:",Label.CENTER);   //create first row (File)      
TextField LonAmt = new TextField(10);  

Panel File2 = new Panel();  
Label RateLabel = new Label("0.0Enter Interest Rate:",Label.CENTER);   //create second row (File)      
TextField Rat = new TextField(10);  

Panel File3 = new Panel();  
Label TermLabel = new Label("Enter Term-Years:",Label.CENTER);   //create third row (File)      
TextField Trm = new TextField(10);  

Panel File4 = new Panel();  
Label PaymentLabel = new Label("Monthly Payments:",Label.CENTER);   //create forth row (File)      
TextField MthlyPymnt = new TextField(10);  

Panel File5 = new Panel();   //create fifth row (Buttons)      
 
//create  borders(Grid layout)  
public Hw_prog1() {  //Reference the main method
super("McBride.Financial.MortCal");  //Calling setSize  
setSize(875,130);   //set size

//name Buttons
Calculate = new JButton("Calculate");  
Clear = new JButton("New Amount");  
Exit = new JButton("Exit");  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //inform Frame to close
   
GridLayout layout =new GridLayout(1, 5, 10, 10); //calls the TextFeild & sets paramerters  
Container pane = getContentPane();  //holds the feilds in place & generates a ‚ÄòContainer‚Äô object that represents the pane
pane.setLayout(layout); // checks known parameters

PrincipalLabel.setForeground(Color.magenta);
RateLabel.setForeground(Color.blue);
TermLabel.setForeground(Color.magenta);
PaymentLabel.setForeground(Color.red);

Calculate.setForeground(Color.magenta);
Clear.setForeground(Color.blue);
Exit.setForeground(Color.red);


pane.setForeground(Color.blue);

//TRON listens for Button fuctions
Calculate.addActionListener(TRON);  
Clear.addActionListener(TRON);  
Exit.addActionListener(TRON);

//‚FlowLayout‚a components fall into place from left to right  
FlowLayout mgr = new FlowLayout(FlowLayout.CENTER,10, 10); //sets parameters  
File1 .setLayout(mgr); //Directs the user were to place data  
File1 .add(PrincipalLabel); //add Component  
File1 .add(LonAmt); //add Component    
pane.add(File1 );  

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File2.setLayout(layout2);  
File2.add(RateLabel);  
File2.add(Rat);  
pane.add(File2);  

FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File3.setLayout(layout3);  
File3.add(TermLabel);  
File3.add(Trm);  
pane.add(File3);  

FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER,10, 10);  
MthlyPymnt.setEditable(false);  
File4.setLayout(layout4);  
File4.add(PaymentLabel);  
File4.add(MthlyPymnt);  
pane.add(File4);  

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER,5, 5);  
File5.setLayout(layout5);  
File5.add(Calculate);  
File5.add(Clear);  
File5.add(Exit);  
pane.add(File5);  
setVisible(true);  
}  

public static void main(String[] arguments) {  
Hw_prog1 frame = new Hw_prog1();  
}
class Hw_prog1Event implements ActionListener {  
Hw_prog1 virtual;  

public Hw_prog1Event(Hw_prog1 in) {  
virtual=in;  
}  
public void actionPerformed(ActionEvent event) {  
String command = event.getActionCommand();  
if (event.getSource() == Calculate)  
 LaunchComputation();  
if (event.getSource() == Clear)  
reset();  
if (event.getSource() == Exit)  
System.exit(0);  
}  
void LaunchComputation() {  // sub-program to do math
 try   //when called
 {  
double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue();   // Object & string, double value when Tron calls virtual
double Rat = Double.valueOf(virtual.Rat.getText()).doubleValue();  
int Trm = Integer.parseInt(virtual.Trm.getText());  
int totalmonths = (Trm*12);  
double Payment = 0;  
String monthlyPayment = new String();  
NumberFormat currency = NumberFormat.getCurrencyInstance();  
double I = (Rat/100.0/12.0);  

Payment = (LonAmt*I)/(1-Math.pow(1/(1+I),totalmonths));  
virtual.MthlyPymnt.setText(currency.format(Payment));  
} catch (NumberFormatException ex){  
}  
}  
protected void reset(){  
virtual.LonAmt.setText(null);  
virtual.Rat.setText(null);  
virtual.Trm.setText(null);  
virtual.MthlyPymnt.setText(null);  
}  
}  

 
}  
   
   

Avatar of girionis
girionis
Flag of Greece image

> ok here is the code it works great, but now I want to add a scrollpane  with a large text feild that a user can immplment at the touch of a button

First you have to define the scroll pane as a variable and a text area (I guess you are talking about a text area and not a text field). Then while you initialize the scroll pane add the text area to it. Do something like the following:

JTextArea area = new JTextArea(5, 10);
JScrollPane scrollPane = new JScrollPane(area);

Now I am not sure I undertand what exactly you mean when you say "a user can implement at the touch of a button". Do you want it to open in a new window when a user clicks a button. If so then you will have to add a button in the same way you do with the rest of the buttons, and when this button is clicked load a new window. You will have to define a new JFrame and add a new JPanel to this frame. Then add your jscrollpane to this jpanel. Otherwise, if you just want it displayed with the rest of the components, just add it like you do with the rest of them.

If you need any more help or clarifications let me know.
Avatar of charliebaby
charliebaby

ASKER

now that I have defined , and placed the equippment, It compiles, now what more do I have to do just to see the textarea and scrol, I won;t worry about extra buttons at the moment. thank you (must implement?)



Hw_prog1Event TRON = new Hw_prog1Event(this);  
JButton Calculate;
JButton Clear;  
JButton Exit;
JTextArea area;>>>Okay I think that is defined right<<<<<<<<<<<<
JScrollPane scrollPane;>>>Okay I think that is defined right<<<<<<<<<<
.....................................
GridLayout layout =new GridLayout(1, 5, 10, 10); //calls the TextFeild & sets paramerters  
Container pane = getContentPane();  //holds the feilds in place & generates a ‚ÄòContainer‚Äô object that represents the pane
pane.setLayout(layout); // checks known parameters

JTextArea area = new JTextArea(5, 10);>>>placed here <<<<<<<<<<
JScrollPane scrollPane = new JScrollPane(area);>>>placed here<<<<<<<<<<
I know I have to set these to be true? also I am not sure if I have to  "JTextArea.go(); some how if so how do I write it and about were would they go? Thank you
I think I am on the right track but I need a little more help,promise will give up the points after next correspondance, and post a new question. Thankyou

Hw_prog1Event TRON = new Hw_prog1Event(this);  
JButton Calculate;
JButton Clear;  
JButton Exit;
JTextArea area;
JScrollPane scrollPane;

FlowLayout layout0 = new FlowLayout(FlowLayout.CENTER,10, 10);
Panel File0  = new Panel();
JTextArea area = new JTextArea(20, 50);
File0.setJTextArea(layout0);
File0.add(File0);


/////or maybe this is better , Says 2 errors though, but I think I'm getting closer?////

GridLayout layout =new GridLayout(1, 5, 10, 10); //calls the TextFeild & sets paramerters  
Container pane = getContentPane();  //holds the feilds in place & generates a ‚ÄòContainer‚Äô object that represents the pane
pane.setLayout(layout); // checks known parameters

FlowLayout layout0 = new FlowLayout(FlowLayout.CENTER,10, 10);
JPanel File0  = new JPanel();
JTextArea area = new JTextArea(20, 50);
 area.go();
 area.set LineWrap(true)

 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>???//error>>Hw_prog1.java:64: ';' expected

JScrollPane scrollPane = new JScrollPane(area);

I think this is even better I do of corse get 3 errors <Hw_prog1.java:44: <identifier> expected.> what else do I have to give it ?



Hw_prog1Event TRON = new Hw_prog1Event(this);
JTextArea text;  
JButton Calculate;
JButton Clear;  
JButton Exit;
JTextArea text;

Panel File5 = new Panel();   //create fifth row (Buttons)
text=new JTextArea(50,70);
text.setLineWrap(false);

public static void main(String[] arguments) {
TextArea gui = new TextArea();
gui.go();  

> JTextArea area;>>>Okay I think that is defined right<<<<<<<<<<<<
> JScrollPane scrollPane;>>>Okay I think that is defined right<<<<<<<<<<

Yes this is define and you do it right :)

Right from what I can see you are trying to change the layout to accomodate the text area. This might not be necessary if you want the text area to be launched in a new window at the push of a button. I am not sure what you mean in the following:

> I want to add a scrollpane  with a large text feild that a user can immplment at the touch of a button

Do you want the text area to display in a new window when a use clicks a button or do you want it to display in the new window with ther rest of the buttons and text fields?
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece 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
That seems like a nice guide to get the job done I'll get started and if I need more help I will post a new question thank you so much.
Thank you for accepting, if you need more help don't hesitate to ask.