aaaa
aaaa

Reputation: 57

how to return the number when the button is clicked

My question is when the number is get from the an arraylist, for example JNumber.size()=10;

    for(int a=0;a<JNumber.size();a++)
    {
         btnNumber= new JNumber(""+(a+1));
         btnNumber.setPreferredSize(new Dimension(20, 10));
         panel.setLayout(new GridLayout(10,10));
         panel.add(btnNumber, BorderLayout.SOUTH);
    }

Then how to return the number when the button is clicked?

Output:

Number 2 is clicked.

Upvotes: 1

Views: 2129

Answers (3)

David Kroukamp
David Kroukamp

Reputation: 36423

Here is a small example hope it helps. Basically just add JButtons to JPanel, add ActionListener to each JButton, add JPanel to JFrame, after clicking a button a println() will be executed with the ActionCommand of the JButton (+1 to @Pr0gr4mm3r for setActionCommand() usage):

enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class ButtonsTest {

    public ButtonsTest() {
        initComponents();
    }

    private void initComponents() {

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(2, 2));//create gridlayput to hold buttons

        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //display action command of jbutton
                String ac = ((JButton) ae.getSource()).getActionCommand();
                System.out.println(ac);
                //display full test in Jbutton
                //String text = ((JButton) ae.getSource()).getText();
                //System.out.println(text);
            }
        };

        for (int i = 0; i < 4; i++) {
            JButton b = new JButton("No: " + String.valueOf((i + 1)));
            b.setActionCommand(String.valueOf((i + 1)));
            b.addActionListener(al);
            panel.add(b);
        }

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //set L&F and create UI on EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {//set L&F
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, you can set the GUI to another look and feel.
                }
                //create UI
                new ButtonsTest();
            }
        });
    }
}

Upvotes: 1

Pr0gr4mm3r
Pr0gr4mm3r

Reputation: 6230

You can do this in an ActionListener. Using the code given, this should work in your case:

If you want a text shown which is not only numeric then initialize the button with the specific text and set the actioncommand of the button to the actual number. (See below.)

btnNumber= new JNumber(""+(a+1));
btnNumber.setActionCommand(""+(a+1));    
btnNumber.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)evt.getSource();
        int num = Integer.parseInt(button.getActionCommand());
        System.out.println(num);
      }
    }); 

Upvotes: 2

Dan D.
Dan D.

Reputation: 32391

Declare btnNumber as final and add an ActionListener to it:

btnNumber.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.out.println("Number " + btnNumber.getText() + " is clicked");
  }
});

Upvotes: 0

Related Questions