943,626 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1026
  • Java RSS
Dec 22nd, 2007
0

Java Question

Expand Post »
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:
java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Dec 22nd, 2007
0

Re: Java Question

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,
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Dec 23rd, 2007
0

Re: Java Question

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 :

java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Planet code with Enum type
Next Thread in Java Forum Timeline: OutLookPanel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC