| | |
Java Question
![]() |
•
•
Join Date: Sep 2007
Posts: 56
Reputation:
Solved Threads: 0
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:
Let me know if you need a better explanation.
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)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingMenu implements ActionListener{ public static void main(String[] args) { SwingMenu s = new SwingMenu(); } public SwingMenu(){ Toolkit toolkit = Toolkit.getDefaultToolkit(); // Get size Dimension dimension = toolkit.getScreenSize(); JFrame frame = new JFrame(" "); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menubar = new JMenuBar(); JMenu filemenu = new JMenu("File"); JMenuItem fileItem1 = new JMenuItem("Exit"); filemenu.add(fileItem1); fileItem1.addActionListener(this); menubar.add(filemenu); frame.setJMenuBar(menubar); JTextArea textArea = new JTextArea(27,27); JScrollPane scrollPane = new JScrollPane(textArea); panel.add(scrollPane); JTextArea textArea2 = new JTextArea(3,3); JScrollPane scrollPane2 = new JScrollPane(textArea2); panel.add(scrollPane2); frame.add(panel); int height = dimension.height-30; frame.setSize(dimension.width,height); frame.setVisible(true); } public void actionPerformed(ActionEvent ev) { // this is where I want to handle the events } }
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,
Then you can access the components within the actionPerformed method as needed.
Hope this helps.
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingMenu implements ActionListener{ JMenuBar menuBar; JMenu filemenu; // etc public static void main(String[] args) { SwingMenu s = new SwingMenu(); } public SwingMenu(){ menuBar = new JMenuBar(); filemenu = new JMenu("File"); // etc } }
Hope this helps.
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 :
In this example I have written a separate class for event handling. you can use inner class or a separate class
Hope this helps
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)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingMenu { // Inner Class Implementation /* class actionListener implements ActionListener { public void actionPerformed(ActionEvent avt) { System.out.println("Triggered"); } } */ public static void main(String[] args) { SwingMenu swingMenu = new SwingMenu(); JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu("File"); JMenuItem demo = new JMenuItem("demo"); //demo.addActionListener(swingMenu.new actionListener()); // Using Inner class Implementation demo.addActionListener(new actionListener()); // Using a Separate class file.add(demo); menuBar.add(file); JFrame frame = new JFrame("Frame"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.NORTH,menuBar); frame.setSize(300,400); } } // Event Handling class class actionListener implements ActionListener { public void actionPerformed(ActionEvent avt) { System.out.println("Triggered"); } }
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
-Albert Einstein
![]() |
Similar Threads
- Simple Java question (Java)
- Traveling Salesman Problem ... question about Triangle Inequality (Computer Science)
- Java Question (Java)
- A Java Question?? (Java)
- Java Question (Java)
- Please help me whit this java question. (Java)
- java question (Java)
Other Threads in the Java Forum
- Previous Thread: Planet code with Enum type
- Next Thread: OutLookPanel
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





