Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-09-2007, 07:28 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-25-2007, 09:33 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 739
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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....
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWT Layout JavaForums Java Blogs 0 12-31-2007 05:53 PM
Layout Managers gmioannou AWT / Swing 1 12-24-2007 05:12 AM
Help with Grid Layout coco AWT / Swing 1 08-06-2007 09:03 PM
Use the Layout in java lenny AWT / Swing 2 07-26-2007 08:20 PM
MiG Layout Manager 2.4 levent Java Announcements 0 05-16-2007 06:11 AM


All times are GMT +3. The time now is 06:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org