?
Solved

easy question for experts

Posted on 2003-02-21
8
Medium Priority
?
204 Views
Last Modified: 2010-03-31
Hi experts,
I have a little problem with my program,

1.How can I make a textField takes just int, or just float values (not Strings).
2.I have created a class called carComponent to create objects for a Hashtable (which is not designed yet). What I want is by clicking the button “Submit” the variables from the class carComponent can get the text from the textFileds.

   

NOTE: I AM NOT USING “JAVA SWING”.

That’s all for now.

Thanks for your corporation in advance.

Below is my code

// importing java libraries.
import java.awt.*;
import java.util.*;
import java.awt.event.*;


// extending the application frame and implementing ActionListener to preform actions
public class myServer extends Frame implements ActionListener{

    //definig variables
    private TextField tf1,tf2,tf3,tf4,tf5, tf6;
    private Button submit;
    private Label L, L1, L2, L3, L4, L5;
    private Font myFont;
    private Panel p1,p2,p3,p4,p5,p6,p7;
    private Hashtable table;

        public myServer() {
          // giving a lable to the frame
                  super("::::(SERVER)::::");

          //Building the GUI with 6 TextFields and One Button
          //setting up the overall layout with 7 panels
          setLayout(new GridLayout(7,1,90,0));
                  p1 = new Panel();
          p2 = new Panel();
          p3 = new Panel();
          p4 = new Panel();
          p5 = new Panel();
          p6 = new Panel();
          p7 = new Panel();
          add(p1);
          add(p2);
          add(p3);
          add(p4);
          add(p5);
          add(p6);
          add(p7);

          // setting up panel 1 layout with one Label
          p1.setLayout(new FlowLayout(FlowLayout.LEFT));
          L = new Label("Enter the available parts information to update the database:");
          p1.add(L);
          myFont = new Font("Times New Roman", Font.BOLD, 12);
          L.setFont(myFont);
          L.setBackground(Color.yellow);
          L.setForeground(Color.black);

          // setting up panel 2 layout with a Label and a Textfield
          p2.setLayout(new FlowLayout(FlowLayout.LEFT));
          L1  = new Label("Model Name:         ");
          tf1 = new TextField(10); //model name textfield
          p2.add(L1);
          p2.add(tf1);

          // setting up panel 3 layout with a Label and a Textfield
          p3.setLayout(new FlowLayout(FlowLayout.LEFT));
          L2 = new Label("Model Part Name:");
          tf2 = new TextField(10);//Model Part Name textfield
          p3.add(L2);
          p3.add(tf2);

          // setting up panel 4 layout with a Label and a Textfield
          p4.setLayout(new FlowLayout(FlowLayout.LEFT));
          L3  = new Label("Model Built Year:   ");
          tf3 = new TextField(10); //Model Model Built Year textfield
          p4.add(L3);
          p4.add(tf3);

          // setting up panel 5 layout with a Label and a Textfield
          p5.setLayout(new FlowLayout(FlowLayout.LEFT));
          L4 = new Label("Price £:                   ");
          tf4 = new TextField(10);//price textfield
          p5.add(L4);
          p5.add(tf4);

          // setting up panel 6 layout with a Label, Textfield and a Button
          p6.setLayout(new FlowLayout(FlowLayout.LEFT));
          L5 = new Label("Available Quantity:");
          tf5 = new TextField(10);//Available Quantity textfield
          submit = new Button("Submit");
          p6.add(L5);
          p6.add(tf5);
          p6.add(submit);

          //adding ActoinListener to Button b
          submit.addActionListener(this);

          // setting up panel 7 to test the button and the textfields
          p7.setLayout(new FlowLayout(FlowLayout.CENTER));
          tf6 = new TextField (" RESULT TEST FIELD ",110);

          // stting TextField tf6 into Uneditable form
          tf6.setEditable(false);
          p7.add(tf6);

          //seting the size of the frame
              setSize(new Dimension(950, 300));

          // show the GUI
          show();
           }

       public static void main(String[] args) {
           // use the constructor to build the GUI
                   myServer server = new myServer();
           }


       
       
        public void actionPerformed(ActionEvent e) {

            // defining the variables that be used
            String str1,str2,str3,str4,str5;
            str1 = tf1.getText();
            str2 = tf2.getText();
            str3 = tf3.getText();
            str4 = tf4.getText();
            str5 = tf5.getText();


            // setting ActionPerformance for Button b
            // to display information entered in tf1, tf2, tf3, tf4, tf5
            // into tf6
            if (e.getSource() == submit){
           
            // test result field to make sure that the button and tetfields are working.
            tf6.setText("Model Name:"+str1 + "----Model Part Name:" + str2 + "----Model Built Year:"+ str3 + "-----Price£:" + str4 + "----Available Quantity:" + str5);
      }

  }

}

/*creating a class called carComponent
to creat objects to put them in the hashTabel*/
class carComponent{

   //definig variables of the class
   public String modelName, partName;
   public int builtYear, availableQuantity;
   public float price;

}
0
Comment
Question by:MoSt
[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
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7992841
For the no-string thing you'll have to add a key listener to the field to disallow everything but numbers and the decimal point.

Where you have >>tf6.setText("<< etc. you should construct a new car component object and add it to a hashtable. I would give it a unique part number and return that as the implementation of the class' hashCode method:

public int hashCode() {
  return partNumber;
}
0
 

Author Comment

by:MoSt
ID: 7993184
i apretiate your answer,
thanks for help, but its still unclear for me. can you please give an example for the KeyListener using the given code.

what about the class that i've created "carComponent" is it wrong?
if it is possible to show and example (using the code given) showing howing how the variables from the class carComponent can get the text from the textFileds by pressing the button “Submit”.  

thans for your help.
0
 

Author Comment

by:MoSt
ID: 7993203
i apretiate your answer,
thanks for help, but its still unclear for me. can you please give an example for the KeyListener using the given code.

what about the class that i've created "carComponent" is it wrong?
if it is possible to show and example (using the code given) showing howing how the variables from the class carComponent can get the text from the textFileds by pressing the button “Submit”.  

thans for your help.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:MoSt
ID: 7993266
i apretiate your answer,
thanks for help, but its still unclear for me. can you please give an example for the KeyListener using the given code.

what about the class that i've created "carComponent" is it wrong?
if it is possible to show and example (using the code given) showing howing how the variables from the class carComponent can get the text from the textFileds by pressing the button “Submit”.  

thans for your help.
0
 

Author Comment

by:MoSt
ID: 7993954
is there any expert who can answer my question?!!!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 7995570
Here is something about KeyListeners. Come back again when you've incorporated that into your code:
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
0
 

Expert Comment

by:CleanupPing
ID: 9059168
MoSt:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9582194
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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