Error with ActionListener

0
  import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;

    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class SwingFileDialog  extends JFrame {
        private JButton bDialog; //botão p/ acionar o diálogo
        private JLabel bDialogo;  //botão p/ resultados
        private JFileChooser dialogo;  //diálogo de arquivos

        public SwingFileDialog() {
            super("SwingFileDialog"); //ajusta titulo
            Container cp = getContentPane(); //painel de conteudo
            cp.setLayout(new GridLayout(2, 1)); //layout grade 4 x1
            JLabel lResultado;
            cp.add(lResultado = new JLabel("Sem seleção"));
            lResultado.setBorder(
                    BorderFactory.createMatteBorder(2, 2, 3, 2, Color.GREEN));
            bDialogo.addActionListener(new ActionListener() { //AQUI ESTA DANDO ERROR

                public void actionPerformed(ActionEvent e) {
                   dialogo = new JFileChooser();
                   int res = dialogo.showOpenDialog(SwingFileDialog.this);
                   if(res==JFileChooser.APPROVE_OPTION) {
                       File  arq = dialogo.getSelectedFile();
                       lResultado.setText(arq.getName());
                           }    }   
                } );
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            }
        public static void main(String[] args) {
            new SwingFileDialog().setVisible(true);

        }
}

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method addActionListener(new ActionListener(){}) is undefined for the type JLabel

at SwingFileDialog.<init>(SwingFileDialog.java:29)
at SwingFileDialog.main(SwingFileDialog.java:43)
    
asked by anonymous 03.12.2014 / 20:40

1 answer

1

Your bDialogo variable is not a button, it's a label ("label"). Labels only serve to show information, there is no action associated with them (like clicking action). You are calling the addActionListener method of a JLabel - that does not have this method - hence the compilation error.

The fact that you're having a run error - rather than a build error - is probably due to the IDE (Eclipse?) responsible for compiling this class: that response in SOen , for example, it is indicated that Eclipse compiles code and enters this exception despite of compile error (me seems like a pretty stupid thing to do, but they must have their motives ...). If you are using Eclipse, try to "clean" or "refresh" the code before compiling it (I have not used this IDE for a long time, I can not remember how it works).

To correct the error, of course, just change the type of bDialogo from JLabel to JButton .

    
03.12.2014 / 22:55