GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2007, 06:32
RobRob47 RobRob47 is offline
New Member
 
Join Date: Mar 2007
Posts: 2
RobRob47 is on a distinguished road

Anything i can do with static addActionListener(this);


I'm new to java and started creating a program with a menu and menubars which i would like to have addActionListener(this); but seems i can't do this due to it being static. Is there a way around this?

JAVA Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Main implements ActionListener 
{
	private JPanel JPanelNorth, JPanelCenter, JPanelWest, JPanelSouth, JPanelEast;

    static JMenuItem menuItem;
	//public static JFrame window = new JFrame("Border Layout");
	
	
	
	static JMenu menu, submenu;
	static JMenuItem menuSave, menuQuit, menuAbout, menuNew;
	static JFrame frame;
    static ActionListener actionListener;
	static	JMenuBar menuBar;
    private static JPanel jMenuNorth;
	
	
	
	
    public static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane)

    
    {
        if (!(pane.getLayout() instanceof BorderLayout)) 
        {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return;
        }

        if (RIGHT_TO_LEFT) 
        {
            pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }
        
        
        	JPanel JPanelNorth = new JPanel();
        	JPanelNorth.setPreferredSize(new Dimension(200, 200));
        	JPanelNorth.setBackground(new Color(0,0,0));
        	pane.add(JPanelNorth, BorderLayout.PAGE_START);

        	
        	
            jMenuNorth = new JPanel();
            pane.add(jMenuNorth, BorderLayout.PAGE_START);
            jMenuNorth.setBackground(new Color(245,245,255));
            
        	
        	
    		menuBar = new JMenuBar();
    		menu = new JMenu ("File");
    		
      		JMenuItem menuNew = new JMenuItem("New");
      		menu.add(menuNew);
    		menuNew.addActionListener(this);
    		menuBar.add(menu);
    		        		
      		JMenuItem menuSave = new JMenuItem("Save");
      		menu.add(menuSave);
      		
      		JMenuItem menuQuit = new JMenuItem("Quit");
      		menu.add(menuQuit);
    		menuQuit.addActionListener(this);
    		menuBar.add(menu);
    	
    		
    		menu = new JMenu ("Help");
    		menuAbout = new JMenuItem("About");
      		menu.add(menuAbout);
    		menuAbout.addActionListener(this);
    		menuBar.add(menu);
    		
    		

    		
        //Make the center component big, since that's the
        //typical usage of BorderLayout.
        JPanel JPanelCenter = new JPanel();
        JPanelCenter.setPreferredSize(new Dimension(700, 700));
        JPanelCenter.setBackground(new Color(100,100,100));
        pane.add(JPanelCenter, BorderLayout.CENTER);
        

        JPanel JPanelWest = new JPanel();
        JPanelWest.setPreferredSize(new Dimension(200, 200));
        JPanelWest.setBackground(new Color(200,200,200));
        pane.add(JPanelWest, BorderLayout.LINE_START);
        
        JPanel JPanelSouth = new JPanel();
        JPanelSouth.setPreferredSize(new Dimension(200, 20));
        JPanelSouth.setBackground(new Color(10,200,150));
        pane.add(JPanelSouth, BorderLayout.PAGE_END);

        JPanel JPanelEast = new JPanel();
        JPanelEast.setPreferredSize(new Dimension(200, 200));
        JPanelEast.setBackground(new Color(255,255,255));
        pane.add(JPanelEast, BorderLayout.LINE_END);
        

    }


    private static void createAndShowGUI() 
    {
    	  //Create and set up the window. 	
    	
        JFrame window = new JFrame("BorderLayout");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        

        
        
        //Set up the content pane.
        addComponentsToPane(window.getContentPane());
        
		window.setJMenuBar(menuBar);
        
        
        //Display the window.
        window.setSize(1024, 720);
        window.setVisible(true);
    }

    public static void main(String[] args) 
    {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }

}
  #2  
Old 26-Mar-2007, 11:30
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 924
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Anything i can do with static addActionListener(this);


How about eliminating the need for 'static context'?...

1. EXCEPT for the line with main, which needs the static keyword, eliminate all other static keywords.

2. Then change this:
JAVA Code:
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
to this
JAVA Code:
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            Main m = new Main();

            public void run()
            {
                m.createAndShowGUI();
            }
        });
3. Then add the actionPerformed() / addActionListener() where desired.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 26-Mar-2007, 12:17
RobRob47 RobRob47 is offline
New Member
 
Join Date: Mar 2007
Posts: 2
RobRob47 is on a distinguished road

Re: Anything i can do with static addActionListener(this);


Nice one, thanks it worked, now onto my next problem :> i'll give it a try first.
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
draw in image jafar FLTK Forum 2 19-Sep-2006 10:51
Handling keyup events. harroldm FLTK Forum 3 24-Aug-2006 08:48
addActionListener(this) conflicts with static _Y_ Java Forum 2 03-Aug-2006 01:47
calling static member function through object alcoholic CPP / C++ Forum 3 06-Dec-2005 06:10
error related to static member functions and varaibles kai85 CPP / C++ Forum 3 20-May-2005 06:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.