Hello, I have problem with jbotton. I have botton decalre it. but how do i access it ? because I have 10X10 or 100 botton. let i want to access 95 botton how can i do that? how do i set the title?

here is code

import java.awt.*;

import javax.swing.*;
public class BattleShip extends JFrame

    public static void main(String[] args)
        int rows = 10;
        int cols = 10;
        BattleShip newbattleship = new BattleShip(rows, cols );
        //newbattleship =("This my title");
        //newbattleship.setSize (400, 300);
       // newbattleship.setLocationRelativeTo (null);
    public BattleShip (int rows, int cols) 
     Container pane = getContentPane();
        pane.setLayout(new GridLayout(rows, cols));
        for (int i = 0; i < 100; i++)
          JButton button = new JButton(Integer.toString(i + 1));


You could store your button references in a 10x10 array (or a 100 element array) as you create them.

The code I show above is running and display on 100 botton. Now I need to access to it and assign thing in it. For example I write something in 85 botton. When I click 85 it will show that the information I put it in early.


You could store your button references in a 10x10 array (or a 100 element array) as you create them.

hmmm why bothering with, most of Object in Java APIs has historical artifacts from the distant past - public final void putClientProperty(Object key, Object value)

then (for/grom Swing rellated APIs)

in the loop

buttons[i][j].putClientProperty("column", i);
buttons[i][j].putClientProperty("row", j);
buttons[i][j].addActionListener(new MyActionListener());

and to get value from ActionListener (for example)

public class MyActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton) e.getSource();
        System.out.println("clicked column " + btn.getClientProperty("column")
                + ", row " + btn.getClientProperty("row"));

excelent, short, simple (it should be) with lambdas in Java8


to OP - let i want to access 95 botton how can i do that?

  • use JToggleButtons instead

  • use GridLayout

Yes, I also would use client properties to identify the button that was clicked, or maybe an action listener that takes a button number in its constructor.
The array suggestion was to answer "i want to access button 95", as opposed to "i want to handle a click in button 95". Maybe its just got lost in translation :)

I still don't understand how does the action listener know which number have been hit?

Let say #43 contain $23 and #90 contain $57….……… and user pass #90 it should display $57 not $23 how does AL know that. Should I have a for loop to loop the number? 0 to 100? I am new GUI sorry

short answer

read Oracle tutorials about how to use AtctionListener and JButton too


long answer

have to test whats happens

Use the client property (see mKorbel's code for an example) to store the $23 or whatever in each button. Then in your action listener you can retrieve that (see mKorbel's code for an example).