Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

easy question for experts

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
MoSt
Asked:
MoSt
1 Solution
 
CEHJCommented:
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
 
MoStAuthor Commented:
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
 
MoStAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MoStAuthor Commented:
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
 
MoStAuthor Commented:
is there any expert who can answer my question?!!!
0
 
CEHJCommented:
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
 
CleanupPingCommented:
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
 
tomboshellCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now