Error with Actionlistener

Asked

Viewed 264 times

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)
  • Provide more information about the error.

  • I don’t know why you’re making a mistake in this code

1 answer

1


Its variable bDialogo it’s not a button, it’s a label ("label"). Labels only serve to display information, there is no action associated with them (such as clicking action). You are calling the method addActionListener of a JLabel - which does not have this method - hence the compilation error.

The fact that you’re having an error of execution - not compilation - probably due to the IDE (Eclipse?) responsible for compiling this class: in that reply in Soen, for example, it is specified that Eclipse compiles the code and introduces this exception despite from the compilation error (it seems like a pretty stupid thing to do, but they must have their reasons...). If you are using Eclipse, try to "clean" or "refresh" the code before compiling it (I haven’t used this IDE for a long time, I can’t remember how it works).

And to correct the mistake, of course, just change the type of bDialogo of JLabel for JButton.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.