Java Question

Reply

Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

Java Question

 
0
  #1
Dec 22nd, 2007
I'm new to Java and I'm wonder if someone can help me with this.
I have a class that's constructor makes a Gui window, and it's main calls this constructor. The problem is the event handling doesn't work because the event handling code can't access the GUI window variables (It just gives errors). I tried putting the Gui code inside main,but then I get errors about accessing non static variables from inside the static main.
This is the code:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SwingMenu implements ActionListener{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. SwingMenu s = new SwingMenu();
  10.  
  11.  
  12.  
  13. }
  14.  
  15. public SwingMenu(){
  16. Toolkit toolkit = Toolkit.getDefaultToolkit();
  17.  
  18. // Get size
  19. Dimension dimension = toolkit.getScreenSize();
  20. JFrame frame = new JFrame(" ");
  21. JPanel panel = new JPanel();
  22.  
  23. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  24. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. JMenuBar menubar = new JMenuBar();
  26. JMenu filemenu = new JMenu("File");
  27. JMenuItem fileItem1 = new JMenuItem("Exit");
  28. filemenu.add(fileItem1);
  29. fileItem1.addActionListener(this);
  30. menubar.add(filemenu);
  31. frame.setJMenuBar(menubar);
  32. JTextArea textArea = new JTextArea(27,27);
  33. JScrollPane scrollPane = new JScrollPane(textArea);
  34. panel.add(scrollPane);
  35. JTextArea textArea2 = new JTextArea(3,3);
  36. JScrollPane scrollPane2 = new JScrollPane(textArea2);
  37. panel.add(scrollPane2);
  38. frame.add(panel);
  39. int height = dimension.height-30;
  40. frame.setSize(dimension.width,height);
  41. frame.setVisible(true);
  42. }
  43.  
  44.  
  45. public void actionPerformed(ActionEvent ev) {
  46.  
  47. // this is where I want to handle the events
  48. }
  49. }
Let me know if you need a better explanation.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Java Question

 
0
  #2
Dec 22nd, 2007
I think you might be better off listing your GUI components outside the methods. You still initialise your components inside the swingMenu method, but doing it this way gives global access within the class. For example,
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SwingMenu implements ActionListener{
  6. JMenuBar menuBar;
  7. JMenu filemenu;
  8. // etc
  9. public static void main(String[] args) {
  10. SwingMenu s = new SwingMenu();
  11. }
  12.  
  13. public SwingMenu(){
  14. menuBar = new JMenuBar();
  15. filemenu = new JMenu("File");
  16. // etc
  17. }
  18. }
Then you can access the components within the actionPerformed method as needed.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Java Question

 
0
  #3
Dec 23rd, 2007
Hi curt22 ,

It's best practice to separate event handling method from the event source class(class which contains UI widgets). So that later it will be easy to change that without affecting event source class.

Consider this example :

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SwingMenu {
  6.  
  7.  
  8. // Inner Class Implementation
  9.  
  10. /*
  11.  
  12. class actionListener implements ActionListener {
  13.   public void actionPerformed(ActionEvent avt) {
  14.   System.out.println("Triggered");
  15.  }
  16. } */
  17.  
  18. public static void main(String[] args) {
  19.  
  20. SwingMenu swingMenu = new SwingMenu();
  21.  
  22.  
  23. JMenuBar menuBar = new JMenuBar();
  24. JMenu file = new JMenu("File");
  25. JMenuItem demo = new JMenuItem("demo");
  26.  
  27. //demo.addActionListener(swingMenu.new actionListener()); // Using Inner class Implementation
  28.  
  29. demo.addActionListener(new actionListener()); // Using a Separate class
  30.  
  31.  
  32. file.add(demo);
  33. menuBar.add(file);
  34.  
  35. JFrame frame = new JFrame("Frame");
  36. frame.setVisible(true);
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. frame.getContentPane().add(BorderLayout.NORTH,menuBar);
  39. frame.setSize(300,400);
  40. }
  41. }
  42.  
  43. // Event Handling class
  44. class actionListener implements ActionListener {
  45. public void actionPerformed(ActionEvent avt) {
  46. System.out.println("Triggered");
  47. }
  48. }

In this example I have written a separate class for event handling. you can use inner class or a separate class

Hope this helps
The important thing is not to stop questioning. Curiosity has its own reason for existing.
-Albert Einstein
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC