Button location as an event source? [HELP?]
I'm trying to make a piano keyboard. Here's what I was doing. Instead of creating individual button objects, i thought i'd just create the same objects on a loop with varying x_location.
I was just wondering if there's any piece of code I could use on the actionPerformed() to make the location/bounds of the object be used an indication/event source when it's clicked?
publicclass Keyboardextends JFrameimplements ActionListener
{
publicfinalint FRAME_WIDTH = 600;
publicfinalint FRAME_HEIGHT = 400;
privateint keyGap = 0;
Container contentPane = getContentPane();
public Keyboard()
{
//frame properties
//only to be used for this code
setTitle("Digital Music Sampler Keyboard");
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane.setLayout(null);
//create keys
for(int x = 0; x < 20; x++)
{
createWhiteKeys(keyGap);
keyGap+=21;
}
}
privatevoid createWhiteKeys(int gap)
{
// JButton buttonA, buttonB, buttonC, buttonD,
//buttonE, buttonF, buttonG;
//JFrame frame = new JFrame();
button =new JButton();
button.setBackground(Color.white);
button.setBounds(75+keyGap,100,20,75);
contentPane.add(button);
}
Is this possible?
Or.. do I just create the buttons individually, add the actionlistener.. and done..
[2554 byte] By [
Neutrinoa] at [2007-10-3 3:33:22]

sure, if i understand your problem correctly... (which i doubt :-) )
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Keyboard extends JFrame implements ActionListener {
public final int FRAME_WIDTH = 600;
public final int FRAME_HEIGHT = 400;
private int keyGap = 0;
Container contentPane = getContentPane();
public Keyboard() {
// frame properties
// only to be used for this code
setTitle("Digital Music Sampler Keyboard");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane.setLayout(null);
// create keys
for (int x = 0; x < 20; x++) {
createWhiteKeys(keyGap);
keyGap += 21;
}
setVisible(true);
}
private void createWhiteKeys(int gap) {
JButton button = new JButton();
button.setBackground(Color.white);
button.setBounds(75 + keyGap, 100, 20, 75);
contentPane.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
System.out.println(button.getLocation());
}
public static void main(String[] args) {
new Keyboard();
}
}
Sorry if my question didn't really make sense..
Thanks for that.. Although what I wanted is to use that location to tell the actionEvent which button [at this location] was pressed.
I mean, I could create 20 button objects and add the actionlistener to each one. But I reckon creating a loop to create 20 of the same object is much faster and efficient.
Now having those 20 same objects.. how can i tell the actionlistener to differentiate between which button was pressed? or can I?
Is there an "event.source" kinda thing that gives you the location/bounds instead?
Thanks!
> I mean, I could create 20 button objects and add the
> actionlistener to each one.
you ARE doing it! each createWiteKeys call creates a new button
> But I reckon creating a
> loop to create 20 of the same object is much faster
> and efficient.
what?
I believe you are confusing same variable (button) with same object... each time you call 'new JButton();' it's a new object, no matter if you are storing it in the same variable or not
> how can i tell the
> actionlistener to differentiate between which button
> was pressed? or can I?
using the same action listener does the job... the action event stores the source of the action (the button), so that's what you need (look at the actionPerformed above)
> Is there an "event.source" kinda thing that gives you
> the location/bounds instead?
the event source is the same JButton object that is being displayed... just get its location/bounds, as shown in the last code snippet
Swing related questions should be posted in the Swing forum.Maybe this simple Calculator example will give you some ideas of how to share an ActionEvent with multiple buttons: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=609795
Why dont you just create an array of JButtons? Try something like this:
public class Keyboard extends JFrame implements ActionListener
{
public final int FRAME_WIDTH = 600;
public final int FRAME_HEIGHT = 400;
private int keyGap = 0;
Container contentPane = getContentPane();
private JButton[] keys;
public Keyboard()
{
//frame properties
//only to be used for this code
setTitle("Digital Music Sampler Keyboard");
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane.setLayout(null);
//create keys
for(int x = 0; x < 20; x++)
{
keys[x] = new JButton();
keys[x].setBackground(Color.white);
keys[x].setBounds(75+(x*21), 100, 20, 75);
contentPane.add(keys[x]);
}
}
}
I hope this helps.