Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By seniorstatus

Thread: Implement actionlistener with panel

  1. #1
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Implement actionlistener with panel

    im designing a interface that models the datapath of a pipelined mips processor. i have two panels. one panel that holds the menu and user input, etc. the second panel is holding an image for now which will eventually be the basis of mapping the datapath. the code worked fine until i tried to add the two panels. the errors im getting are:

    At line 9:
    The type GUI must implement the inherited abstract method
    ActionListener.actionPerformed(ActionEvent)

    At various other lines:
    Multiple markers at this line
    - The static field ListBoxes.Instructions should be accessed in a static way
    - The method addActionListener(ActionListener) in the type List is not
    applicable for the arguments (new JPanel(){})

    the actions are defined in other classes. how do i resolve these errors?
    Java Code:
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
     
    public class GUI implements ActionListener
    {
    	JPanel MenuPanel, DataPanel;
    	JPanel mainPane;
    	ImageIcon icon;
    	
        public GUI()
        {
            
            icon = new ImageIcon("src/background3.jpg");
            MenuPanel = new JPanel()
            {
            	public UI usr = new UI();//user interface call
            	public Stages stg = new Stages(); //stages class call
            	public ListBoxes lb = new ListBoxes();//list box interface call
            	public Directions theDirections;//directions popup
            	public Vector instr = new Vector();// vector to hold instruction
                public Simulator sim; // simulator object call
                private boolean stepped, instrAdded;// if stepped is clicked or instruction added
                public int numCycles = 0;	//number of current cycles
                public Button qButton = new Button("Quit");// quit button to close application 
            	
            	
            	GridBagLayout gridbag = new GridBagLayout();
        		GridBagConstraints constraints = new GridBagConstraints();
        		//builds gridbag
        	  	void buildConstraints(GridBagConstraints gbc, int gx, int gy,
        	  		int gw, int gh, int wx, int wy)
        	  	{
        	  		gbc.gridx = gx;
        	  		gbc.gridy = gy;
        	  		gbc.gridwidth = gw;
        	  		gbc.gridheight = gh;
        	  		gbc.weightx = wx;
        	  		gbc.weighty = wy;
        	  	}
        	  	
        	  	//initializes GUI
        	  	public void init()
        	  	{
        	  		sim = new Simulator(instr, lb, stg);
        	  		
        	  		GridBagLayout gridbag = new GridBagLayout();
        	  		GridBagConstraints constraints = new GridBagConstraints();
        	  		setLayout(gridbag);
        	  		
        	  		// ListBoxes object
        	  		buildConstraints(constraints, 0, 1, 1, 1, 100, 50);
        	  		constraints.fill = GridBagConstraints.BOTH;
        	  		constraints.anchor = GridBagConstraints.CENTER;
        	  		gridbag.setConstraints(lb, constraints);
        	  		lb.Instructions.addActionListener(this);
        	  		lb.tfai.addActionListener(this);
        	  		lb.addinst.addActionListener(this);
        	  		lb.delete.addActionListener(this);
        	  		lb.directions.addActionListener(this);
        	  		add(lb);
        	  		
        	  		// Stages object
        	  		buildConstraints(constraints, 0, 2, 1, 1, 100, 30);
        	  		constraints.fill = GridBagConstraints.BOTH;
        	  		constraints.anchor = GridBagConstraints.CENTER;
        	  		gridbag.setConstraints(stg, constraints);
        	  		add(stg);
    
        	  		// UI object
        	  		buildConstraints(constraints, 0, 0, 1, 1, 100, 20);
        	  		constraints.fill = GridBagConstraints.BOTH;
        	  		constraints.anchor = GridBagConstraints.CENTER;
        	  		gridbag.setConstraints(usr, constraints);
        	  		usr.step.addActionListener(this);
        	  		usr.reset.addActionListener(this);
        	  		add(usr);
        	  		
        	  		usr.step.setEnabled(false);
        	  		
        	  		validate();
        	  		
        	  		stepped = false;
        	  		instrAdded = false;
        	  		
        	  		//theDirections = new Directions(this);
        	  	}
        	  	
    
        	  	//action events
        	  	public void actionPerformed(ActionEvent e)
        	  	{
        	  		String temp;
        	  		int tempnum, j = 0;
        	  		Instruction tempinstr;
        	  		boolean OK, done;
        	  		
        	  		Object src = e.getSource();
        	  		
        	  		if ((src == lb.addinst) || (src == lb.tfai))
        	  		{
        	  			temp = lb.tfai.getText();
        	  			temp = temp.toUpperCase();
        	  			tempinstr = new Instruction(temp);
        	  			if (tempinstr.valid() == true)
        	  			{
        	  				if(!instrAdded)
        	  				{
        	  					usr.step.setEnabled(true);
        	  					instrAdded = true;
        	  				}
        	  				instr.addElement(tempinstr);
        	  				tempnum = lb.Instructions.getItemCount();
        	  				temp = tempnum + ": " + temp;
        	  				lb.Instructions.add(temp);
        	  				lb.tfai.selectAll();
        	  				lb.tfai.requestFocus();		
        	  			}
        	  			else
        	  				lb.Messages.add(tempinstr.theError(), 0);
    
        	  		} else if (src == usr.reset)
        	  		{
        	  			lb.Messages.removeAll();
        	              lb.initRF();
        	              lb.initMemory();
        	              usr.nCycles.setText("0");
        	              usr.nInstr.setText("0");
        	              numCycles = 0;
        	              stg.clearAll();
        	              stepped = false;
        	  			setEnabledUI(true);
        	  			if (src == usr.reset)
        	  			{
        	              	lb.Messages.add("Program has been reset.", 0);
        	              	if (instrAdded)
        	              	{
        	  					usr.step.setEnabled(true);
        	  				}
        	  			}
        	              else
        	              {
        	                  lb.Messages.add("Program has been cleared.", 0);
        	                  if (!instr.isEmpty())
        	                  {
        	                      instr.removeAllElements();
        	                      lb.Instructions.removeAll();
        	                  }
        	                  instrAdded = false;
        	  				usr.step.setEnabled(false);
        	              }
        	  			sim = new Simulator(instr, lb, stg);
        	  		} else if (src == usr.step)
        	  		{
        	  			usr.step.setEnabled(false);
        	  			usr.reset.setEnabled(false);
        	  			lb.directions.setEnabled(false);
        	  			
        	  			done = false;
        	  			
        	  			if (!stepped)
        	  			{
        	  				setEnabledUI(false);
        	  				stepped = true;
        	  			}
        	  			
        	  			if (src == usr.step)
        	  				done = sim.step();
        	  			else
        	  			{
        	  				OK = true;
        	  RUNLOOP:		if(OK)
        	  				{
        	  					if(j>0)
        	  					{
        	  						if (!stepped)
        	  						{
        	  							setEnabledUI(false);
        	  							stepped = true;
        	  						}
        	  						for(tempnum=0;tempnum<j;tempnum++)
        	  						{
        	  							done = sim.step();
        	  							numCycles++;
        	  							if(done)
        	  								break RUNLOOP;
        	  						}
        	  						temp = "Finished running "+j+" steps.";
        	  						lb.Messages.add(temp, 0);
        	  					}
        	  					else
        	  					{
        	  						lb.Messages.add("The run length is invalid.", 0);
        	  					}
        	  				}
        	  			}
        	  			
        	  			numCycles++;
        	  			usr.nCycles.setText(String.valueOf(numCycles));
        	  			j = sim.myWriteBack.getNumberExecutedInstructions();
        	  			usr.nInstr.setText(String.valueOf(j));
        	  			if(done)
        	  			{
        	  				usr.step.setEnabled(false);
        	  			}
        	  			else
        	  			{
        	  				usr.step.setEnabled(true);
        	  			}
        	  			usr.reset.setEnabled(true);
        	  			lb.directions.setEnabled(true);
        	  			
        	  		} else if (src == lb.delete)
        	  		{
        	  			tempnum = lb.Instructions.getSelectedIndex();
        	  			if(tempnum != -1)
        	  			{
        	  				instr.removeElementAt(tempnum);
        	  				lb.Instructions.removeAll();
        	  				for(tempnum=0;tempnum<instr.size();tempnum++)
        	  				{
        	  					tempinstr = (Instruction) instr.elementAt(tempnum);
        	  					temp = tempnum + ": " + 
        	  					    tempinstr.toString();
        	  					lb.Instructions.add(temp);
        	  				}
        	  			}
        	  			else
        	  				lb.Messages.add("An instruction must be selected",0);
    
        	  		} else if (src == lb.directions)
        	  		{
        	  			theDirections.show();
        	  			theDirections.toFront();
        	  		} else if (src == qButton)
        	  		{
        	  			System.exit(0);
        	  		}
    
        	  	}
        	  	
        	  	void setEnabledUI(boolean cond)
        	  	{
        	  		lb.addinst.setEnabled(cond);
        	  		lb.tfai.setEnabled(cond);
        	  		lb.delete.setEnabled(cond);
        	  		lb.branch.setEnabled(cond);
        	  		lb.forward.setEnabled(cond);
        	  	}
            };
            DataPanel = new JPanel()
            {
            	public void paintComponent(Graphics g)
            	{
            		g.drawImage(icon.getImage(), 0, 0, null);
            		super.setOpaque(false);
            		super.paintComponent(g);
            	}
            };
            DataPanel.setOpaque(false);
            DataPanel.setPreferredSize(new Dimension(1280, 550));
            
         
            //Create a JPanel, and add the ConversionPanels to it.
            mainPane = new JPanel();
            mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
            mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
            mainPane.add(MenuPanel);
            mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
            mainPane.add(DataPanel);
            mainPane.add(Box.createGlue());
            mainPane.setPreferredSize(new Dimension(1280, 800));
        }
     
     
    
    
    
    	/**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Set the look and feel.
     
            //Create and set up the window.
            JFrame frame = new JFrame("MIPS SIMULATOR");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            GUI panel = new GUI();
            panel.mainPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(panel.mainPane);
            frame.setSize(1280, 800);
            frame.setLocationRelativeTo(null);
            //Display the window.
            frame.pack();
            frame.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. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Implement actionlistener with panel

    The type GUI must implement the inherited abstract method
    ActionListener.actionPerformed(ActionEvent)
    Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    At various other lines:
    Multiple markers at this line
    - The static field ListBoxes.Instructions should be accessed in a static way
    - The method addActionListener(ActionListener) in the type List is not
    applicable for the arguments (new JPanel(){})

    the actions are defined in other classes.
    Various other lines? Your compiler doesn't tell you which lines? Or do you expect volunteers to spend extra time figuring out which lines?

    how do i resolve these errors?
    Make a fresh start, and compile often -- every time you add a few lines of code. Don't copy and paste; even if you make use of a sample as reference, type every line of code yourself.

    And take some some time out to learn the basics, such as implementing an interface, before you get back to GUI programming. You've gone in too deep too fast.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Implement actionlistener with panel

    the error is at lines 60-64, 79, and 80. i didnt feel it was necessary to say those lines because they all deal with the primary issue: ActionListener not being accessible through nested classes. I've made a fresh start and did not copy and paste. I wrote the first half of the code with basic user interface (buttons, textboxes, list boxes, and labels) as a standalone app a few weeks ago. I got tasked 2 days ago to build a visual datapath of the simulator to track the steps of the instructions going through the "processor", which is due on monday. i know im going into JAVA and interfaces fast, but i have no choice. im working with what i was dealt and learning as i go. since the data path is dependent upon what is input into the code i already had, conceptually i figured the easiest method to implement this would be to put my existing code into a panel, then build the datapath portion in another panel below it. and now here i am with the actionlistener issue. so my original question isnt necesarily "fix-my-code", its "how do i get actionlistener to work with classes nested in a panel?"


    SN: thank you for the link. it is going to be useful for what i need to do after i fix this issue.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Implement actionlistener with panel

    It's a rather fundamental issue: when you implement an interface in a concrete class, you have to provide concrete implementations for any and all methods that the interface defines.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Implement actionlistener with panel

    what you said made sense. thanks for the guidance. so what i did was i put my pre-existing code with the action listener into its own separate class. then for the GUI, i called the class and added it as a component of of a panel. as seen below:

    Java Code:
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.*;
    import javax.swing.*;
    
    public class GUI
    {
        public MPanel Menu = new MPanel();//MPanel interface call
        public DPanel DP = new DPanel();
    	JPanel MenuPanel;
    	JPanel DataPanel;
    	JPanel mainPane;
    	
    	ImageIcon backgroundimage=new ImageIcon("images/background5.jpg");
    	 
    	public GUI() 
        {
    		GridBagLayout gridbag = new GridBagLayout();
    		GridBagConstraints constraints = new GridBagConstraints();
            MenuPanel = new JPanel();
            MenuPanel.setOpaque(false);
            MenuPanel.add(Menu);
            Menu.setMinimumSize(new Dimension(500, 600));
            Menu.setPreferredSize(new Dimension(500, 600));
            Menu.setMaximumSize(new Dimension(500, 600));
            DataPanel = new JPanel()
           {
              	  protected void paintComponent(Graphics g)
              	  {
              		 g.drawImage(backgroundimage.getImage(), 0,0, null);
              		  super.paintComponent(g);
              	   }
           };
            DataPanel.setOpaque(false);
            DataPanel.setVisible(true);
            DataPanel.setPreferredSize(new Dimension(1080, 600));
            
         //create the main panel and add the other panels as components
            mainPane = new JPanel();
            mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.LINE_AXIS));
            mainPane.setBorder(BorderFactory.createEmptyBorder(0,15,15,15));
            mainPane.add(Box.createRigidArea(new Dimension(0, 0)));
            mainPane.add(MenuPanel);
            mainPane.add(Box.createRigidArea(new Dimension(1, 0)));
            mainPane.add(DataPanel);
            mainPane.add(Box.createGlue());
            mainPane.setPreferredSize(new Dimension(1620, 610));
        }
     
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("MIPS SIMULATOR");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            GUI panel = new GUI();
            panel.mainPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(panel.mainPane);
            frame.setSize(1580, 610);
            frame.setLocationRelativeTo(null);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    i hope this helps someone
    DarrylBurke likes this.

Similar Threads

  1. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 07:05 PM
  2. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 09:29 PM
  3. Best way to implement an ActionListener.
    By VisionIncision in forum New To Java
    Replies: 4
    Last Post: 12-07-2010, 01:52 AM
  4. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 09:43 PM
  5. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •