identifying buttons in applet
hi all,
In my applet i have a button named "btn1".After clicking it you can create multiple buttons just by clicking somewhere within the applet. Now the problem is i cannot perform an actionevent for the newly created buttons . I can only perform action for the button that have been created lastly.For example, i want to make a button invisible by clicking on it.But that event works only for the button that have been created lastly and not working for the other buttons that have been created. Please help me solve thisproblem.....Very urgent....
My code looks like this....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class repainting extends JApplet implements ActionListener,MouseListener,MouseMotionListener
{
JButton btn1,btn2;
public void init()
{
setLayout(null);
btn1=new JButton("btn1");
btn1.setBounds(0,0,100,30);
add(btn1);
btn1.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
btnclk=true;
}
else if(e.getSource()==btn2)
{
btn2.setVisible(false);
}
}
public void mouseReleased(MouseEvent e) {
if(btnclk)
{
btn2=new JButton("btn2");
add(btn2);
btn2.setBounds(curx,cury,100,30);
btn2.addActionListener(this);
}
}
public void mouseMoved(MouseEvent e) {
curx=e.getX();
cury=e.getY();
repaint();
}
public void paint(Graphics g)
{
if(btnclk)
{
g.drawRect(curx,cury,100,30);
}
}
}
[1734 byte] By [
ravisenana] at [2007-10-2 15:30:16]

