Results 1 to 2 of 2
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Question Help needed for a new comer to Java!

    Hello, everyone. I am trying to create a frame with components. It works. I have other request. I need to have an algorithm for adding events or removing them from specific components. I`ve tried HashMap to keep a record of the items but it appeared that actually I was passing the event handler to unasigned to the frame button. Here is my source code:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.LinkedList;
    import java.util.Vector; 
    import java.util.HashMap;
    
    public class Calculator implements ActionListener
    {
    	JFrame frame;
    	static HashMap elements = new HashMap();
    	//OVERRIDES
    	public void actionPerformed(ActionEvent e) {
    			clicked();
    		}
    	public void clicked() {
    		System.out.println("CLICK CLICK");
    	}
    	//Inner classes[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
    	
    /*######################################################################
    #########	i	m	p	o	r	t	a	n	t ##########################
    ######################################################################*/
    	
    	
    	
    	final  static class ComponentFactory 
    
    	{
    		static JComponent jComp;
    		//OVERRIDES
    		
    		//End OVERRIDES	
    		/*note: Always pass a constructable componentws with "new" as args :
    		 * ex JTextField = ComponentFactory.addComponent(new JButton("bla"), new FlowLayout());
    		 * */
    		
    		public static JComponent addComponent(String name, JComponent comp, LayoutManager layout) {
    			String k = name;
    			JComponent v = comp;
    			jComp = comp;
    		
    			elements.put(k, v);
    			return jComp;
    		}
    		
    		
    		public static JComponent componentConstructWithComponents(
    												String name, 
    												JComponent type,
    												LayoutManager style,
    												JComponent[] items) 
    		{
    			
    			JComponent jc = type;
    			jc.setLayout(style);
    			JComponent[] jitems = new JComponent[items.length];
    			for (int i=0; i < items.length; i++) {
    				
    				jc.add(items[i]);
    			}
    			elements.put(name, jc);
    			return jc; //if nesting the constructions
    		}
    		
    	} // end Component Factory
    	
    	//////// PROTO 2 Construction Factory
     // end Component Factory
    	
    /*####################################################################*/
    	
    	final static class FrameFactory {
    		//create and set a frame for usage
    		//fil in later...
    		
    		public static JFrame frameConstruct(String name, LayoutManager style, int w, int h) 
    		{
    			JFrame jf = new JFrame(name);
    			jf.setLayout(style);
    			jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			jf.setSize(w, h); 
    			jf.setVisible(true);
    			return jf;
    		}
    	
    	} // end Frame factory
    	
    	//]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
    	
    	
    	/*Methods and constructors below, must have add and remove methods,,
    	 * aswell as action events handlers.
    	 * Notes:
    	 * 
    	 * 
    	 * 
    	 * 
    	 * 
    	 * 
    	 * 
    	 * =============================================================*/
    	 
    	 
    	Calculator() {
    	
    		frame = FrameFactory.frameConstruct(
    		"MAINFRAME", new FlowLayout(), 300, 300);
    		
    		
    		frame.add(ComponentFactory.componentConstructWithComponents(
    				"ROW1", new JPanel(), new GridLayout(4,4,1,1),
    				new JComponent[] {
    					ComponentFactory.addComponent("Button1",new JButton("1"), null),
    					ComponentFactory.addComponent("Button2",new JButton("2"), null),
    					ComponentFactory.addComponent("Button3",new JButton("3"), null),
    					ComponentFactory.addComponent("Button4",new JButton("4"), null),
    					ComponentFactory.addComponent("Button5",new JButton("5"), null),
    					ComponentFactory.addComponent("Button6",new JButton("6"), null),
    					ComponentFactory.addComponent("Button7",new JButton("7"), null),
    					ComponentFactory.addComponent("Button8",new JButton("8"), null),
    					ComponentFactory.addComponent("Button9",new JButton("9"), null),
    					ComponentFactory.addComponent("Button10",new JButton("0"), null),
    					ComponentFactory.addComponent("Button11",new JButton("00"), null),
    					ComponentFactory.addComponent("Button12",new JButton("."), null),
    					
    				})); // add calculator buttons
    		//add symbols panel
    		JComponent[] jButtons = {
    							ComponentFactory.addComponent("ButtonPlus",new JButton("+"), null),
    							ComponentFactory.addComponent("ButtonMinus",new JButton("-"), null),
    							ComponentFactory.addComponent("ButtonMultiply",new JButton("X"), null),
    							ComponentFactory.addComponent("ButtonDivide",new JButton("/"), null),
    							ComponentFactory.addComponent("ButtonEqual",new JButton("="), null),
    							ComponentFactory.addComponent("ButtonPow",new JButton("pow"), null),
    							ComponentFactory.addComponent("ButtonSqrt",new JButton("sqrt"), null),
    							ComponentFactory.addComponent("ButtonExp",new JButton("exp"), null),
    						};
    		
    		frame.add(ComponentFactory.componentConstructWithComponents(
    				"ROW2", new JPanel(), new FlowLayout(FlowLayout.RIGHT,2,2),
    				new JComponent[] {
    					ComponentFactory.componentConstructWithComponents(
    						"p1", new JPanel(), new GridLayout(4,4,1,1), jButtons) 
    						} 
    					)
    				);
    						
    		JComponent jtext =  ComponentFactory.addComponent("TextField",new JTextField(25),null);			
    		frame.add( ComponentFactory.componentConstructWithComponents( 
    									"ROW3",
    									new JPanel(), 
    									new FlowLayout(FlowLayout.CENTER, 5,5), 
    									new JComponent[] { 
    										jtext
    									})
    			); //end constructing display
    		
    		
    		
    }//end basic constructor
    	
    	public void printAll(String name) {
    		
    		System.out.println(elements.get(name).toString());
    		System.out.println("Adding clickable");
    		if ( elements.containsKey("Button2") ) {
    			
    			System.out.println("Has button1 and element");
    			elements.get("Button2").addActionListener(this);
    		}
    				
    		
    	}
    	
    	public static void main(String[] args) {
    		try {
    			Calculator c= new Calculator();
    			c.printAll("Button1");
    		} catch (Exception e) { e.printStackTrace(); }
    	}
    	
    }
    Compiling blows when I try to access the element from the hash storage, since, it`s my guess: the element does not belong to the JPAnel. However, if you compile without the event interface it works but, it appears that adding event to the element from the hash is not an option. Hash of hashes however didn`t make sense to me too. I can make a key-valye pair to the name in which I want to refer but JPanel already is bundled with buttons, that I need to refer to. The option is that JPanel must return it`s components, but I looked into the API and I did not saw any get objects methods for either JPanel or JComponent, even in Component. Component can find elements by XY but I don`t want that.
    My idea was a custom class hash based that will store my demands, but I just want to know that there is better way.
    Last edited by heatblazer; 11-30-2012 at 10:58 AM. Reason: bad text

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help needed for a new comer to Java!

    Please go through the Forum Rules -- particularly the third paragraph.

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

Similar Threads

  1. Help needed with java
    By ozzy in forum New To Java
    Replies: 11
    Last Post: 11-01-2009, 10:08 PM
  2. java help needed
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 05:33 AM
  3. Java experts needed- 30 minute online Java projects
    By michelle in forum Jobs Offered
    Replies: 0
    Last Post: 03-05-2008, 11:47 PM
  4. New Comer, Java C# question
    By lmei007 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 04:54 PM
  5. Java help needed
    By jeneal in forum New To Java
    Replies: 0
    Last Post: 11-21-2007, 03:04 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
  •