Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Weird ActioListener problem

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Weird ActioListener problem

    Hello,

    I seem to have no syntax errors although when adding code it wont execute.

    In the Action performed method i have:

    if(e.getSource() == button1) { System.exit(0); }

    the System.exit(0); will not execute a matter of fact nothing i put in there will... However if i add the code above the if statement it seems to execute. The problem being im having multiple buttons and dont want them all to do the same thing, Here is my code as ive said no errors but the code wont execute.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    /**
     * This program will recieve the IP, PCI and client name of client computers and save them to a text file.
     * 
     * @author Marko Klesnik
     * @version 1.0
     */
     
    public class guiDesign implements ActionListener
    {
     
        // Variable Declarations
        // Declare JButtons
        public JButton btnNew, btnSave, btnFind, btnDelete, btnExit;
     
        // Declare JLabels
        public JLabel lblName, lblIP, lblPCI, lblDesc, lblDesc2, lblDesc3, lblDesc4;
     
        // Declare JTextFields
        public JTextField txtName, txtIP, txtPCI;
     
        // Create a string array
        public String [] clientList;
     
     
        public static void main(String[] args)
            {
                guiDesign test = new guiDesign();
                test.init();
     
            }
     
     
        public void init ()
        {
     
            // Create a string array
            clientList = new String [9];
            clientList [0] = "Please click Find Entry..";
     
            // Declare JList
            JList Selector = new JList ();
            Selector.setSize(90, 90);
            Selector.setBackground(Color.BLACK);
            Selector.setListData (clientList);
     
             // Scrollpane for JList
            JScrollPane ListScrollPane = new JScrollPane(Selector);
            ListScrollPane.setPreferredSize(new Dimension(550,200));
     
            // Define default JButton data
            JButton btnNew = new JButton("New Entry ");
            btnNew.setBackground(Color.BLACK);
            btnNew.setForeground(Color.GREEN);
            JButton btnSave = new JButton("Save Entry");
            btnSave.setBackground(Color.BLACK);
            btnSave.setForeground(Color.GREEN);
            JButton btnFind = new JButton("Find Entry");
            btnFind.setBackground(Color.BLACK);
            btnFind.setForeground(Color.GREEN);
            JButton btnDelete = new JButton("Delete Entry");
            btnDelete.setBackground(Color.BLACK);
            btnDelete.setForeground(Color.GREEN);
            JButton btnExit = new JButton("Exit System");
            btnExit.setBackground(Color.BLACK);
            btnExit.setForeground(Color.RED);
     
            // Define default JLabel data
            JLabel lblName = new JLabel("Client Name:");
            lblName.setForeground(Color.WHITE);
            JLabel lblIP = new JLabel("IP Address:");
            lblIP.setForeground(Color.WHITE);
            JLabel lblPCI = new JLabel("PC Identifier:");
            lblPCI.setForeground(Color.WHITE);
            JLabel lblDesc = new JLabel(" This program will allow the user to add ");
            lblDesc.setForeground(Color.WHITE);
            JLabel lblDesc2 = new JLabel(" edit or delete Users.their PC Identifiers ");
            lblDesc2.setForeground(Color.WHITE);
            JLabel lblDesc3 = new JLabel(" and their PC's IP Address. ");
            lblDesc3.setForeground(Color.WHITE);
            JLabel lblDesc4 = new JLabel(" Please click on the 'Find Entry' button to generate a client list... ");
            lblDesc4.setForeground(Color.WHITE);
     
            //Define default JTextField data
            JTextField txtName = new JTextField("Client1", 10);
            txtName.setBackground(Color.BLACK);
            txtName.setForeground(Color.GREEN);
            JTextField txtIP = new JTextField("", 10);
            txtIP.setBackground(Color.BLACK);
            txtIP.setForeground(Color.GREEN);
            JTextField txtPCI = new JTextField("", 10);
            txtPCI.setBackground(Color.BLACK);
            txtPCI.setForeground(Color.GREEN);
     
            //Create container + Layout
            SpringLayout scnLayout = new SpringLayout();
     
            // Create a frame and apply settings
            JFrame progFrame = new JFrame("[COMPANY NAME] Client Recording Software");
            progFrame.setVisible(true);
            progFrame.setSize(900, 800);
            progFrame.setResizable(false);
     
            // Create a JPanel and apply settings
            JPanel panel1 = new JPanel();
            panel1.setVisible(true);
            panel1.setSize(200, 300);
            panel1.setBackground(Color.black);
            panel1.setLayout(scnLayout);
     
            // Add panels to frame
            progFrame.add(panel1);
     
            // Add components to panels
            panel1.add(lblName);
            panel1.add(txtName);
            panel1.add(btnNew);
     
            panel1.add(lblPCI);
            panel1.add(txtPCI);
            panel1.add(lblIP);
            panel1.add(txtIP);
            panel1.add(btnSave);
            panel1.add(btnFind);
            panel1.add(btnDelete);
            panel1.add(btnExit);
            panel1.add(lblDesc);
            panel1.add(lblDesc2);
            panel1.add(lblDesc3);
            panel1.add(lblDesc4);
            panel1.add(ListScrollPane);
     
            // Set locations of panel1 components
            scnLayout.putConstraint(SpringLayout.WEST, lblName, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblName, 200, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtName, 200, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtName, 35, SpringLayout.EAST, lblName);
            scnLayout.putConstraint(SpringLayout.NORTH, btnNew, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, btnNew, 25, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblPCI, 250, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblPCI, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtPCI, 250, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtPCI, 35, SpringLayout.EAST, lblPCI);
            scnLayout.putConstraint(SpringLayout.NORTH, lblIP, 300, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblIP, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtIP, 300, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtIP, 45, SpringLayout.EAST, lblIP);
            scnLayout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, btnSave, 150, SpringLayout.EAST, btnNew);
            scnLayout.putConstraint(SpringLayout.NORTH, btnFind, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnFind, -150, SpringLayout.WEST, btnDelete);
            scnLayout.putConstraint(SpringLayout.NORTH, btnDelete, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnDelete, -25, SpringLayout.EAST, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnExit, -25, SpringLayout.EAST, panel1);
            scnLayout.putConstraint(SpringLayout.SOUTH, btnExit, -20, SpringLayout.SOUTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, ListScrollPane, 200, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, ListScrollPane, 150, SpringLayout.SOUTH, txtIP);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc, 80, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc2, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc2, 10, SpringLayout.SOUTH, lblDesc);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc3, 385, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc3, 10, SpringLayout.SOUTH, lblDesc2);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc4, 300, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc4, -16, SpringLayout.NORTH, ListScrollPane);
     
            btnExit.addActionListener((ActionListener) this.btnExit);
            btnNew.addActionListener(this);
     
        }
     
     
        public void actionPerformed(ActionEvent e)
        {
            //System.exit(0);
           if (e.getSource() == btnExit)  { System.exit(0); }
           //repaint();
           }
     
        }

    Im puzzled lol..


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Weird ActioListener problem

    Quote Originally Posted by macko View Post
    if(e.getSource() == button1) { System.exit(0); }
    There is no button1 in your code. There is a btnExit though. In future post correct code and information to avoid confusion.

    You problem is a common one. You have 2 btnExit variables. An instance one and a local one in your constructor.

  3. The Following User Says Thank You to Junky For This Useful Post:

    macko (May 12th, 2011)

  4. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Weird ActioListener problem

    Oh i had figured it would be something tiny.. and sorry i should of just said the btnExit.

    Thanks for your reply.

Similar Threads

  1. ClassNotFoundException (bit weird)
    By chronoz13 in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 02:15 AM
  2. Replies: 27
    Last Post: February 17th, 2011, 05:42 PM
  3. [SOLVED] Weird calendar.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 3rd, 2011, 08:19 PM
  4. Weird thing with JFrame
    By Brt93yoda in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 05:00 PM
  5. Jsp weird problem
    By johniem in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 5th, 2010, 06:46 AM