Hi all

I'm wondering how i add Action listener to this Button
It has no text, just an image, from an ImageIcon
I tried I3 for the action listener but i doesant work

ImageIcon I3;

I3 = new ImageIcon ("carrier.gif");

J1 = new JButton (I3);
J1.addActionListener (this);
J1.setBounds (420, 196, 150, 25);
infoPane.add (J1);

Hi all

I'm wondering how i add Action listener to this Button
It has no text, just an image, from an ImageIcon
I tried I3 for the action listener but i doesant work

ImageIcon I3;

I3 = new ImageIcon ("carrier.gif");

J1 = new JButton (I3);
J1.addActionListener (this);
J1.setBounds (420, 196, 150, 25);
infoPane.add (J1);

check this just to be sure you did it correctly:http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

i once wrote an app that had a image as a button except i didnt use a jButton, i used a label and then added an action listener to that... hope this helps

if not here's a link i found try this:http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

check this just to be sure you did it correctly:http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

i once wrote an app that had a image as a button except i didnt use a jButton, i used a label and then added an action listener to that... hope this helps

if not here's a link i found try this:http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

umm, I looked their already, because this is usually my last resort,
so It didn't really help, thanks anyway

umm, I looked their already, because this is usually my last resort,
so It didn't really help, thanks anyway

try looking at my 2nd link, and if not then why not just use a label and add an actionlistener to that?

try looking at my 2nd link, and if not then why not just use a label and add an actionlistener to that?

I did look at second link, and doesant matter what i use, how would i add actionlister

becouse this doesant work

if (event.equals (I3))
        {
            Boattype = "Carrier";
        }

All my other Buttons work becouse they have text
like so

B1 = new JButton ("Play");
        B1.setFont (new Font ("Arial", Font.BOLD, 18));
        B1.setBorder (compound);
        B1.setOpaque (false);
        B1.setBounds (400, 140, 200, 30);
        B1.addActionListener (this);
        infoPane.add (B1);

and then i would add it to action listener like so

if (event.equals ("Play"))
        {
            try
            {
                InputStream b = new FileInputStream ("Bomb.wav");
                bomb = new AudioStream (b);
            }
            catch (java.io.IOException z)  //catching the exception
            {
            }

            AudioPlayer.player.start (bomb); //as soon as Button is clicked
            Play ();
        }

Just i dont know what to put it to listen for, becouse their is no text, just a picture
which is I3 as seen in my first post
and I3 doesant respond

why not try:

if (event.equals ("J1")){}

use the buttons actual name and not the text. I had the source of a program like that but i cant find it now, but it worked on the buttons name and not the text set to the button.check this maybe:http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Button.html#setActionCommand(java.lang.String) especially setActionCommand()

why not try:

if (event.equals ("J1")){}

use the buttons actual name and not the text. I had the source of a program like that but i cant find it now, but it worked on the buttons name and not the text set to the button

Nope, Doesn't work, thanks anyway

Nope, Doesn't work, thanks anyway

Read my edited post use the link. look here:

Button b = new Button("Click me");
                add(b);
                add(text);
                b.addActionListener(this);
                b.setActionCommand("J1");
//in actionPerformed(ActionEvent e) {} you would put this
if(e.getActionCommand().equals("J1")){System.out.println("J1 pressed");}

now we manually set an actioncommand for the button i just tested this myself it should work no problems

Nope, Doesn't work, thanks anyway

be sure that works

commented: was even doubting myself :) +5

Read my edited post use the link. look here:

Button b = new Button("Click me");
                add(b);
                add(text);
                b.addActionListener(this);
                b.setActionCommand("J1");
//in actionPerformed(ActionEvent e) {} you would put this
if(e.getActionCommand().equals("J1")){System.out.println("J1 pressed");}

now we manually set an actioncommand for the button i just tested this myself it should work no problems

Yes, works perfect, no problems thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.