0

I would really appreciate an answer: I have a few JButtons generated with this:

for(int i = 0; i < buttons.length; i++){
        buttons[ i] = new JButton(blank);
        if(i == 3 || i == 6){
            newLine++;
            lineCount = 0;
        }
        buttons[ i].setBounds(lineCount*150,newLine*150,150,150);
        cont.add(buttons[ i]);
        buttons[ i].addActionListener(this);
        lineCount++;
    }

so of course they don't have global names...

Because of this, I need to know how to take the image "out" of the JButton so I know what kind of button it is or be able to identify buttons by name(?).
So how can I do this? Thanks!

by the way, cont is a java.awt.Container

blustone
  • 315
  • 4
  • 13
  • 3
    Best to get rid of the `null` layout and the `setBounds` calls. Use a `LayoutManager` instead – Robin Nov 07 '12 at 06:55
  • Rows in DB tables are identified by string IDs and not by images. There could be an analogy here :) – Dan D. Nov 07 '12 at 07:15
  • For a rectangular array of buttons, see this [approach](http://stackoverflow.com/a/7706684/230513). – trashgod Nov 07 '12 at 11:24

3 Answers3

4

I don't find a good approach to identify buttons based on their icons. Your components, including JButtons, can have names that you can use for identification. This is how acceptance testing tools work.

button.setName(uniqueName);
button.getName();
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    *"I don't find a good approach to identify buttons based on their icons."* +1 I was typing much the same as a comment. This question reeks of bad code design from even reading the title. – Andrew Thompson Nov 07 '12 at 06:54
  • This question can be restated as how to retrieve an imageicon from the JButton – Aniket Inge Nov 07 '12 at 06:55
  • 1
    The question may be restated to sound fine, but the goal is really funny. Once you get the image icon, how do you identify the button? Do you make a pixel by pixel comparison? – Dan D. Nov 07 '12 at 06:57
  • You area aware of `Event.getSource()`? Much easier way to identify which button was clicked, though most would recommend adding a separate listener to each button, wherein the source is clear. – Andrew Thompson Nov 07 '12 at 07:01
  • @AndrewThompson I use an action listener but I don't know why I'm getting an EventDispatchThread error. – blustone Nov 07 '12 at 07:04
2

use setIcon() and getIcon() methods to set and get image-icons on JButton

Edit in question, demands edit in answer:

Identification of button is best done with either:

  1. Component.getName() and Component.setName() or
  2. using different strings with getText() and setText().
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

use putClientProperty for identifying JComponent

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

and get from ActionListener (for example)

public class MyActionListener implements ActionListener {

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

but proper way for JButton should be to use Swing Action instead of ActionListener

mKorbel
  • 109,525
  • 20
  • 134
  • 319