referencing a JButton by name

Hi to all,

I'm new in Java, doing simple Yamb (Yahtzee) app for a class in Java programming I attend...

Anyhow, this is what I have (a 2D grid of buttons):

int row = 1;

int column = 1;

for(int i=0; i<24; i++){

if(i%4==0){

if(i!=0){

this.add(new JLabel(""),c);

c.gridy++;

row++;

column= 1;

}

this.add(new JLabel("" +j++, JLabel.CENTER),c);

}

JButton b = new JButton(" ");

b.setName(column + "_" + row);

b.setMnemonic(KeyEvent.VK_E);

b.setActionCommand(column + "_" + row);

b.addActionListener(this);

this.add(b,c);

column++;

}

.

.

.

public void actionPerformed(ActionEvent e) {

String str = new String(e.getActionCommand());

tf.setText(str);

JButton btnSource = (JButton)e.getSource();

btnSource.setText(str);

btnSource.setEnabled(false);

}

OK, so this works...what I would like to be able to do is when I press a button, I get its name i.e. "1_1"...I can tokenize that and get a column and a row from it...so I would like to increment, let's say a row value and reference to button with that name so I can enable/disable it...

I need something like eval(String JButton.Name).enable(false)...

Can this be done without beans? Referencing a JButton instance by its name?

Thanks for listening (reading)...

Cheers,

Alen

[1460 byte] By [_alena] at [2007-11-26 16:43:44]
# 1
How about instead of creating buttons and throwing away the references to them, you instead create an array or other kind of collection to put your buttons in? Then you would already have a nice collection to get into, to get the references to the buttons.
warnerjaa at 2007-7-8 23:10:55 > top of Java-index,Java Essentials,Java Programming...
# 2

@warnerja - thanks for the reply...

and to answer my own question:

// create the Map itself:

Map TB= new HashMap();

// populate the Map with 'n' names and components:

for (int i= 0; i < n; i++)

TB.put("TB"+i, new JTextField());

// now use any ((JComponent)map.get("TB"+i))

I found this here on the forum (posted by JosAH) - this forum is a great resource, tons of stuff here...

Cheers,

Alen

_alena at 2007-7-8 23:10:55 > top of Java-index,Java Essentials,Java Programming...