Results 1 to 6 of 6
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Need Help With Button Events

    I'm trying to create an application that utilizes a form to gather info from the user. The form has 4 buttons - start, exit, clear and search. I'm trying to program the appropriate actions for these buttons but no matter what I also have an error. When I have tried to compile I get and error to the effect that the variable is non-static and cannot be run.

    Can anybody help me out?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class FlightInterface {
    
         
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            JLabel title = new JLabel("Flight Info");
            title.setFont(new Font("Serif", Font.BOLD, 30));
            JPanel bannerPanel = new JPanel();
            bannerPanel.setPreferredSize(new Dimension(300, 50));
            bannerPanel.add(title);
            
            JTextField nameInputField = new JTextField();
            JLabel inputAirlineName = new JLabel("Airline Name:     ");
            inputAirlineName.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel namePanel = new JPanel();
            namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
            namePanel.setPreferredSize(new Dimension(200, 20));
            namePanel.add(inputAirlineName);
            namePanel.add(nameInputField);
            
            JLabel inputFlightNum = new JLabel("Flight Number:   ");
            inputFlightNum.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField flightNumField = new JTextField();          
            JPanel flightNumPanel = new JPanel();
            flightNumPanel.setLayout(new BoxLayout(flightNumPanel, BoxLayout.X_AXIS));
            flightNumPanel.setPreferredSize(new Dimension(200, 20));
            flightNumPanel.add(inputFlightNum);
            flightNumPanel.add(flightNumField);
            
            JLabel inputOrigin = new JLabel("City of Origin:    ");
            inputOrigin.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField originField = new JTextField();          
            JPanel originPanel = new JPanel();
            originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.X_AXIS));
            originPanel.setPreferredSize(new Dimension(200, 20));
            originPanel.add(inputOrigin);
            originPanel.add(originField);
            
            JLabel inputDestination = new JLabel("Destination City: ");
            inputDestination.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField destinationField = new JTextField();          
            JPanel destinationPanel = new JPanel();
            destinationPanel.setLayout(new BoxLayout(destinationPanel, BoxLayout.X_AXIS));
            destinationPanel.setPreferredSize(new Dimension(200, 20));
            destinationPanel.add(inputDestination);
            destinationPanel.add(destinationField);
            
            JPanel blankPanel = new JPanel();
            blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS));
            blankPanel.setPreferredSize(new Dimension(200, 20));
            
            JButton startButton = new JButton("Get Data");
            startButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton exitButton = new JButton("Exit");
            exitButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton searchButton = new JButton("Search");
            searchButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton clearButton = new JButton("Clear");
            clearButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.setPreferredSize(new Dimension(250, 20));
            buttonPanel.add(startButton);
            buttonPanel.add(clearButton);
            buttonPanel.add(searchButton);
            buttonPanel.add(exitButton);
            
                  
            JPanel mainPanel = new JPanel();
            mainPanel.add(bannerPanel, BorderLayout.NORTH);
            mainPanel.add(namePanel, BorderLayout.CENTER);
            mainPanel.add(flightNumPanel, BorderLayout.CENTER);
            mainPanel.add(originPanel, BorderLayout.CENTER);
            mainPanel.add(destinationPanel, BorderLayout.CENTER);
            mainPanel.add(blankPanel, BorderLayout.CENTER);
            mainPanel.add(buttonPanel, BorderLayout.SOUTH);
            
            JFrame f = new JFrame(); // creates new instance of JFrame so graph can be displayed
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // tells JFrame to close on exit
            f.getContentPane().add(mainPanel);
            f.setSize(300, 265); // sets size of JFrame
            f.setLocation(200, 200); // sets location of JFrame
            f.setVisible(true); // makes JFrame visible
            
                 
        } // end main
        
    } // end class
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Your code compiles and runs okay. Here's a suggestion.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class FI {
        private JPanel getContent() {
            JLabel title = new JLabel("Flight Info");
            title.setFont(new Font("Serif", Font.BOLD, 30));
            JPanel bannerPanel = new JPanel();
            bannerPanel.setPreferredSize(new Dimension(300, 50));
            bannerPanel.add(title);
            
            JTextField nameInputField = new JTextField();
            JLabel inputAirlineName = new JLabel("Airline Name:     ");
            inputAirlineName.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel namePanel = new JPanel();
            namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
            namePanel.setPreferredSize(new Dimension(200, 20));
            namePanel.add(inputAirlineName);
            namePanel.add(nameInputField);
            
            JLabel inputFlightNum = new JLabel("Flight Number:   ");
            inputFlightNum.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField flightNumField = new JTextField();          
            JPanel flightNumPanel = new JPanel();
            flightNumPanel.setLayout(new BoxLayout(flightNumPanel, BoxLayout.X_AXIS));
            flightNumPanel.setPreferredSize(new Dimension(200, 20));
            flightNumPanel.add(inputFlightNum);
            flightNumPanel.add(flightNumField);
            
            JLabel inputOrigin = new JLabel("City of Origin:    ");
            inputOrigin.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField originField = new JTextField();          
            JPanel originPanel = new JPanel();
            originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.X_AXIS));
            originPanel.setPreferredSize(new Dimension(200, 20));
            originPanel.add(inputOrigin);
            originPanel.add(originField);
            
            JLabel inputDestination = new JLabel("Destination City: ");
            inputDestination.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField destinationField = new JTextField();          
            JPanel destinationPanel = new JPanel();
            destinationPanel.setLayout(new BoxLayout(destinationPanel, BoxLayout.X_AXIS));
            destinationPanel.setPreferredSize(new Dimension(200, 20));
            destinationPanel.add(inputDestination);
            destinationPanel.add(destinationField);
    
            JPanel blankPanel = new JPanel();
            blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS));
            blankPanel.setPreferredSize(new Dimension(200, 20));
    
            JButton startButton = new JButton("Get Data");
            startButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton exitButton = new JButton("Exit");
            exitButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton searchButton = new JButton("Search");
            searchButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton clearButton = new JButton("Clear");
            clearButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    //        buttonPanel.setPreferredSize(new Dimension(250, 20));
            buttonPanel.add(startButton);
            buttonPanel.add(clearButton);
            buttonPanel.add(searchButton);
            buttonPanel.add(exitButton);
    
            JPanel mainPanel = new JPanel();
            System.out.println("JPanel default layout manager = " +
                                mainPanel.getLayout().getClass().getName());
            mainPanel.add(bannerPanel, BorderLayout.NORTH);
            mainPanel.add(namePanel, BorderLayout.CENTER);
            mainPanel.add(flightNumPanel, BorderLayout.CENTER);
            mainPanel.add(originPanel, BorderLayout.CENTER);
            mainPanel.add(destinationPanel, BorderLayout.CENTER);
            mainPanel.add(blankPanel, BorderLayout.CENTER);
            mainPanel.add(buttonPanel, BorderLayout.SOUTH);
            return mainPanel;
        }
    
        public static void main(String[] args) {
            // Using the variable "app" will help avoid problems
            // with the static modifier in your class.
            FI app = new FI();
            // creates new instance of JFrame so graph can be displayed
            JFrame f = new JFrame();
            // tells JFrame to close on exit
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(app.getContent());
            f.setSize(300, 265);      // sets size of JFrame
            f.setLocation(200, 200);  // sets location of JFrame
            f.setVisible(true);       // makes JFrame visible
        } // end main
    } // end class

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired,

    I will give it a try. Thanks for the tip. I'm starting to get it as I actually handcoded the form which I was quite impressed with myself for.

  4. #4
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired,

    I'm still getting error messages but at least they are different ones. Here is the code I have. I'm just trying to code the exit button first and once I figured that out I thought I would add in the others.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class FlightInterface {
        
        private JPanel getContent() {
            
            JLabel title = new JLabel("Flight Info");
            title.setFont(new Font("Serif", Font.BOLD, 30));
            JPanel bannerPanel = new JPanel();
            bannerPanel.setPreferredSize(new Dimension(300, 50));
            bannerPanel.add(title);
            
            JTextField nameInputField = new JTextField();
            JLabel inputAirlineName = new JLabel("Airline Name:     ");
            inputAirlineName.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel namePanel = new JPanel();
            namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
            namePanel.setPreferredSize(new Dimension(200, 20));
            namePanel.add(inputAirlineName);
            namePanel.add(nameInputField);
            
            JLabel inputFlightNum = new JLabel("Flight Number:   ");
            inputFlightNum.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField flightNumField = new JTextField();
            JPanel flightNumPanel = new JPanel();
            flightNumPanel.setLayout(new BoxLayout(flightNumPanel, BoxLayout.X_AXIS));
            flightNumPanel.setPreferredSize(new Dimension(200, 20));
            flightNumPanel.add(inputFlightNum);
            flightNumPanel.add(flightNumField);
            
            JLabel inputOrigin = new JLabel("City of Origin:    ");
            inputOrigin.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField originField = new JTextField();
            JPanel originPanel = new JPanel();
            originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.X_AXIS));
            originPanel.setPreferredSize(new Dimension(200, 20));
            originPanel.add(inputOrigin);
            originPanel.add(originField);
            
            JLabel inputDestination = new JLabel("Destination City: ");
            inputDestination.setFont(new Font("Serif", Font.PLAIN, 14));
            JTextField destinationField = new JTextField();
            JPanel destinationPanel = new JPanel();
            destinationPanel.setLayout(new BoxLayout(destinationPanel, BoxLayout.X_AXIS));
            destinationPanel.setPreferredSize(new Dimension(200, 20));
            destinationPanel.add(inputDestination);
            destinationPanel.add(destinationField);
            
            JPanel blankPanel = new JPanel();
            blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS));
            blankPanel.setPreferredSize(new Dimension(200, 20));
            
            JButton startButton = new JButton("Get Data");
            startButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton exitButton = new JButton("Exit");
            exitButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton searchButton = new JButton("Search");
            searchButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JButton clearButton = new JButton("Clear");
            clearButton.setFont(new Font("Serif", Font.PLAIN, 14));
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.add(startButton);
            buttonPanel.add(clearButton);
            buttonPanel.add(searchButton);
            buttonPanel.add(exitButton);
            
            exitButton.addActionListener(this);
                   
            JPanel mainPanel = new JPanel();
            System.out.println("JPanel default layout manager = " +
                    mainPanel.getLayout().getClass().getName());
            mainPanel.add(bannerPanel, BorderLayout.NORTH);
            mainPanel.add(namePanel, BorderLayout.CENTER);
            mainPanel.add(flightNumPanel, BorderLayout.CENTER);
            mainPanel.add(originPanel, BorderLayout.CENTER);
            mainPanel.add(destinationPanel, BorderLayout.CENTER);
            mainPanel.add(blankPanel, BorderLayout.CENTER);
            mainPanel.add(buttonPanel, BorderLayout.SOUTH);
            return mainPanel;
        }
        
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == exitButton){
                System.exit(0);
            }
         
        }
        
        public static void main(String[] args) {
            // Using the variable "app" will help avoid problems
            // with the static modifier in your class.
            FlightInterface app = new FlightInterface();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(app.getContent());
            f.setSize(300, 265);      // sets size of JFrame
            f.setLocation(200, 200);  // sets location of JFrame
            f.setVisible(true);       // makes JFrame visible
        } // end main
    }
    Here are the error messages that I get:

    E:\Intro OOP\FlightInterface\src\flightinterface\FlightInte rface.java:84: illegal start of expression
    public void actionPerformed(ActionEvent e){
    E:\Intro OOP\FlightInterface\src\flightinterface\FlightInte rface.java:89: ';' expected
    }

    Thanks for your help

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I copied, pasted (into a new folder) and compiled your last posted code and got these in the console:
    Java Code:
    C:\jexp\flightInterface>javac FlightInterface.java
    FlightInterface.java:84: cannot find symbol
    symbol  : class ActionEvent
    location: class FlightInterface
        public void actionPerformed(ActionEvent e){
                                    ^
    FlightInterface.java:69: addActionListener(java.awt.event.ActionListener) in jav
    ax.swing.AbstractButton cannot be applied to (FlightInterface)
            exitButton.addActionListener(this);
                      ^
    FlightInterface.java:85: cannot find symbol
    symbol  : variable exitButton
    location: class FlightInterface
            if(e.getSource() == exitButton){
                                ^
    3 errors
    Make the following changes to compile and run okay:
    Java Code:
    // Add import statement.
    import java.awt.event.*;
    
    // Implement ActionListener so "this" can be an ActionListener
    public class FlightInterface implements ActionListener {
        // Declare exitButton as a member variable.
        // This puts it in class scope where it can be
        // accessed inside the actionPerformed method.
        JButton exitButton;
    
            startButton.setFont(new Font("Serif", Font.PLAIN, 14));
            // Remove the type declaration from the instantiation so
            // that exitButton is not a local variable hiding the
            // member variable (of the same name) which would remain
            // uninstantiated and thus have a value of null, causing
            // NullPointerExceptions when you try to access it in
            // the actionPerformed method.
            exitButton = new JButton("Exit");
    I did not get the compile errors you reported. The console errors give the line number of the trouble to help track it down. Maybe you omitted a semi-colon somewhere above the actionPerformed method.

  6. #6
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired once again you come through for me. I had the closing bracket for the JPanel method after the ActionPerformed method instead of before. I changed that and made the changes you suggested before and now it works. Thanks

Similar Threads

  1. How to get the Call events in J2ME
    By krmlkr in forum CLDC and MIDP
    Replies: 1
    Last Post: 03-08-2008, 09:39 AM
  2. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM
  3. To capture events with struts
    By Ed in forum Web Frameworks
    Replies: 2
    Last Post: 07-04-2007, 06:01 AM
  4. Help, events in Jbutton class
    By Heather in forum Java Applets
    Replies: 2
    Last Post: 06-30-2007, 04:32 PM

Posting Permissions

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