Results 1 to 5 of 5
  1. #1
    feoleb is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Code simplification for an evelator panel

    I'm having trouble getting the for loop to work, to save me some lines of code. I added the for loop, but I didn't subtract any code it's supposed to replace, because when I do, the program won't run. It's the for loop in 'private void initialize().' The program functions as intended. Just trying to get the code shorter and more organized.

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import java.util.Arrays;
    
    public class ElevatorPanel extends JFrame implements ActionListener
    {
    	public ElevatorPanel()
    	{
    		initialize();
    	}
    
    	public static void main(String[] args)
    	{
    		ElevatorPanel thisFrame = new ElevatorPanel();
    		thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		thisFrame.setSize(200,600);
    		thisFrame.setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    	// TODO Auto-generated method stub
    	}
    	
    	//declare JButton type buttons
    
        private JButton jone;
    	private JButton jtwo;
    	private JButton jthree;
    	private JButton jfour;
    	private JButton jfive;
    	private JButton jsix;
    	private JButton jseven;
    	private JButton jeight;
    	private JButton jnine;
    	private JButton jten;
    	private JButton jtest;
    	private JButton jclear;
    	
    	private JButton button_array [] = {jone, jtwo, jthree, jfour, jfive, jsix, jseven, jeight,
    			jnine, jten, jtest, jclear};
    
    	
    	
    	private void initialize()
    	{
    	
    		
    		button_array = new JButton[11]; 
    
    		String buttonCaption = null;
    
    		for (int i = 0; i < button_array.length; ++i) 
    		{
    		    buttonCaption = (i < 11) ? Integer.toString(i + 1) : "";
    		    button_array[i] = new JButton(buttonCaption);
    		    button_array[i].addActionListener(this);
    		    add(button_array[i]);
    		}
    
    		button_array[button_array.length - 2].setText("Test");
    		button_array[button_array.length - 1].setText("Clear");
    
    		//initialize buttons
    		jone = new JButton("1");
    		jone.addActionListener(this);
    		jone.setBackground(Color.gray);
    		
    		jtwo = new JButton("2");
    		jtwo.addActionListener(this);
    		jtwo.setBackground(Color.gray);
    		
    		jthree = new JButton("3");
    		jthree.addActionListener(this);
    		jthree.setBackground(Color.gray);
    		
    		jfour = new JButton("4");
    		jfour.addActionListener(this);
    		jfour.setBackground(Color.gray);
    		
    		jfive = new JButton("5");
    		jfive.addActionListener(this);
    		jfive.setBackground(Color.gray);
    		
    		jsix = new JButton("6");
    		jsix.addActionListener(this);
    		jsix.setBackground(Color.gray);
    		
    		jseven = new JButton("7");
    		jseven.addActionListener(this);
    		jseven.setBackground(Color.gray);
    		
    		jeight = new JButton("8");
    		jeight.addActionListener(this);
    		jeight.setBackground(Color.gray);
    		
    		jnine = new JButton("9");
    		jnine.addActionListener(this);
    		jnine.setBackground(Color.gray);
    		
    		jten = new JButton("10");
    		jten.addActionListener(this);
    		jten.setBackground(Color.gray);
    		
    		jclear = new JButton("clear");
    		jclear.addActionListener(this);
    		jclear.setBackground(Color.gray);
    		
    		jtest = new JButton("test");
    		jtest.addActionListener(this);
    		jtest.setBackground(Color.gray);
    		
    		//Add buttons to panel
    		JPanel panel = new JPanel(new GridLayout(6,2));
    	    panel.add(jnine);
    	    panel.add(jten);
    	    panel.add(jseven);
    	    panel.add(jeight);
    	    panel.add(jfive);
    	    panel.add(jsix);
    	    panel.add(jthree);
    	    panel.add(jfour);
    	    panel.add(jone);
    	    panel.add(jtwo);
    	    panel.add(jtest);
    	    panel.add(jclear);
    		        
    	    //Add listeners for each button
    	    oneListenerClass listener1 = new oneListenerClass();
    	    jone.addActionListener(listener1);
    	    
    	    twoListenerClass listener2 = new twoListenerClass();
    	    jtwo.addActionListener(listener2);
    	    
    	    threeListenerClass listener3 = new threeListenerClass();
    	    jthree.addActionListener(listener3);
    	    
    	    fourListenerClass listener4 = new fourListenerClass();
    	    jfour.addActionListener(listener4);
    	    
    	    fiveListenerClass listener5 = new fiveListenerClass();
    	    jfive.addActionListener(listener5);
    	    
    	    sixListenerClass listener6 = new sixListenerClass();
    	    jsix.addActionListener(listener6);
    	    
    	    sevenListenerClass listener7 = new sevenListenerClass();
    	    jseven.addActionListener(listener7);
    	    
    	    eightListenerClass listener8 = new eightListenerClass();
    	    jeight.addActionListener(listener8);
    	    
    	    nineListenerClass listener9 = new nineListenerClass();
    	    jnine.addActionListener(listener9);
    	    
    	    tenListenerClass listener10 = new tenListenerClass();
    	    jten.addActionListener(listener10);
    	    
    	    testListenerClass listener11 = new testListenerClass();
    	    jtest.addActionListener(listener11);
    	    
    	    clearListenerClass listener12 = new clearListenerClass();
    	    jclear.addActionListener(listener12);
    	    
    	    //ads the panel to the window
    	    add(panel);
    	}
    	
    	class oneListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jone.setBackground(Color.yellow);
    		}
    	}
    	
    	class twoListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jtwo.setBackground(Color.yellow);
    		}
    	}
    	
    	class threeListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jthree.setBackground(Color.yellow);
    		}
    	}
    	
    	class fourListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jfour.setBackground(Color.yellow);
    		}
    	}
    	
    	class fiveListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jfive.setBackground(Color.yellow);
    		}
    	}
    	
    	class sixListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jsix.setBackground(Color.yellow);
    		}
    		
    	}
    	
    	class sevenListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jseven.setBackground(Color.yellow);
    		}
    	}
    	
    	class eightListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jeight.setBackground(Color.yellow);
    		}	
    	}
    	
    	class nineListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jnine.setBackground(Color.yellow);
    		}
    	}
    	
    	class tenListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			jten.setBackground(Color.yellow);
    		}
    	}
    	
    	class testListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    		jone.setBackground(Color.yellow);
    		jtwo.setBackground(Color.yellow);	
    		jthree.setBackground(Color.yellow);
    		jfour.setBackground(Color.yellow);
    		jfive.setBackground(Color.yellow);
    		jsix.setBackground(Color.yellow);
    		jseven.setBackground(Color.yellow);
    		jeight.setBackground(Color.yellow);
    		jnine.setBackground(Color.yellow);
    		jten.setBackground(Color.yellow);
    		}
    	}
    	
    	class clearListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    		jone.setBackground(Color.gray);
    		jtwo.setBackground(Color.gray);	
    		jthree.setBackground(Color.gray);
    		jfour.setBackground(Color.gray);
    		jfive.setBackground(Color.gray);
    		jsix.setBackground(Color.gray);
    		jseven.setBackground(Color.gray);
    		jeight.setBackground(Color.gray);
    		jnine.setBackground(Color.gray);
    		jten.setBackground(Color.gray);
    		}
    	}
    }
    Last edited by feoleb; 03-18-2013 at 08:27 PM.

  2. #2
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Code simplification for an evelator panel

    If you want to be 'more organized' then you should consider using an array for those JButtons.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Code simplification for an evelator panel

    You might want to consider subclassing JPanel and JButton. You can then add the actionListeners and set colors in the in the constructor. You can then pass the JButton instance as an argument to your Panel constructor where it can get added.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Code simplification for an evelator panel

    Quote Originally Posted by jim829 View Post
    You might want to consider subclassing JPanel and JButton.
    I'd call that a misuse of inheritance. There's no need to subclass anything here, and least of all to subclass JFrame -- which the posted code does.

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

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Code simplification for an evelator panel

    Yeah. I realize that. How about creating JButton and JPanel factory methods to facilitate the creation of the objects?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  2. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 08:43 PM
  3. Panel in a panel?
    By pahiker in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2010, 11:42 AM
  4. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  5. covert code from a panel to an applet
    By rebelstone in forum Java Applets
    Replies: 5
    Last Post: 07-31-2008, 09:43 PM

Posting Permissions

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