ActionListener

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 10
Reputation: shaqnolysis is an unknown quantity at this point 
Solved Threads: 0
shaqnolysis shaqnolysis is offline Offline
Newbie Poster

ActionListener

 
0
  #1
Nov 8th, 2007
Lemme first greet all my beloved fellows and i would love to give you all credits for helping each other with problems concerning java!!!

I have a problem with listening methods in my code.the only respond i get is the exit part of my buttons....please i would be happy to get help from you fellows with my actionlistening methods....i was testing with the exit method.
  1. import java.awt.*; import java.awt.event.*; import javax.swing.*;
  2.  
  3. public class Layers // driver function
  4. {
  5.  
  6. public static void main(String args[])
  7. {
  8. try {UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName());}
  9. catch (Exception e) {;}
  10. Layer_Model model = new Layer_Model();
  11. Layer_View view = new Layer_View("TRAIN RESERVATION SYSTEM:RESERVATION");
  12. Layer_Controller controller = new Layer_Controller(model,view);
  13. }
  14. }
  15.  
  16. class Layer_View extends JFrame//set up GUI
  17. {
  18. String reservertions[] = {"Surname","Firstname",
  19. "ID Number","Email","Phone Number","Place of Departure","No-Of-Tickets",
  20. "Time of Departure","Time of Arrival"};
  21.  
  22. private JTextField input = new JTextField(7);
  23. private JTextField inputName = new JTextField(7);
  24. private JTextField inputID = new JTextField(7);
  25. private JTextField inputEmail = new JTextField(7);
  26. private JTextField inputNumber = new JTextField(7);
  27. private JTextField inputDeparture = new JTextField(7);
  28. private JTextField inputDestination = new JTextField(7);
  29. private JTextField inputTickets = new JTextField(7);
  30. private JTextField inputDepatime = new JTextField(7);
  31. private JTextField inputArrival = new JTextField(7);
  32.  
  33. //declare button components
  34. JButton price = new JButton("PRICE");
  35. JButton submit = new JButton("SUBMIT");
  36. JButton cancel = new JButton("CANCEL");
  37. JButton exit = new JButton("EXIT");
  38. JPanel buttons = new JPanel(new GridLayout(4,1,2,2));
  39. JPanel but = new JPanel(new GridLayout(4,1,2,2));
  40.  
  41. //BorderLayout pane to hold input/button JPanels
  42. //border layout to hold inputs and buttons
  43. JPanel report = new JPanel(new GridLayout(4,1,2,2));
  44. JPanel main = new JPanel(new BorderLayout());
  45.  
  46. class Layer_Model // provides working functionality
  47. {;}
  48.  
  49. public Layer_View(String title)// constructor method for GUI
  50. {
  51. super(title);setBounds(100,100,500,180);setResizable(true);
  52. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  53. //construct input fieldset
  54. for (int i=0; i<9;i++)
  55. {
  56. JLabel lab = new JLabel(reservertions[i]);
  57. lab.setForeground(Color.black);
  58. JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
  59.  
  60. p.add(lab); switch (i) {
  61. case 0: p.add(input); break;
  62. case 1: p.add(inputName); break;
  63. case 2: p.add(inputID); break;
  64. case 3: p.add(inputEmail); break;
  65. case 4: p.add(inputNumber); break;
  66. case 5: p.add(inputDeparture); break;
  67. case 6: p.add(inputDestination); break;
  68. case 7: p.add(inputTickets); break;
  69. case 8: p.add(inputDepatime); break;
  70. case 9: p.add(inputArrival); break; }
  71.  
  72. report.add(p);
  73. }
  74. //constructing buttons
  75. //construct button controls
  76. buttons.add(price); but.add(cancel);
  77. buttons.add(submit); but.add(exit);
  78.  
  79. //completing the panel assembly
  80. main.add("Center",report); main.add("West",buttons); main.add("East",but);
  81.  
  82.  
  83.  
  84. this.getContentPane().add("Center",main);setVisible(true);
  85. }
  86.  
  87.  
  88.  
  89.  
  90. class Layer_Controller implements ActionListener
  91. {
  92. Layer_Model model; Layer_View view;
  93.  
  94. public Layer_Controller(Layer_Model model, Layer_View view)
  95. {
  96. this.model = model; this.view = view;
  97. view.buttonActionListeners(this);
  98.  
  99. }
  100.  
  101.  
  102. // methods to deal with the interactions performed on the panel.
  103. public void actionPerformed(ActionEvent ae)
  104. {
  105. String action_com = ae.getActionCommand();
  106. if (action_com.equals("price")) {System.out.print("what price do you want to set");}
  107. else if (action_com.equals("exit")) {System.exit(0);}
  108. else if (action_com.equals("submit")) {System.exit(0);}
  109. else if (action_com.equals("cancel")) {System.exit(0);}
  110. }
  111.  
  112.  
  113. }
  114.  
  115.  
  116. public void buttonActionListeners(ActionListener al)
  117. {
  118. price.addActionListener(al);price.setActionCommand("price");
  119. exit.addActionListener(al);exit.setActionCommand("exit");
  120. submit.addActionListener(al);submit.setActionCommand("submit");
  121. cancel.addActionListener(al);cancel.setActionCommand("cancel");
  122. }
  123.  
  124. }
