0

I did a JButton array in Java, but I don't know to present them in the frame, is it possible?

This is the array, I inserted to each index ImageIcon:

JButton [] buttons=new JButton[55];
4
  • Well...how do you want to present them in the JFrame? What are the buttons for? It is most certainly possible. Commented Jun 28, 2021 at 11:13
  • I want to present them in blank Jframe. Commented Jun 28, 2021 at 11:21
  • Obviously but in what format? Like in a grid similar to a calculator? Commented Jun 28, 2021 at 11:41
  • yes, for example. Commented Jun 28, 2021 at 11:53

1 Answer 1

2

For example this:

javax.swing.JFrame frame = new javax.swing.JFrame("Just a JFrame Window Demo");
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setLayout(new java.awt.BorderLayout());

javax.swing.JPanel topPanel = new javax.swing.JPanel();
topPanel.setLayout(new java.awt.GridLayout());
javax.swing.JLabel headerLabel = new javax.swing.JLabel(
        "50 JButtons in a grid using the GridLayout Layout:");
headerLabel.setHorizontalAlignment(javax.swing.JLabel.CENTER);
headerLabel.setPreferredSize(new java.awt.Dimension(500, 80));
topPanel.add(headerLabel);

javax.swing.JPanel centerPanel = new javax.swing.JPanel();
centerPanel.setLayout(new java.awt.GridLayout(5, 10));
javax.swing.JButton[] buttons = new javax.swing.JButton[50];
for (int i = 0; i < buttons.length; i++) {
    buttons[i] = new javax.swing.JButton(String.valueOf(i));
    buttons[i].setName("button_" + i);
    buttons[i].addActionListener(new AllButtonsActionListener());
    centerPanel.add(buttons[i]);
}

javax.swing.JPanel bottomPanel = new javax.swing.JPanel();
bottomPanel.setLayout(new java.awt.FlowLayout());
javax.swing.JButton somethingButton = new javax.swing.JButton("Some Button");
somethingButton.addActionListener(new AllButtonsActionListener());

javax.swing.JButton exitButton = new javax.swing.JButton("Exit");
exitButton.addActionListener(new AllButtonsActionListener());
bottomPanel.add(somethingButton);
bottomPanel.add(exitButton);

frame.add(topPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.pack();

javax.swing.SwingUtilities.invokeLater(() -> {
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
});

And an inner class to catch the button selection:

// Inner Class
class AllButtonsActionListener implements java.awt.event.ActionListener {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
        String aCmd = e.getActionCommand().toLowerCase();
        System.out.println("Action Command is: " + aCmd);

        if (aCmd.equals("exit")) {
            System.exit(0);
        }
    }
}    

When the above code is run you should see...

enter image description here

When you select any button it's caption will be displayed in the console window.

Sign up to request clarification or add additional context in comments.

2 Comments

Also resize the window when you get a chance :)
somethingButton.setPreferredSize(..) Better avoided. Change the size of a button by changing the text length or font size, the size of the icon or the margin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.