Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:31:31+00:00 2026-05-30T01:31:31+00:00

Okay so I am making a 2d array of JToggleButtons. I got the action

  • 0

Okay so I am making a 2d array of JToggleButtons. I got the action listener up and going, but I have no way to tell which button is which.

If I click one, all it returns is something like

javax.swing.JToggleButton[,59,58,19×14,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@53343ed0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=]

Is there anyway to stick some sort of item or number in the button object to associate each button?
And then when the button is clicked I can retrieve that item or number that was given to it?

Here is my button generator code. (How could I make “int l” associate (and count) to each button made, when it is called, it will return that number, or something along those lines.

JToggleButton buttons[][] = new JToggleButton[row][col];
int l = 0;


        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                buttons[i][j] = new JToggleButton("");
                buttons[i][j].setSize(15,15);
                buttons[i][j].addActionListener(new e());
                panel.add(buttons[i][j]);
                l++;

            }
        }

ActionListner

public class e implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        System.out.println(source);
    }

}

variable “source” is what I use to get my data, so how can int l, be returned through “source” (as its unique value for the unique button clicked) as a button is clicked?

Thanks,
-Austin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:31:33+00:00Added an answer on May 30, 2026 at 1:31 am

    very simple way is add ClientProperty to the JComponent, add to your definition into loop e.g.

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

    rename e to the MyActionListener and change its contents

    public class MyActionListener implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            JToggleButton btn = (JToggleButton) e.getSource();
            System.out.println("clicked column " + btn.getClientProperty("column")
                    + ", row " + btn.getClientProperty("row"));
    }
    

    EDIT:

    for MinerCraft clone isn’t required to implements ony of Listeners, there is only about Icon, find out that in this code (don’t implement any of Listeners anf remove used ItemListener)

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonsIcon extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
        private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
        private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ButtonsIcon t = new ButtonsIcon();
                }
            });
        }
    
        public ButtonsIcon() {
            setLayout(new GridLayout(2, 2, 4, 4));
    
            JButton button = new JButton();
            button.setBorderPainted(false);
            button.setBorder(null);
            button.setFocusable(false);
            button.setMargin(new Insets(0, 0, 0, 0));
            button.setContentAreaFilled(false);
            button.setIcon((errorIcon));
            button.setRolloverIcon((infoIcon));
            button.setPressedIcon(warnIcon);
            button.setDisabledIcon(warnIcon);
            add(button);
    
            JButton button1 = new JButton();
            button1.setBorderPainted(false);
            button1.setBorder(null);
            button1.setFocusable(false);
            button1.setMargin(new Insets(0, 0, 0, 0));
            button1.setContentAreaFilled(false);
            button1.setIcon((errorIcon));
            button1.setRolloverIcon((infoIcon));
            button1.setPressedIcon(warnIcon);
            button1.setDisabledIcon(warnIcon);
            add(button1);
            button1.setEnabled(false);
    
            final JToggleButton toggleButton = new JToggleButton();
            toggleButton.setIcon((errorIcon));
            toggleButton.setRolloverIcon((infoIcon));
            toggleButton.setPressedIcon(warnIcon);
            toggleButton.setDisabledIcon(warnIcon);
            toggleButton.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (toggleButton.isSelected()) {
                    } else {
                    }
                }
            });
            add(toggleButton);
    
            final JToggleButton toggleButton1 = new JToggleButton();
            toggleButton1.setIcon((errorIcon));
            toggleButton1.setRolloverIcon((infoIcon));
            toggleButton1.setPressedIcon(warnIcon);
            toggleButton1.setDisabledIcon(warnIcon);
            toggleButton1.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (toggleButton1.isSelected()) {
                    } else {
                    }
                }
            });
            add(toggleButton1);
            toggleButton1.setEnabled(false);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I think I'm just making a stupid mistake here, but I want to
Okay, Ive got a cog (toothed gear) that I have drawn in opengl. I
okay i making a jQuery plugin to make a slide down menu on click,
Okay, got a question. I have assembled a bit mask for options. Basically my
Okay I have seen some very similar questions here but none seem to be
Okay, so I'm making a table right now for Box Items. Now, a Box
Okay, so I am basically making a script to pass post data using cURL.
Okay so here's what I'm doing. I'm making a request to a server to
Okay, so I'm running a small test webserver on my private network. I've got
Okay so im working on this php image upload system but for some reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.