Reputation: 45
so i want my buttons to be labeled 1-9 but I dont want to list out all the action listeners and action-commands for each button. How can I do that
and also I cannot use add.ActionListener(this) so what can i use
JButton[] button = new JButton[9];
panel.setLayout(new GridLayout(3,3));
for (int i = 0; i < button.length; i++) {
button[i] = new JButton();
panel.add(button[i]);
String bu = Integer.toString(i);
button[i].setActionCommand(bu);
button[i].addActionListener(new ActionListener());
Sorry im new to java swing so its abit confusing still
Upvotes: 1
Views: 550
Reputation: 17454
and also I cannot use add.ActionListener(this) so what can i use
I will interpret what you meant here as "you are not allowed to let the container class implements ActionListener, but still allowed to use ActionListener".
If that is the case, you have at least 2 more choices:
An example using GridLayout with inner-class Actionlistener:
how to put actionlistenerand actioncommand to multiple jbuttons
The following uses inner class to handle buttons' action.
class MainPanel extends JPanel //not implementing ActionListener here
{
private JButton[] btns;
public MainPanel(){
setPreferredSize(new Dimension(150, 150));
setLayout(new GridLayout(3, 3));
initComponents();
addComponents();
}
private void initComponents(){
btns = new JButton[9];
ButtonHandler bh = new ButtonHandler();
for(int x=0; x<btns.length; x++){
btns[x] = new JButton(Integer.toString(x+1));
btns[x].addActionListener(bh); //NOT using addActionListener(this)
}
}
private void addComponents(){
for(int x=0; x<btns.length; x++)
add(btns[x]); //Add in sequential order into grid layout
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e){
String s = ((JButton)e.getSource()).getText();
JOptionPane.showMessageDialog(null, "Button " + s + " was clicked.");
}
}
}
Finally, run your codes in the EDT:
class TestRunner
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame("Buttons Pad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Upvotes: 0
Reputation: 312
You have to implement the actionListener
public class Butt implements ActionListener {
public JPanel method()
{
JPanel panel = new JPanel();
JButton[] button = new JButton[9];
panel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < button.length; i++)
{
button[i] = new JButton(""+i);
panel.add(button[i]);
String bu = Integer.toString(i);
button[i].setActionCommand(bu);
button[i].addActionListener(this);
}
return panel;
}
@Override
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new Butt().method());
frame.setVisible(true);
frame.setSize(500, 500);
}
} see now there is no errors.
Upvotes: 0
Reputation: 324118
I cannot use add.ActionListener(this) so what can i use
You create a class that implements an ActionListener.
Or better yet create a class that implement Action. An Action is the same as an ActionListener. The benefit is that an Action can be used with Key Bindings.
Here is a basic example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CalculatorPanel extends JPanel
{
private JTextField display;
public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
// display.setCaretPosition( display.getDocument().getLength() );
display.replaceSelection(e.getActionCommand());
}
};
setLayout( new BorderLayout() );
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(30, 30) );
buttonPanel.add( button );
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(text), text);
inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
button.getActionMap().put(text, numberAction);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Now you can either click on the button or type the number and the value will be inserted into the text field.
Upvotes: 2
Reputation: 205
Just add on main action performed method.
Example would be like this :
public void actionPerformed(ActionEvent e){
// your todo code here
}
Make sure to import the appropriate packages.
Upvotes: 0