Eventos de um botão

Bom, na faculdade, meu professor fala que para colocar um evento no JButton utiliza-se o evento MouseClick. Reparei que neste evento o tecla Enter não funciona, mesmo quando selecionado nele e se o botão estiver desabilitado (setEnabled(false)) ele pega mesmo assim. Por esses motivos eu optei em usar o ActionPerformed que resolve esses problemas…

Mas fiquei curioso, pq ele recomenda usar o MouseClicked?

Oi anderson_lp789,

É melhor perguntar a ele… :wink:

+1

O correto é usar actionPerformed. Mesmo se você quiser saber se foi pressionada a tecla Ctrl ou Shift enquanto você clicou no botão você pode continuar a usar actionPerformed. Exemplo:

package guj;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class TesteRadioButtons extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel pnlButtons = null;
	private JRadioButton rdoFirst = null;
	private JRadioButton rdoSecond = null;
	private JRadioButton rdoThird = null;
	private ButtonGroup btgMain = null;
	private JButton btnOK = null;

	private JPanel getPnlButtons() {
		if (pnlButtons == null) {
			pnlButtons = new JPanel();
			pnlButtons.setLayout(new BoxLayout(getPnlButtons(),
					BoxLayout.Y_AXIS));
			pnlButtons.setBorder(BorderFactory
					.createTitledBorder(null, "Radio Buttons",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.ABOVE_TOP, new Font("sansserif",
									Font.BOLD, 12), new Color(59, 59, 59)));
			pnlButtons.add(getRdoFirst(), null);
			pnlButtons.add(getRdoSecond(), null);
			pnlButtons.add(getRdoThird(), null);
			pnlButtons.add(getBtnOK(), null);
			getBtgMain();
		}
		return pnlButtons;
	}

	private ButtonGroup getBtgMain() {
		if (btgMain == null) {
			btgMain = new ButtonGroup();
			btgMain = new ButtonGroup();
			btgMain.add(getRdoFirst());
			btgMain.add(getRdoSecond());
			btgMain.add(getRdoThird());
			btgMain.setSelected(getRdoFirst().getModel(), true);
		}
		return btgMain;
	}

	private JRadioButton getRdoFirst() {
		if (rdoFirst == null) {
			rdoFirst = new JRadioButton();
			rdoFirst.setText("First");
		}
		return rdoFirst;
	}

	private JRadioButton getRdoSecond() {
		if (rdoSecond == null) {
			rdoSecond = new JRadioButton();
			rdoSecond.setText("Second");
		}
		return rdoSecond;
	}

	private JRadioButton getRdoThird() {
		if (rdoThird == null) {
			rdoThird = new JRadioButton();
			rdoThird.setText("Third");
		}
		return rdoThird;
	}

	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setText("Check");
			btnOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					StringBuilder sb = new StringBuilder();
					int modifiers = e.getModifiers();
					if ((modifiers & ActionEvent.SHIFT_MASK) != 0)
						sb.append("Shift, ");
					if ((modifiers & ActionEvent.CTRL_MASK) != 0)
						sb.append("Ctrl, ");
					if ((modifiers & ActionEvent.ALT_MASK) != 0)
						sb.append("Alt, ");
					if ((modifiers & ActionEvent.META_MASK) != 0)
						sb.append("Meta, ");
					JOptionPane.showMessageDialog(TesteRadioButtons.this, sb
							.toString());
				}
			});
		}
		return btnOK;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TesteRadioButtons thisClass = new TesteRadioButtons();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	public TesteRadioButtons() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPnlButtons(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
}

Olá entanglement , não entendi sua colocação a respeito de teclas pressionadas ao mesmo tempo do click.

é se quando ele clicou no botão ele está pressionando alguma tecla junto? eh isso?

Haveria problemas de se utilizar o actionPerfomed?

[quote=anderson_lp789]Olá entanglement , não entendi sua colocação a respeito de teclas pressionadas ao mesmo tempo do click.

é se quando ele clicou no botão ele está pressionando alguma tecla junto? eh isso?

Haveria problemas de se utilizar o actionPerfomed?[/quote]
Exatamente isso que ele demonstrou: não há problema algum em usar actionPerformed mesmo que você precise, por exemplo, saber se o usuário pressionou alguma tecla modificador (como Ctrl, Shift ou Alt) ao clicar no botão.

[quote=marcobiscaro2112][quote=anderson_lp789]Olá entanglement , não entendi sua colocação a respeito de teclas pressionadas ao mesmo tempo do click.

é se quando ele clicou no botão ele está pressionando alguma tecla junto? eh isso?

Haveria problemas de se utilizar o actionPerfomed?[/quote]
Exatamente isso que ele demonstrou: não há problema algum em usar actionPerformed mesmo que você precise, por exemplo, saber se o usuário pressionou alguma tecla modificador (como Ctrl, Shift ou Alt) ao clicar no botão.[/quote]

ah entendi… obrigado pelos esclarecimentos