Solved

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

Posted on 2006-10-31
11
240 Views
Last Modified: 2010-03-31
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);  
}  
}  

 
}  
   
   

0
Comment
Question by:charliebaby
  • 6
  • 4
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17849162
> 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.
0
 

Author Comment

by:charliebaby
ID: 17852612
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<<<<<<<<<<
0
 

Author Comment

by:charliebaby
ID: 17852713
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
0
 

Author Comment

by:charliebaby
ID: 17853089
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);


0
 

Author Comment

by:charliebaby
ID: 17853246
/////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);

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:charliebaby
ID: 17854400
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();  

0
 
LVL 35

Expert Comment

by:girionis
ID: 17856332
> 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?
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 17856401
Now that I am thinking about it the simplest solution would be to define a new Panel and to add your text area to this new panel. To do this you will need:

1) define the variables like you did with text area and scroll pane

JTextArea area;
JScrollPane scrollPane;

2) Initialize these variables in the public Hw_prog1()

area = new JTextArea(5, 10);  // 5 rows and 10 columns
scrollPane = new JScrollPane(area);  // add the text area to the scroll pane

3) Define a new Panel where you have the rest of the panels, lets call the new panel File6

Panel File6 = new Panel();

4) In the constructor ( public Hw_prog1()) add a new FlowLayout (lets call it layout6 for convenience) and set this layout to the new panel. Then add the scroll pane to the panel. This code should be before the line setVisible(true);  and after where you declared and added File5 to the pane. Then as a last step add the new panel to the pane.

FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);  
File6.setLayout(layout6);
File6.add(scrollPane);
pane.add(File6);

I think this is it. If you have any more questions let me know.
0
 

Author Comment

by:charliebaby
ID: 17869966
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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17879518
Thank you for accepting, if you need more help don't hesitate to ask.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 98
best (free) software to access postgres db (java) 1 32
ArrayIndexOutOfBoundException 9 66
Java Server Faces parameter pass? 6 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

929 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

12 Experts available now in Live!

Get 1:1 Help Now