call validate(); after adding, removing ... any Components to/from the view. This refreshes the view and your buttons start working.
hi mezler,Since i am new to this,i dont know how to use this validate method.I have searched in google several times and i cannot find an understandable solution.Please help me do that by making necessary changes in my code.Its very urgent.Thanks in advanceRegards
just call it - for example when adding a button:
if(btnclk)
{
btn2=new JButton("btn2");
add(btn2);
btn2.setBounds(curx,cury,100,30);
btn2.addActionListener(this);
validate(); // in swing you may call revalidate() which does a validate in event queue
}
do this whenever you change the gui (adding or removing components)
hi
try this
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class repainting extends JApplet implements ActionListener,MouseListener,MouseMotionListener
{
JButton btn1;
static int counter = 0;
int buttonNumber = 0;
boolean btnclk;
public void init()
{
setLayout(null);
btn1=new JButton("btn1");
btn1.setBounds(0,0,100,30);
add(btn1);
btn1.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
btnclk=true;
}
else
{
String l_strCommand = e.getActionCommand();
buttonNumber = Integer.parseInt(l_strCommand.substring(l_strCommand.length() - 1, l_strCommand.length()));
btnclk=true;
}
//else if(e.getSource()==btn2)
{
//btn2.setVisible(false);
}
}
int curx,cury;
public void mouseReleased(MouseEvent e) {
if(btnclk)
{
JButton btnNew=new JButton("btn" + " - " + buttonNumber + counter++);
add(btnNew);
btnNew.setBounds(curx, cury, 100, 30);
btnNew.addActionListener(this);
btnclk = false;
}
}
public void mouseMoved(MouseEvent e) {
curx=e.getX();
cury=e.getY();
repaint();
}
public void paint(Graphics g)
{
if(btnclk)
{
g.drawRect(curx, cury, 100, 30);
}
}
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
}
}
r u making games?
best of luck
may be this one can solve better
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class repainting extends JApplet implements ActionListener,MouseListener,MouseMotionListener
{
JButton btn1;
static int counter = 0;
int buttonNumber = 0;
boolean btnclk;
public void init()
{
setLayout(null);
btn1=new JButton("btn1");
btn1.setBounds(0,0,100,30);
add(btn1);
btn1.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
btnclk=true;
}
else
{
String l_strCommand = e.getActionCommand();
buttonNumber = Integer.parseInt(l_strCommand.substring(l_strCommand.length() - 1, l_strCommand.length()));
btnclk=true;
}
//else if(e.getSource()==btn2)
{
//btn2.setVisible(false);
}
}
int curx,cury;
public void mouseReleased(MouseEvent e) {
if(btnclk)
{
JButton btnNew=new JButton("btn" + " - " + buttonNumber + counter++);
add(btnNew);
btnNew.setBounds(curx, cury, 100, 30);
btnNew.addActionListener(this);
btnclk = false;
setSize(getSize().width + 1, getSize().height);
setSize(getSize().width - 1, getSize().height);
}
}
public void mouseMoved(MouseEvent e) {
curx=e.getX();
cury=e.getY();
repaint();
}
public void paint(Graphics g)
{
if(btnclk)
{
g.drawRect(curx, cury, 100, 30);
}
}
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
}
}
hi mezler,
I tried using validate() method,but that dint make any difference." the method revalidate is undefined for the type painting." this is the error i get when using revalidate().My problem is, I have to drag the newly created buttons.But i can only drag the recently created button and not the others.To drag all the buttons i have to know which button is being clicked.Since they are created dynamically its not possible to add actionlistener to everybutton thats being created.Neither we cant check like "if(e.getSource()==btn2)".Please help me to solve this problem.
Thanks in advance
Regards
hi aniruddha,
Its not gaming, but similar to that :). I have tried your code,and found that you have played with numbers.I am not trying to change the label of the button.What i need is to drag the buttons that have been created dynamically using btn2=new JButton("btn2"); Please try to solve this problem and help me.
Thanks in advance
Regards
hi!
try this now, and pls reply what exactly ur target. any way let me know if i have solved ur prob
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class repainting extends JApplet implements ActionListener,MouseListener,MouseMotionListener
{
JButton btn1;
MyButton btn2;
static int counter = 0;
int buttonNumber = 0;
boolean btnclk;
public void init()
{
setLayout(null);
btn1=new JButton("btn1");
btn1.setBounds(0,0,100,30);
add(btn1);
btn1.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
btnclk=true;
}
else
{
//String l_strCommand = e.getActionCommand();
//buttonNumber = Integer.parseInt(l_strCommand.substring(l_strCommand.length() - 1, l_strCommand.length()));
//btnclk=true;
}
//else if(e.getSource()==btn2)
{
//btn2.setVisible(false);
}
}
int curx,cury;
public void mouseReleased(MouseEvent e) {
if(btnclk)
{
//JButton btnNew=new JButton("btn" + " - " + buttonNumber + counter++);
btn2 = new MyButton("btn2");
add(btn2);
btn2.setBounds(curx, cury, 100, 30);
btn2.addActionListener(this);
btnclk = false;
setSize(getSize().width + 1, getSize().height);
setSize(getSize().width - 1, getSize().height);
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(getContentPane(), ((MyButton)e.getSource()).getCreationTime());
}
});
}
}
public void mouseMoved(MouseEvent e) {
curx=e.getX();
cury=e.getY();
repaint();
}
public void paint(Graphics g)
{
if(btnclk)
{
g.drawRect(curx, cury, 100, 30);
}
}
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
}
class MyButton extends JButton
{
private long creationTime;
/**
*
*/
public MyButton()
{
super();
init();
}
/**
* @param a
*/
public MyButton(Action a)
{
super(a);
init();
}
/**
* @param icon
*/
public MyButton(Icon icon)
{
super(icon);
init();
}
/**
* @param text
* @param icon
*/
public MyButton(String text, Icon icon)
{
super(text, icon);
init();
}
/**
* @param text
*/
public MyButton(String text)
{
super(text);
init();
}
private void init()
{
creationTime = System.currentTimeMillis();
}
/**
* @return Returns the creationTime.
*/
public long getCreationTime()
{
return creationTime;
}
}
}
hi Aniruddha,Thank you very much.I got what i wanted from "YOUR" code.This is the idea i was looking for.The problem is solved. Now i can drag all the buttons created.Thanks a lot.Regards