I would catch by a listener the button pressed but not by the text inside it because I've button only with background image.
This code catch it by label inserted in the jbutton constructor but I don't want show this label.
So or I find a way to hide label on button or I don't insert a label and catch button by some other handle.
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
JOptionPane.showMessageDialog(null,"È stato premuto"+b.getActionCommand());
}
}
Use setActionCommand() to avoid a default action command of the button text.
JButton myButton = new JButton();
myButton.setActionCommand("myButtonCommand");
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (actionCommand.equals("myButtonCommand")) {
// do something...
}
}
ImageIcon ic=new ImageIcon("C:/image.png");
JButton btn=new JButton(ic);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JButton b = (JButton)ae.getSource();
JOptionPane.showMessageDialog(null,"È stato premuto"+b.getActionCommand());
}
});
Related
button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
I have 3 buttons here, but they are doing same thing. It takes some space in code. How can I group them all and assigned to one ActionListener?
Something like this. I don't know how it should be.
button1.button2.button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
Just assign the ActionListener to a different variable first:
ActionListener listener = new ActionListener() {
...
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
It's just a reference after all - the only "special" thing here is the use of an anonymous inner class to create an instance of ActionListener.
If there are multiple things that you want to do with all your buttons, you may well want to put them into a collection rather than having three separate variables for them, too.
Juste create a variable :-)
ActionListener listener = new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
ActionListener listener = new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
Implement ActionListener in your class
public class YourClass implements ActionListener
That should make you have to implement an actionPerformed method in just one place where it can handle all your actions.
Your addActionListener lines should then become:
button1.addActionListener(this);
button2.addActionListener(this);
etc.
Then use:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == button1 || event.getSource() == button2 || event.getSource() == button3) {
exite.setEnabled(true);
}
}
You can create a class called event that implements the ActionListener for JButtons. Then use the parameter getCommand() method to get the text of the JButton and create if statements for all of the buttons based on the text that the JButton has, the code would be:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*
public class MultipleJButtons extends JFrame {
JButton button1, button2, button3;
public static void main(String args[]) {
MultipleJButtons gui = new MultipleJButtons();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setTitle("Multiple Buttons");
gui.setVisible(true);
}
public MultipleJButtons() {
event e = new event();
setLayout(new FlowLayout());
button1 = new JButton("Button 1");
add(button1);
button1.addActionListener(e);
button2 = new JButton("Button 2");
add(button2);
button2.addActionListener(e);
button3 = new JButton("Button 3");
add(button3);
button3.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Button 1")) {
System.out.println("You pressed button 1");
} else if (command.equals("Button 2")) {
System.out.println("You pressed button 2");
} else if (command.equals("Button 3")) {
System.out.println("You pressed button 3");
}
}
I have multiple text fields and buttons. When a text field is selected, text should be added to it when a button is pressed but nothing is inserted with bellow code. what i have missed.
thanks for your help.
public class ButtonExample_Extended extends JFrame implements ActionListener {
public JPanel createContentPane (){
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 50);
buttonPanel.setSize(1370, 770);
totalGUI.add(buttonPanel);
B9 = new JButton("9");
B9.setLocation(1190, 570);
B9.setSize(50, 50);
B9.addActionListener(this);
buttonPanel.add(B9);
JPasswordField passwordField = new JPasswordField(20);
passwordField.setLocation(900,565);
passwordField.setSize(120,30);
buttonPanel.add(passwordField);
}
private JTextComponent selectedTextField;
// TextFields onFocus event
private void a33FocusGained(java.awt.event.FocusEvent evt) {
selectedTextField = (JTextComponent) evt.getSource();
}
// action for button
public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == B9)
selectedTextField.setText( selectedTextField.getText() + "9" );
}
}
with above code i expected to insert 9 to textPasswordField but it doesn't.
Are you sure that
private void a33FocusGained(java.awt.event.FocusEvent evt) {
selectedTextField = (JTextComponent) evt.getSource();
}
is ever called? I guess your class should implement FocusListener and add something like
passwordField.addFocusListener(this);
#Override
public void focusGained(FocusEvent e) {
selectedTextField = (JTextComponent) e.getSource();
}
#Override
public void focusLost(FocusEvent e) {
selectedTextField = null;
}
This is a sample code of what you should do (if I understood you correctly), note that at first you need to set cursor to the password field and after that the button will work, however you can see the bad side of this approach in the focusLost method
public class Snippet implements ActionListener, FocusListener {
public JFrame totalGUI = new JFrame();
private JPanel buttonPanel;
private JButton B9;
public Snippet() {
createContentPane();
}
public void createContentPane() {
buttonPanel = new JPanel(new GridBagLayout());
B9 = new JButton("9");
B9.addActionListener(this);
buttonPanel.add(B9);
JPasswordField passwordField = new JPasswordField(20);
passwordField.setSize(120, 30);
passwordField.addFocusListener(this);
buttonPanel.add(passwordField);
totalGUI.getContentPane().add(buttonPanel);
totalGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
totalGUI.pack();
}
private JTextComponent selectedTextField;
#Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == B9 && selectedTextField != null)
selectedTextField.setText(selectedTextField.getText() + "9");
}
public static void main(String[] args) {
new Snippet().totalGUI.setVisible(true);
}
#Override
public void focusGained(FocusEvent e) {
if(e.getSource() instanceof JTextComponent)
selectedTextField = (JTextComponent) e.getSource();
}
#Override
public void focusLost(FocusEvent e) {
// when you push the button the text field will lose focus
// selectedTextField = null;
}
}
Don't use a FocusListener and ActionListener together. This assumes that events will be fired in a certain order, that is first the focusGained and then the actionPerformed. Swing provides no guarantees about the order of the events.
Instead you can extend TextAction. TextAction is a special Action used be Swing components because it tracks the last component that had focus. For example to create an Action that selects all the text you can do:
class SelectAll extends TextAction
{
public SelectAll()
{
super("Select All");
}
public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.selectAll();
component.requestFocusInWindow();
}
}
Then to use the Action you would do:
b9.addActionListener( new SelectAll() );
I have a JTextField and a JButton, when user hits enter on the JTextField then action has to be performed on the corresponding JButton. Here is my code snippet.
Also i want to disable the JButton on enter and enable it later when something is changed in the JTextField
JButton jb=new JButton("Print");
JTextField jt=new JTextField(20);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println(jt.getText());
}
});
Add the same action listener to the button and the text field:
JButton jb = new JButton("Print");
JTextField jt = new JTextField(20);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(jt.getText());
}
};
jb.addActionListener(listener);
jt.addActionListener(listener);
Here is a modification of your code, i registered a DocumentListener to listen for text changes in JTextField and also added ActionListener to JTextField
jb=new JButton("Print");
jt=new JTextField(20);
jt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
jb.doClick();
}
});
jt.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void changedUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void removeUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
});
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println(jt.getText());
jb.setEnabled(false);
}
});
To perform same action on button click and enter press on Jfield.
JButton jb = new JButton("Print");
JTextField jt = new JTextField(20);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(jt.getText());
// Diable Button after action performed.
jb.setEnabled(false);
}
};
jb.addActionListener(listener);
jt.addActionListener(listener);
// Enable Button after any change in JtextField value. JTextField value change Listener refered from Link
jt.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void changedUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void removeUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
});
I would like to set editable option of a text box based on the selection of a radio button? How to code the action listener on the radio button?
This is the solution that I would use in this case.
//The text field
JTextField textField = new JTextField();
//The buttons
JRadioButton rdbtnAllowEdit = new JRadioButton();
JRadioButton rdbtnDisallowEdit = new JRadioButton();
//The Group, make sure only one button is selected at a time in the group
ButtonGroup editableGroup = new ButtonGroup();
editableGroup.add(rdbtnAllowEdit);
editableGroup.add(rdbtnDisallowEdit);
//add allow listener
rdbtnAllowEdit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setEditable(true);
}
});
//add disallow listener
rdbtnDisallowEdit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setEditable(false);
}
});
My Java is a little rusty, but this should be what you're looking for.
Here is your listener:
private RadioListener implements ActionListener{
private JTextField textField;
public RadioListener(JTextField textField){
this.textField = textField;
}
public void actionPerformed(ActionEvent e){
JRadioButton button = (JRadioButton) e.getSource();
// Set enabled based on button text (you can use whatever text you prefer)
if (button.getText().equals("Enable")){
textField.setEditable(true);
}else{
textField.setEditable(false);
}
}
}
And here is the code that sets it up.
JRadioButton enableButton = new JRadioButton("Enable");
JRadioButton disableButton = new JRadioButton("Disable");
JTextField field = new JTextField();
RadioListener listener = new RadioListener(field);
enableButton.addActionListener(listener);
disableButton.addActionListener(listener);
Another answer for this question. Modify a little code from zalpha314 's answer.
You could know which radio button is selected by the text of this button, and you could also know it by Action Command. In the oracle's radio button demo code http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java , I learnt how to use action command.
First, define two action command
final static String ON = "on"
final static String OFF = "off"
Then add action command to buttons
JRadioButton enableButton = new JRadioButton("Enable");
enableButton.setActionCommand(ON);
JRadioButton disableButton = new JRadioButton("Disable");
disableButton.setActionCommand(OFF);
So in actionPerformed, you could get the action command.
public void actionPerformed(ActionEvent e){
String ac = e.getActionCommand();
if (ac.equals(ON)){
textField.setEditable(true);
}else{
textField.setEditable(false);
}
}
Action command maybe better when the button.getText() is a very long string.
Try this:
JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// Do something here...
}
});
Try this:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewStudent {
public static void main(String[] args){
NewStudent st=new NewStudent();
}
public NewStudent(){
JFrame frame=new JFrame("STUDENT REGISTRATION FORM");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
JPanel p1=new JPanel();
p1.setLayout(null);
p1.setBackground(Color.CYAN);
frame.add(p1);
ButtonGroup buttonGroup=new ButtonGroup();
JRadioButton male=new JRadioButton("MALE");
male.setBounds(100,170,100,20);
buttonGroup.add(male);
p1.add(male);
JRadioButton female=new JRadioButton("FEMALE");
female.setBounds(250,170,100,20);
buttonGroup.add(female);
p1.add(female);
JLabel sex =new JLabel("SEX:");
sex.setBounds(10,200,100,20);
p1.add(sex);
final JTextField gender= new JTextField();
gender.setBounds(100,200,300,20);
p1.add(gender);
male.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ie){
gender.setText("MALE");
}
});
female.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ie){
gender.setText("FEMALE");
}
});
}
What is the equivalent of calling button3_Click(sender, e); in Java?
I am trying to make a text field action (hitting Enter) fire a button's code.
Thanks!
final JButton btn = new JButton("Click Me!");
JTextField txt = new JTextField(10);
InputMap inputMap = txt.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = txt.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), 13);
actionMap.put(13, new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
btn.doClick();
}
});
Use TextListener for TextField like
public void textValueChanged(TextEvent e) {
// call doClick();
}
For Button use ActionListener
public void actionPerformed(ActionEvent e) {
}
call doClick() method refer
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#doClick%28%29