0

My code simply displays a random colour and has a button to get a new one but how could I make the ActionListener respond to pressing Enter as well?

Also, how can I make the button not fill the whole width of the JFrame?

Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomColour {

    public static JFrame frame;

    public static Color[] colours = {Color.RED, Color.YELLOW, Color.ORANGE, Color.BLUE, Color.GREEN, Color.BLACK, Color.MAGENTA, Color.CYAN, Color.PINK,};

    public static int random;

    public static void main(String[] args) {


        random = (int)(Math.random()*(9)+0);

        JButton button = new JButton ("New Random Colour");

        button.setSize(10, 10);

        frame = new JFrame ("Random Colour");
        JPanel panel = new JPanel(new GridLayout(6,6,6,6));

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().setBackground(colours[(int)(Math.random()*(9)+0)]);
            }
        });


        panel.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setContentPane(panel);
        frame.getContentPane().setBackground(colours[random]);
        frame.setLocationRelativeTo(null);
    }
}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • 1
    Possible duplicate of [*Fire a JButton with spacebar, or enter key, or mouse click*](http://stackoverflow.com/q/10710748/230513), which illustrates `setDefaultButton()`. – trashgod Nov 28 '16 at 21:10

2 Answers2

1

The button is already supposed to react to the Enter keypress but it needs to be set as the default button for your frame and it needs to have the focus, so add the following two lines to your code and it will work as you are expecting it to:

frame.getRootPane().setDefaultButton(button);
button.requestFocus();

The more detailed illustration below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomColour {
    public static JFrame frame;

    public static Color[] colours = {Color.RED, Color.YELLOW, Color.ORANGE, Color.BLUE, Color.GREEN, Color.BLACK, Color.MAGENTA, Color.CYAN, Color.PINK,};

    public static int random;

    public static void main(String[] args) {


        random = (int)(Math.random()*(9)+0);

        JButton button = new JButton ("New Random Colour");

        button.setSize(10, 10);

        frame = new JFrame ("Random Colour");
        frame.setLayout(new GridLayout(3,2));

        JPanel panel = new JPanel(new GridLayout(3,2));

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().setBackground(colours[(int)(Math.random()*(9)+0)]);
            }
        });

        panel.add(button);

        // The changes are the next two lines
        frame.getRootPane().setDefaultButton(button);
        button.requestFocus();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setContentPane(panel);
        frame.getContentPane().setBackground(colours[random]);
        frame.setLocationRelativeTo(null);
    }
}

Swing uses layout manager to handle components presentations and arrangement for you, if you want more flexibility you should consider using the GridBagLayout ( Example here) or design more complexe layout by combining existing layout managers (such as the default BorderLayoutManager with GridLayout for example)

alainlompo
  • 4,414
  • 4
  • 32
  • 41
0

So you have 2 options,

you can either copy the entire action listener code you already wrote and add it to the enter button the same way.

OR...

You can make the action listener into a named object (example)

ActionListener thing = new ActionListener(...)

then

button.addActionListener(thing)
enterbutton.addActionListener(thing)

will stick the same one onto both.

Sidharth Ghoshal
  • 658
  • 9
  • 31