Question about ArrayList and Jbuttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JinFTW
    New Member
    • Feb 2008
    • 12

    Question about ArrayList and Jbuttons

    Hey guys I hate to bother you with this, but I'm having a problem with creating an arrayList of JButtons that will end up using an actionlistener. My original program was this:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    
    public class KeyBorderPanel extends JPanel
    {
    	private JLabel resultLabel;
    	private JButton one, two, three, four, five, six, seven, eight, nine, pound, zero, star, clear;
    	private JPanel keyPanel, resultPanel, clearPanel;
    	private String result;
    	
    	public KeyBorderPanel()
    	{
    		JFrame frame = new JFrame ("KeyPad");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		panel.setLayout (new BorderLayout());
    		panel.setBackground (Color.green);	
    		
    	
    				
    		
    
    		
    		JPanel resultPanel = new JPanel();
    		resultPanel.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
    		resultLabel = new JLabel (result);
    		KeyListener listener = new KeyListener();
    		resultPanel.add (resultLabel);
    		panel.add (resultPanel);
    		
    		
    		JPanel keyPanel = new JPanel();
    		keyPanel.setLayout (new GridLayout (4, 3));
    		keyPanel.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
    		one = new JButton ("1");
    		two = new JButton ("2");
    		three = new JButton ("3");
    		four = new JButton ("4");
    		five = new JButton ("5");
    		six = new JButton ("6");
    		seven = new JButton ("7");
    		eight = new JButton ("8");
    		nine = new JButton ("9");
    		pound = new JButton ("#");
    		zero = new JButton ("0");
    		star = new JButton ("*");  
    		
    		
    		keyPanel.add (one);
    		keyPanel.add (two);
    		keyPanel.add (three);
    		keyPanel.add (four);
    		keyPanel.add (five);
    		keyPanel.add (six);
    		keyPanel.add (seven);
    		keyPanel.add (eight);
    		keyPanel.add (nine);
    		keyPanel.add (pound);
    		keyPanel.add (zero);
    		keyPanel.add (star);	
    		panel.add (keyPanel);
    		
    		JPanel clearPanel = new JPanel();
    		clearPanel.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
    		clear = new JButton("Clear");
    		clear.addActionListener (new ButtonListener());
    		clear.addActionListener (listener);
    		clearPanel.add (clear);
    		panel.add (clearPanel);
    
    			
    	one.addActionListener (listener);
    	two.addActionListener (listener);
    	three.addActionListener (listener);
    	four.addActionListener (listener);
    	five.addActionListener (listener);
    	six.addActionListener (listener);
    	seven.addActionListener (listener);
    	eight.addActionListener (listener);
    	nine.addActionListener (listener);
    	pound.addActionListener (listener);
    	zero.addActionListener (listener);
    	star.addActionListener (listener);
    	
    	panel.add (resultPanel, BorderLayout.NORTH);
    	panel.add (keyPanel, BorderLayout.CENTER);
    	panel.add (clearPanel, BorderLayout.EAST);
    	
    	frame.getContentPane().add(panel);
    	frame.pack();
    	frame.setVisible(true);
    	
    	}	
    	private class KeyListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			if (event.getSource() == one)
    				resultLabel.setText("1");
    			if (event.getSource() == two)
    				resultLabel.setText("2");
    			if (event.getSource() == three)
    				resultLabel.setText("3");
    			if (event.getSource() == four)
    				resultLabel.setText("4");
    			if (event.getSource() == five)
    				resultLabel.setText("5");
    			if (event.getSource() == six)
    				resultLabel.setText("6");
    			if (event.getSource() == seven)
    				resultLabel.setText("7");
    			if (event.getSource() == eight)
    				resultLabel.setText("8");
    			if (event.getSource() == nine)
    				resultLabel.setText("9");
    			if (event.getSource() == pound)
    				resultLabel.setText("#");
    			if (event.getSource() == zero)
    				resultLabel.setText("0");
    			if (event.getSource() == star)
    				resultLabel.setText("*");
    		}	
    	}
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			if (event.getSource() == clear)
    				resultLabel.setText("		");	
    		}	
    	}		
     }
    I want to simplify the keypad buttons using an ArrayList. My original inclination was to do it in a manner such as:

    Code:
    import java.util.ArrayList;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    
    public class KeyBorderPanel2 extends JPanel
    {
    	private JLabel resultLabel;
    	private JPanel keyPanel, resultPanel, clearPanel;
    	private JButton clear;
    	private String result;
    	private ArrayList<JButton> keypadList;
    	private String[] buttons = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "pound", "star"};
    	
    	
    	public KeyBorderPanel2()
    	{
    		
    		KeyListener listener = new KeyListener();
    		keypadList = new ArrayList<JButton>();
    		
    		for (int i = 0; i < buttons.length; i++)
    		{
    			keypadList.add (new JButton(buttons[i]));
    			keypadList.add(ActionListener(listener));
    		}
    //......
    But for some reason or another, the ActionListener will not work with my program this way. So I was curious to see if you guys could offer me any advice on this, I'd greatly appreciate it.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What's your ActionListener code looking like now?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by JinFTW
      [code=java]
      //
      private ArrayList<JButt on> keypadList;
      private String[] buttons = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "pound", "star"};


      public KeyBorderPanel2 ()
      {

      KeyListener listener = new KeyListener();
      keypadList = new ArrayList<JButt on>();

      for (int i = 0; i < buttons.length; i++)
      {
      keypadList.add (new JButton(buttons[i]));
      keypadList.add( ActionListener( listener));
      }
      //......
      [/CODE]

      But for some reason or another, the ActionListener will not work with my program this way. So I was curious to see if you guys could offer me any advice on this, I'd greatly appreciate it.
      A couple of remarks:

      - keypadList can only store JButtons
      - an ActionListener must be registered at a JButton
      - a KeyListener is not an ActionListener.

      And a question to you:

      What did you have in mind with that last (incorrect) line of code?

      kind regards,

      Jos

      Comment

      Working...