Results 1 to 7 of 7
  1. #1
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default help with lab - JButton not working

    Can anybody here help me with this? this is a lab for my comp science class where we have to make a little form for title (mr. and mrs.), name, address and such and have it print out.

    for some reason though i cant figure out why the clear and submit buttons do nothing

    heres the code:

    Java Code:
    public class GUI extends JFrame
    {
        public static final int WIDTH = 500;
        public static final int HEIGHT = 400;
        public int credInt;
        String title = "";
        String name = "";
        String address = "";
        String credit = "";
        String creditNum = "";
        private JComboBox titleBox;
        private JTextField nameField;
        private JTextField addressField;
        private JComboBox creditBox;
        private JTextField creditNumField;
        private JButton clear;
        private JButton submit;
    
        public static void main (String [] args)
        {
                GUI gui = new GUI();
                gui.setVisible(true);
        }
        
        public GUI()
        {
                setTitle("GUI");
                setSize(WIDTH, HEIGHT);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
                JPanel aPanel = new JPanel();
                aPanel.setBackground(Color.RED);
                
                JPanel fieldPanel = new JPanel();
                fieldPanel.setBackground(Color.GREEN);
                
                JPanel titlePanel = new JPanel();
                JPanel namePanel = new JPanel();
                JPanel addressPanel = new JPanel();
                JPanel creditPanel = new JPanel();
                JPanel creditNumPanel = new JPanel();
                JPanel clearPanel = new JPanel();
                JPanel submitPanel = new JPanel();
                
                JPanel buttonPanel = new JPanel( );
                buttonPanel.setBackground(Color.BLUE);
                
                String[] titles = {"Mr.","Mrs.","Ms.","Dr."};
                titleBox = new JComboBox(titles);
                titleBox.addActionListener(new Title());
                titleBox.setEnabled(true);
                titleBox.setSelectedItem(null);
                fieldPanel.add( new JLabel("Title: ") );
                fieldPanel.add(titleBox);
                
                nameField = new JTextField(30);
                nameField.addActionListener(new Name());
                fieldPanel.add( new JLabel("Name: ") );
                fieldPanel.add(nameField);
                
                
                addressField = new JTextField(30);
                addressField.addActionListener(new Address());
                fieldPanel.add( new JLabel("Address: ") );
                fieldPanel.add(addressField);
                
                String[] credits = {"Visa","Mastercard","AMEX"};
                creditBox = new JComboBox(credits);
                creditBox.addActionListener(new Credit());
                creditBox.setEnabled(true);
                creditBox.setSelectedItem(null);
                fieldPanel.add( new JLabel("Credit Card: ") );
                fieldPanel.add(creditBox);
                
                creditNumField = new JTextField(16);
                creditNumField.addActionListener(new CreditNum());
                fieldPanel.add( new JLabel("Credit Card #: ") );
                fieldPanel.add(creditNumField);
    
                clear = new JButton("Clear");
                clear.addActionListener(new ButtonListenerGeneral());
                submit = new JButton("Submit");
                submit.addActionListener(new ButtonListenerGeneral());
                
                buttonPanel.add(clear);
                buttonPanel.add(submit);
                aPanel.add(fieldPanel);
                aPanel.add(buttonPanel);
                
                setContentPane(aPanel);
            }
                
    private class Title implements ActionListener
    {
        public void actionPerformed(ActionEvent t)
        {
                Object sourceOfEvent = t.getSource();
                JComboBox firingObject = (JComboBox) sourceOfEvent;
                title = (String) firingObject.getSelectedItem();
        }
    }
    private class Name implements ActionListener 
    {
            public void actionPerformed(ActionEvent n)
            {
                  Object sourceOfEvent = n.getSource();
                  JTextField firingObject = (JTextField) sourceOfEvent;
                  name = ((JTextField) sourceOfEvent) .getText();
            }
    }
    private class Address implements ActionListener 
    {
        public void actionPerformed(ActionEvent a)
        {
                Object sourceOfEvent = a.getSource();
                JTextField firingObject = (JTextField) sourceOfEvent;
                address = ((JTextField) sourceOfEvent) .getText();
        }
    }
    private class Credit implements ActionListener 
    {
        public void actionPerformed(ActionEvent c)
        {
                Object sourceOfEvent = c.getSource();
                JComboBox firingObject = (JComboBox) sourceOfEvent;
                credit = (String) firingObject.getSelectedItem();
        }
    }
    private class CreditNum implements ActionListener 
    {
        public void actionPerformed(ActionEvent cN)
        {
                Object sourceOfEvent = cN.getSource();
                JComboBox firingObject = (JComboBox) sourceOfEvent;
                creditNum = (String) firingObject.getSelectedItem();
                String text = ((JTextField) sourceOfEvent) .getText();
          
    
                try 
                {
                        credInt = Integer.parseInt(text);
                }
                catch ( NumberFormatException ex ) 
                {
                        System.err.println("Invalid entry " + text);
                }
         }
    }
    
    private class ButtonListenerGeneral implements ActionListener 
    {
        public void actionPerformed(ActionEvent b)
        {
                String button = b.getActionCommand();
                
                 if (button.equals("Clear"))
                 {
                           nameField.setText("");
                           addressField.setText("");
                           creditNumField.setText("");
                           titleBox.setSelectedItem(null);
                           creditBox.setSelectedItem(null);
                 }
                 if (button.equals("Submit"))
                {
                        try
                        {
                                int credNum = Integer.parseInt(creditNum);
                                System.out.println(title + name);
                                System.out.println(address);
                                System.out.println(credit + credInt);
                       }
                       catch(NumberFormatException notnum)
                       {
                                System.err.println("Invalid entry " + credInt);
                       }
                }
                else
                System.err.println("Invalid entry ");
            }  
        }
    }
    Last edited by sAntA199; 11-19-2009 at 07:04 AM.

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

    Default

    You are declaring your JTextFields and other components twice, so the nameField (for instance) that is visible to the class is not the same one that is shown on the GUI. Solution, declare the fields just once in the class and don't redeclare them in the constructor.

    So in other words in the constructor it's:
    Java Code:
    // Don't do this:
    // JTextField nameField = new JTextField(30);  // here you declare and initialize both
    
    // Do this:
    nameField = new JTextField(30);  // here you just initialize and don't declare
    
    // do the same for the other JTextFields in your class.

  3. #3
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You are declaring your JTextFields and other components twice, so the nameField (for instance) that is visible to the class is not the same one that is shown on the GUI. Solution, declare the fields just once in the class and don't redeclare them in the constructor.

    So in other words in the constructor it's:
    Java Code:
    // Don't do this:
    // JTextField nameField = new JTextField(30);  // here you declare and initialize both
    
    // Do this:
    nameField = new JTextField(30);  // here you just initialize and don't declare
    
    // do the same for the other JTextFields in your class.
    you are a smart one Fub I'm keeping my eye on you i was wondering if her actionperformeds actually did anything i didn't study the code too long.....they do oops

  4. #4
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    thanks a lot it works fine now

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

    Default

    You're entirely welcome!

  6. #6
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    ok so i cant figure out why the submit button doesnt work.
    I changed the code in the original post to what it is currently to save space
    any ideas?

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

    Default

    Now you're using variables in your ButtonListenerGeneral ActionListener that you never fill. For instance, if name is never given the results from the nameField it won't magically get these results. Time for you to do a little debugging. ;)

Similar Threads

  1. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 PM
  2. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 12:00 AM
  3. Jbutton not working??
    By rmabrey in forum AWT / Swing
    Replies: 7
    Last Post: 09-24-2008, 05:30 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 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
  •