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

    Default Help with JButton and layout

    I'm creating an application that manages the flights for an airline. I'm having trouble getting the exit button to work. I have not gotten to the point of programming the others yet as I will be doing that as I add those features. Any help would be appreciated.

    Thanks

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    import javax.swing.*;
    
    public class FlightManager implements ActionListener{
        
        // JDBC driver name and database url
        static final String DRIVER = "com.mysql.jdbc.Driver";
        static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager";
        private JTextField fn = new JTextField(15);
        private JTextField oc  = new JTextField(15);
        private JTextField dc = new JTextField(15);
        private JTextField dt = new JTextField(15);
        private JLabel fnl = new JLabel("Flight Number: ");
        private JLabel ocl = new JLabel("City of Origin: ");
        private JLabel dcl = new JLabel("Destination City: ");
        private JLabel dtl = new JLabel("Departure Time: ");
        private int flightNum;
        private String originCity;
        private String destinationCity;
        private String departure;
        private JButton addButton;
        private JButton removeButton;
        private JButton searchButton;
        private JButton exitButton;
            
        public static void main(String[] args) throws SQLException {
    
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
            
                Class.forName(DRIVER);
                connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly");
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
    
            FlightManager fm = new FlightManager();
            Container pc = new Container();
            pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS));
            JPanel blank = new JPanel();
            pc.add(blank);
            pc.add(fm.getFlightInputPanel());
            pc.add(fm.getOriginPanel());
            pc.add(fm.getDestinationPanel());
            pc.add(fm.getDepartureTimePanel());
            
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(pc, "First");
            f.add(fm.getButtonPanel(), "Last"); // adds button panel to JFrame
            f.setSize(500, 200);
            f.setLocation(200,200);
            f.setVisible(true);
            
            
            
        }
        
        private JPanel getFlightInputPanel(){
            FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
            JPanel fnp = new JPanel();
            fnp.add(fnl);
            fnp.add(fn);
            
            return fnp;
        }
        
        private JPanel getOriginPanel(){
            FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
            JPanel ocp = new JPanel();
            ocp.add(ocl);
            ocp.add(oc);
            
            return ocp;
        }
        
        private JPanel getDestinationPanel(){
            FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
            JPanel dcp = new JPanel();
            dcp.add(dcl);
            dcp.add(dc);
            
            return dcp;
        }
        
        private JPanel getDepartureTimePanel(){
            FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
            JPanel dtp = new JPanel();
            dtp.add(dtl);
            dtp.add(dt);
            
            return dtp;
        }
        
        private JPanel getButtonPanel(){
            
            FlowLayout buttonLayout = new FlowLayout(FlowLayout.CENTER);
            JPanel buttonPanel = new JPanel();
            addButton = new JButton("Add Flight");
            addButton.addActionListener(this);
            removeButton = new JButton("Delete Flight");
            removeButton.addActionListener(this);
            searchButton = new JButton("Search For Flight");
            searchButton.addActionListener(this);
            exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            buttonPanel.add(addButton);
            buttonPanel.add(removeButton);
            buttonPanel.add(searchButton);
            buttonPanel.add(exitButton);
         
            return buttonPanel;
        }
        
        
        // processes actions for when buttons are pushed
        public void actionPerformed(ActionEvent e) {
    
            // action to take if add ball button is pushed
            if(e.getSource() == addButton)
                
                flightNum = Integer.parseInt(fn.getText());
                originCity = oc.getText();
                destinationCity = dc.getText();
                departure = dt.getText();
                
                // clears out current contents of JTextFields
                fn = null;
                oc = null;
                dc = null;
                dt = null;
            
            // action to take if delete ball button is pushed
            if(e.getSource() == searchButton)
    
            if(e.getSource() == removeButton)
            
                
            // action to take if exit button is pushed
            if(e.getSource() == exitButton)
                System.exit(0);
        }
       }
    Last edited by adlb1300; 12-10-2007 at 06:12 AM. Reason: Solved layout problem so no longer need help on it; also posted updated code with corrected layout

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Glad you solved the problem you were having. I haven't ran or tested this code, but is the code above the one that is now fixed? What exactly was the problem, or rather, what exactly did you find was the solution to your exit button problem?

    EDIT: I just ran this particular code, so it looks as if the existing exit button code on this particular post is not fixed....

Similar Threads

  1. Layout Managers
    By gmioannou in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 05:12 AM
  2. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:03 PM
  3. Use the Layout in java
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 07-26-2007, 08:20 PM
  4. MiG Layout Manager 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-16-2007, 06:11 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
  •