Last edited by ~s.o.s~; Nov 8th, 2007 at 4:07 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,503
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: ActionListener

 
0
  #2
Nov 8th, 2007
Please use code tags when you post code.
Reposting it for you here with tags and formatting, but it still will not compile as you have posted it. You mangled the class definitions by embedding them haphazardly in one another.
  1. import java.awt.*; import java.awt.event.*; import javax.swing.*;
  2.  
  3. public class Layers // driver function
  4. {
  5.  
  6. public static void main(String args[]) {
  7. try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e) {;}
  8. Layer_Model model = new Layer_Model();
  9. Layer_View view = new Layer_View("TRAIN RESERVATION SYSTEM:RESERVATION");
  10. Layer_Controller controller = new Layer_Controller(model,view);
  11. }
  12. }
  13.  
  14. class Layer_View extends JFrame//set up GUI
  15. {
  16. String reservertions[] = {"Surname","Firstname",
  17. "ID Number","Email","Phone Number","Place of Departure","No-Of-Tickets",
  18. "Time of Departure","Time of Arrival"};
  19.  
  20. private JTextField input = new JTextField(7);
  21. private JTextField inputName = new JTextField(7);
  22. private JTextField inputID = new JTextField(7);
  23. private JTextField inputEmail = new JTextField(7);
  24. private JTextField inputNumber = new JTextField(7);
  25. private JTextField inputDeparture = new JTextField(7);
  26. private JTextField inputDestination = new JTextField(7);
  27. private JTextField inputTickets = new JTextField(7);
  28. private JTextField inputDepatime = new JTextField(7);
  29. private JTextField inputArrival = new JTextField(7);
  30.  
  31. //declare button components
  32. JButton price = new JButton("PRICE");
  33. JButton submit = new JButton("SUBMIT");
  34. JButton cancel = new JButton("CANCEL");
  35. JButton exit = new JButton("EXIT");
  36. JPanel buttons = new JPanel(new GridLayout(4,1,2,2));
  37. JPanel but = new JPanel(new GridLayout(4,1,2,2));
  38.  
  39. //BorderLayout pane to hold input/button JPanels
  40. //border layout to hold inputs and buttons
  41. JPanel report = new JPanel(new GridLayout(4,1,2,2));
  42. JPanel main = new JPanel(new BorderLayout());
  43.  
  44. class Layer_Model // provides working functionality
  45. {;}
  46.  
  47. public Layer_View(String title)// constructor method for GUI
  48. {
  49. super(title);setBounds(100,100,500,180);setResizable(true);
  50. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  51. //construct input fieldset
  52. for (int i=0; i<9;i++) {
  53. JLabel lab = new JLabel(reservertions[i]);
  54. lab.setForeground(Color.black);
  55. JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
  56.  
  57. p.add(lab); switch (i) {
  58. case 0: p.add(input); break;
  59. case 1: p.add(inputName); break;
  60. case 2: p.add(inputID); break;
  61. case 3: p.add(inputEmail); break;
  62. case 4: p.add(inputNumber); break;
  63. case 5: p.add(inputDeparture); break;
  64. case 6: p.add(inputDestination); break;
  65. case 7: p.add(inputTickets); break;
  66. case 8: p.add(inputDepatime); break;
  67. case 9: p.add(inputArrival); break; }
  68.  
  69. report.add(p);
  70. }
  71. //constructing buttons
  72. //construct button controls
  73. buttons.add(price); but.add(cancel);
  74. buttons.add(submit); but.add(exit);
  75.  
  76. //completing the panel assembly
  77. main.add("Center",report); main.add("West",buttons); main.add("East",but);
  78.  
  79.  
  80.  
  81. this.getContentPane().add("Center",main);setVisible(true);
  82. }
  83.  
  84.  
  85.  
  86.  
  87. class Layer_Controller implements ActionListener {
  88. Layer_Model model; Layer_View view;
  89.  
  90. public Layer_Controller(Layer_Model model, Layer_View view) {
  91. this.model = model; this.view = view;
  92. view.buttonActionListeners(this);
  93.  
  94. }
  95.  
  96.  
  97. // methods to deal with the interactions performed on the panel.
  98. public void actionPerformed(ActionEvent ae) {
  99. String action_com = ae.getActionCommand();
  100. if (action_com.equals("price")) {System.out.print("what price do you want to set");} else if (action_com.equals("exit")) {System.exit(0);} else if (action_com.equals("submit")) {System.exit(0);} else if (action_com.equals("cancel")) {System.exit(0);}
  101. }
  102.  
  103.  
  104. }
  105.  
  106.  
  107. public void buttonActionListeners(ActionListener al) {
  108. price.addActionListener(al);price.setActionCommand("price");
  109. exit.addActionListener(al);exit.setActionCommand("exit");
  110. submit.addActionListener(al);submit.setActionCommand("submit");
  111. cancel.addActionListener(al);cancel.setActionCommand("cancel");
  112. }
  113.  
  114. }
Last edited by Ezzaral; Nov 8th, 2007 at 12:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shaqnolysis is an unknown quantity at this point 
Solved Threads: 0
shaqnolysis shaqnolysis is offline Offline
Newbie Poster

Re: ActionListener

 
0
  #3
Nov 13th, 2007
thanks alot my brother.am working on the code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC