Atribuir listener de acordo com o nome do JButton

1 resposta
tecnocrata

olá a todos bem to tentando na seguinte linha , supostamente deveria criar um listener dependendo do nome do botao porem isto nao ocorre oque devo fazer ?

class bPanel extends JPanel {
	 public bPanel ()
	 {		
		 //creates the layout and set its to the panel
			GridBagLayout gbl = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();
			
		setLayout(gbl);
	
		c.gridx=0;
		c.gridy=0;
		makebutton("Abrir Arquivo",gbl,c);
		
		c.gridx=0;
		c.gridy=1;
		c.ipadx=15;
		
		makebutton("Abrir Texto",gbl,c);
	 }
	
	 
	protected void  makebutton(String name,	GridBagLayout gbl,GridBagConstraints c)
	 { 
		 JButton button =new JButton(name);
		
		 
		if(name.equals("Abrir Arquivo"))
		{
				button.addActionListener(new
								ActionListener(){
	
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(); 
					
			int result  = chooser.showOpenDialog(null);	
				if (result==JFileChooser.APPROVE_OPTION);
				{
					String file =chooser.getSelectedFile().getPath();
					//labelPanel.
											
				}
										
			}
			});
				
		}else 	if(name.equals("Abrir Texto"));
		{
				button.addActionListener(new
				ActionListener(){
				public void actionPerformed(ActionEvent e) {
				String Message =JOptionPane.showInputDialog("a message");						
			}
						});
				
		}
		 
		 
		 
		 
		 
		 
		 //set the component constants
		 gbl.setConstraints(button, c);
	
		 add(button);
		 
	 }
	
	
}

agradeço a ajuda ,até mais

1 Resposta

diego2005

Por que vc então não implementa o interface ActionListener e no método actionPerformed você verifica qual botão foi pressionado???

T+

Criado 18 de julho de 2007
Ultima resposta 18 de jul. de 2007
Respostas 1
Participantes 2