The moose likes Java in General and the fly likes Error in JButton Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Error in JButton Code" Watch "Error in JButton Code" New topic

Error in JButton Code

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Can some please tell me hoe to fix the error in this code... i have tried everything.... the code is suppose to do the following
1)It creates a Button with a label on the button.
2)When pressed the label on the button changes to something diffrent.
3)A label at the buttom of the button also appears.
here is the code.. its a very small program...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonThing implements ActionListener
private JPanel jp;
private JButton jb;
private JLabel jl;
private JFrame jf ;
private int presses = 0;

public ButtonThing(String title) {
jf = new JFrame(title);
jp = new JPanel(new BorderLayout());
jb = new JButton("Click Me");
jl = new JLabel();
jp.add(jb, BorderLayout.NORTH);
jp.add(jl, BorderLayout.SOUTH);

jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {


public void actionPerformed(ActionEvent ae) {
jb.setLabel("Thank you for clicking");
jl.setText("Press count: " + presses);
public static void main(String args[])
// Run program with:
// java ButtonThing
String s = "Hello";
ButtonThing bt = new ButtonThing(s);

Any help would be grealty appreciated... thanks ..
the main error iam getting is this :
Note: C:\jdk1.3\bin\ uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

Thanks a lot

yours respectfully<br />Avin Sinanan
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Apparently you do not register any ActionListenr on your Button. Thus there is no callback performed when pushing the Button. Add the bold line.
public class ButtonThing implements ActionListener
private JButton jb;

public ButtonThing(String title) {
jb = new JButton("Click Me");

I agree. Here's the link:
subject: Error in JButton Code
It's not a secret anymore!