Results 1 to 3 of 3
  1. #1
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default Problem Reading in data from Text Field

    So I couldn't get this exercise done even though I'm so close and I know it..

    Basically need to create a GUI that has two text fields (input and output) and two buttons (double and triple)

    You input a number into the input text field, press double or triple and voilą it prints out the answer in the output text field.


    This is my first class which builds the GUI implements the action listeners etc.


    Java Code:
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    
    /**
     * 
     */
    
    /**
     * @author Mark Walsh 
     *
     */
    public class MyCalculatorGui extends JFrame implements ActionListener{
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        
        JTextField input = new JTextField(20);
        JTextField output = new JTextField(20);
        int result = 0;
        
        public MyCalculatorGui() {
            
            super("Gui Calculator");
            
            Container c = getContentPane();
            
            JPanel panel = new JPanel();
            
    
            input.setText("");
            input.setEditable(true);
            
            
            
            output.setEditable(false);
            
            
            JButton doubleButton = new JButton();
            doubleButton.setText("Double");
            doubleButton.addActionListener(this);
            
            JButton tripleButton = new JButton();
            tripleButton.setText("Triple");
            tripleButton.addActionListener(this);
            
            panel.add(input);
            panel.add(doubleButton);
            panel.add(tripleButton);
            panel.add(output);
            
            
            c.add(panel);
            
            setSize(450, 400);
            setVisible(true);
            setResizable(true);
            
            
        }
        
        public void actionPreformed(ActionEvent e){
            
            if(e.getActionCommand().equals("Double")){
                String s = input.getText();
                
                int val = Integer.parseInt(s);
                
                int result = MathHelper.doubleInt(val);    
                
                output.setText(""+result);
            }
            
            else if(e.getActionCommand().equals("triple")){
                String s = input.getText();
                
                int val = Integer.parseInt(s);
                
                int result = MathHelper.tripleInt(val);    
                
                output.setText(""+result);
            }
            
            
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            MyCalculatorGui MyGui = new MyCalculatorGui();
    
        }
    
            
    }
    And this is my second class that you pass the information obtained in the input box into, and it returns the answer


    Java Code:
    /**
     * @author Mark Walsh 
     *
     */
    public class MathHelper {
    
        /**
         * @param doubleInt .. info to be filled in
         */
        public static int doubleInt(int inputNum) {
            return inputNum*2;
    
        }
        
        public static int tripleInt(int inputNum) {
            return inputNum*3;
    
        }
    
    }
    Now the program runs but doesn't work to how I want it too. And its really getting to me now.

    I am rather new to the Swing and Awt packages

    Can someone please even give me a good guidance to whats going wrong.

    Cheers guys

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by markious View Post
    Java Code:
    public class MyCalculatorGui extends JFrame implements ActionListener{
        
        //....
        
        public void actionPreformed(ActionEvent e){ // ?? "Preformed"
                //....
        }
            
    }
    Now the program runs but doesn't work to how I want it too. And its really getting to me now.
    Are you sure? Does it even compile? It shouldn't given the critical misspelling noted above.

  3. #3
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Omg..

    I feel completely and utterly stupid. lol Major fail.

    Let me show you my original copy and pasted code:

    Java Code:
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    
    /**
     * 
     */
    
    /**
     * @author Mark Walsh 
     *
     */
    public class MyCalculatorGui extends JFrame implements ActionListener{
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        
        JTextField input = new JTextField(20);
        JTextField output = new JTextField(20);
        int result = 0;
        
        public MyCalculatorGui() {
            
            super("Gui Calculator");
            
            Container c = getContentPane();
            
            JPanel panel = new JPanel();
            
    
            input.setText("");
            input.setEditable(true);
            
            
            
            output.setEditable(false);
            
            
            JButton doubleButton = new JButton();
            doubleButton.setText("Double");
            doubleButton.addActionListener(this);
            
            JButton tripleButton = new JButton();
            tripleButton.setText("Triple");
            tripleButton.addActionListener(this);
            
            panel.add(input);
            panel.add(doubleButton);
            panel.add(tripleButton);
            panel.add(output);
            
            
            c.add(panel);
            
            setSize(450, 400);
            setVisible(true);
            setResizable(true);
            
            
        }
        
        public void actionPreformed(ActionEvent e){
            
            if(e.getActionCommand().equals("Double")){
                String s = input.getText();
                
                int val = Integer.parseInt(s);
                
                int result = MathHelper.doubleInt(val);    
                
                output.setText(""+result);
            }
            
            else if(e.getActionCommand().equals("triple")){
                String s = input.getText();
                
                int val = Integer.parseInt(s);
                
                int result = MathHelper.tripleInt(val);    
                
                output.setText(""+result);
            }
            
            
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            MyCalculatorGui MyGui = new MyCalculatorGui();
    
        }
    
    @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            
        }
    
    }
    notice

    Java Code:
    @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            
        }
    Only started using Eclipse also.. Only Noticed that part after I pasted here. And me thinking I added it by accident deleted that part.


    I removed that code and fixed the spelling error.. Works perfect.

    :o

    Talk about feeling like a complete newb :( :P

Similar Threads

  1. Reading data from a text file
    By Cheguvara in forum New To Java
    Replies: 2
    Last Post: 02-02-2010, 02:33 PM
  2. Problem with text field patern
    By Prashant.surwade in forum Advanced Java
    Replies: 1
    Last Post: 09-05-2009, 02:45 AM
  3. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  4. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 07:29 PM
  5. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •