Link to home
Start Free TrialLog in
Avatar of Vanavah Edwards
Vanavah Edwards

asked on

How to resolve the parameter and methods errors to a separate class for action listener performer class

I have separated my action listener inner class (snippet ID=8238792) from my main to a separate class (snippet ID=8238791) . Now they are a few errors that comes up in the action listener separated class mainly line 7, 30, 31  in class headers.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.*;
import javax.swing.text.DateFormatter;
import javax.swing.JComboBox;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;

import java.io.File;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class MOCA{

	private static JComboBox cb1, cb2;

	public static void main(String[] args) {

 		// START ComboBox Image Setup
	       ImageIcon[] images;
			String[] comboStrings = {"create new po", "browse and select"};
	        images = new ImageIcon[comboStrings.length];	//array size
	        Integer[] intArray = new Integer[comboStrings.length];
	        ComboBoxRenderer renderer= new ComboBoxRenderer();
	        for (int i = 0; i < comboStrings.length; i++) {
	            intArray[i] = new Integer(i);
	            images[i] = renderer.createImageIcon("images/" + comboStrings[i] + ".jpg");  // creates image; send path

	            Image img = images[i].getImage();	// scales image size   
	            Image newimg = img.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
	            images[i] = new ImageIcon(newimg);
	                        
	            if (images[i] != null) {  // sets image description
	                images[i].setDescription(comboStrings[i]);
	            }
	        }
	     // END ComboBox Image Setup

	        cb1 = new JComboBox(images);
	        cb1.setRenderer(renderer);  // displays image description
	        cb1.setMaximumRowCount(2);

	        String[] xxx = {"1111", "2222", "3333"};
	        cb2 = new JComboBox(xxx);

	        ActionListenerPerformer alp = new ActionListenerPerformer();
	        cb1.addActionListener(alp);
	        cb2.addActionListener(alp);
	        
	        JFrame f = new JFrame("teerer");
	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        JPanel p = new JPanel();
	        p.add(cb1);
	        p.add(cb2);
	        f.add(p);
	        f.setSize(300, 300);
	        f.setVisible(true);
	        
	        
//        buildPanel sohscr = new buildPanel(); // make an object of the class
//        sohscr.buildPanel(); // call the method in the class
	}

}

Open in new window

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;


public class ActionListenerPerformer implements ActionListener {

	private static JComboBox cb1, cb2;

	public ActionListenerPerformer(ActionEvent e) {
		if (e.getSource().equals(cb1)) {
	          System.out.println("You've seleccted - "+cb1.getSelectedItem());
		}
	  	if (e.getSource().equals(cb2)) {
	          System.out.println("You've seleccted - "+cb2.getSelectedItem());
	  	}
	  	if(cb1.getSelectedItem().equals("create new po")) {
	  		System.out.println("or right cb1 = "+cb1.getSelectedItem());
	  	}
		if(cb1.getSelectedItem().equals("browse and select")) {
			System.out.println("or right cb1 = "+cb1.getSelectedItem());
		}
		if(cb2.getSelectedItem().equals("1111")) {
		  	System.out.println("or right cb2 = "+cb2.getSelectedItem());
	  	}
	}
	public static void main(String[] args) {
			
		ActionListenerPerformer ap = new ActionListenerPerformer(); 
		ap.ActionListenerPerformer();
				
	}
}

Open in new window

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;


public class ComboBoxRenderer extends JLabel implements ListCellRenderer {
        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(LEFT);
            setVerticalAlignment(CENTER);
        }

        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus)
        {
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            ImageIcon icon = (ImageIcon)value;
            setText(icon.getDescription());
            setIcon(icon);
//	        setPreferredSize(new Dimension(100, 100)); // for a explicit size
	        setPreferredSize(null); // for a pack size

            return this;
        }

    	//** RECEIVES THE PATH; CREATES THE IMAGEICON, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
        	java.net.URL imgURL = ImageComboBox.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                    return null;
            }
        }
        
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

If you declare implements ActionListener you must impleent
public void actipnPerformed(ActionEvent)  
in  this class

Of course it is a mistake - you don't have sucvh method

SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You usually need only one metho main() in the whole project - what is the sense for
having main() method in ActionListenere
I think all these experiemnt for shuffling things back and forth are not very prioductive

Much better to have one resonable template, understand it fully,
 and follow it - these shufflins around mostly lead you to confusion

You don't want to split actionListener separatley - after I changed constructor to
public ActionListener()

to a method

public void actionPerformed(ActionEvent)
it now compiles.

But it will not execute correctly becaues these cb1 and cb2
have nothing to do with cb1 and cb2 in your main class.

This splittiing mostly add additional work and requires passing parameters
and  varaibnels correctly between methods - you don't want to do it.





ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vanavah Edwards
Vanavah Edwards

ASKER

I thought it would be good structured programming to separate the action performers as it is a seperate method because I have a form with several fields for testing and it would be alot of code in the main making it difficult to read.  So if you have different module handing different functions then your can engineer different modules separate - the modularity approach.  Is there a way to link and resolve this problem with the instance and class variables in my main module and the performance listener in the separate class?
Can I pass a set of parameter to a class as you do with other visual language like
=alp(cb1,cb2);
This command = calls the method and passes 2 parameters with or without a return depending upon the requirements.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code works perfect as a separate class but without reference to class variables from the main. It also works in my main without problems.  However, I agree with you and will stick to java syntactics instead of trying to implement the same approach from a another language.  I will close of this session now.
Yes, I think this is the best way of learning - you take some working code form someone and then
pose yourself some taks to do some improvbement/addition - then without major overhaul you accomplish it - in the process
you'll have to undesratdn how this code works. then do another improveemnt,and so on and so on.
Eventually you'll undersatnd everything what they are doing, and if
at some point you feel that you really require quite different functionalty - tghen you'll be able to implement
your own quite different solution which will be useful for your specifci case.
But such things frankly happen rarely - nortmally commone templates happen to be the best.
But to undersatnd those is defineitely good frst step.