Code for One JButton? Help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Code for One JButton? Help!

  1. #1
    Join Date
    Nov 2002
    Posts
    1

    Question Code for One JButton? Help!

    * I have an Input Box. We'll say someone types in the number 5.
    * I have one Jbutton.
    * I have one Text Field for the output.

    What would a sample code look like? Behind JButton1 would be a calculation like "5.9 x Input Box value". So, is someone types in the number 5 in the Input Box and presses JButton1, the answer, which would be(in this example) 29.5, would display in the Text Field.

    If I can see how it works for one button, I think I can get the others. THANKS.

  2. #2
    Join Date
    Nov 2002
    Posts
    7
    I see that experts do not reply to your question. I am in the same boat. Thus new Javers help new Javers.
    I do not know what you mean on Input Box. But for inputs into textField I would offer the following solution:

    void cmdDo_actionPerformed(ActionEvent e) {
    try{
    int x = Integer.parseInt(txtInput.getText());
    txtOutput.setText(String.valueOf(5.9 * x));
    }
    catch(NumberFormatException exp){
    JOptionPane.showMessageDialog(null,exp,"Wrong Input",1);
    }
    }

    Good luck.

    Alexander Lykov

  3. #3
    Join Date
    Dec 2002
    Posts
    17
    you could also make a method that does the calculations for you, in case you have other arithmetic operations besides just 5.9

    but in YOUR case:

    yourButton.addActionListener(this);

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == yourButton)
    {
    your calculation or method call;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center