button in jpanel issue

What's up everybody? I'm trying to create an applet which essentially boils down to two jpanels. The top panel contains a button and a textfield, and the bottom panel will be used for graphics.

As of now, however, I get an error (paneltest is not abstract and does not override abstract method actionPerformed) when I attempt to add ActionListener to the button in the top panel.

I've looked pretty extensively over this forum and over tutorials trying to figure out how to make this work, but to no avail yet. Here's the bare-bones code:

import java.awt.*;

import javax.swing.*;

import java.awt.geom.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/*

<APPLET

CODE=paneltest.class

WIDTH=700

HEIGHT=500 >

</APPLET>

*/

publicclass paneltestextends JAppletimplements ActionListener

{

publicvoid init()

{

Container contentPane = getContentPane();

contentPane.setLayout(null);

JTextField dateinput =new JTextField(12);

dateinput.setText("Enter Date");

JButton startbutton =new JButton("Graph Data");

// create new panels

JPanel toppanel =new JPanel();

JPanel bottompanel =new JPanel()

{

publicvoid paintComponent (Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

Stroke drawingStroke =new BasicStroke(1);

Stroke drawingStrokeb =new BasicStroke(2);

g2d.setStroke(drawingStroke);

Rectangle2D.Double square =new Rectangle2D.Double(75, 20, 550, 380);

g2d.draw(square);

}

};

//add panels to frame

contentPane.add(toppanel);

contentPane.add(bottompanel);

toppanel.setBounds(0,0,700,50);

bottompanel.setBounds(0,50,700,450);

toppanel.add(dateinput);

toppanel.add(startbutton);

//***error occurs when I add ActionListener

startbutton.addActionListener(this);

}

}

Thanks!

P.S. If I've committed any horrible Java coding sins, let me know; good advice is priceless.

[3253 byte] By [rayleighsounda] at [2007-11-27 8:20:30]
# 1
you add an actionListener to your button so that it can do something when clickedso, where is the code that this button-clicking is supposed to trigger?(normally it would be in a method actionPerformed(..)
Michael_Dunna at 2007-7-12 20:08:49 > top of Java-index,Desktop,Core GUI APIs...
# 2

I decided to leave that aside because the error occurs just when trying to add the ActionListener to begin with.Regardless, here's the remainder of the code:

(On running this, I instead get an 'illegal start of expression' error at 'public void actionPerformed'. I thought this was kind've a 'child' error to the ActionListener issue, hence why I left out the extra code. Sorry.)

import java.awt.*;

import javax.swing.*;

import java.awt.geom.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/*

<APPLET

CODE=paneltest.class

WIDTH=700

HEIGHT=500 >

</APPLET>

*/

public class paneltest extends JApplet implements ActionListener

{

//establish variables

int ylist[] = new int[10];

double db_incr = 550/ylist.length;

int x_incr = (int) db_incr;

int x_pos = 75 + x_incr;

int y_max = 0;

int y_incr = y_max/ylist.length;

boolean graph_req = false;

public void init()

{

Container contentPane = getContentPane();

contentPane.setLayout(null);

JTextField dateinput = new JTextField(12);

dateinput.setText("Enter Date");

JButton startbutton = new JButton("Graph Data");

// create new panels

JPanel toppanel = new JPanel();

JPanel bottompanel = new JPanel()

{

public void paintComponent (Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

Stroke drawingStroke = new BasicStroke(1);

Stroke drawingStrokeb = new BasicStroke(2);

g2d.setStroke(drawingStroke);

Rectangle2D.Double square = new Rectangle2D.Double(75, 20, 550, 380);

g2d.draw(square);

g2d.setColor(Color.red);

g2d.setStroke(drawingStrokeb);

if(graph_req == true) {

for(int loop_index = 1; loop_index < ylist.length; loop_index++)

{

int old = loop_index - 1;

int old_x = x_pos - x_incr;

int old_y = ylist[old];

int new_x = x_pos;

int new_y = ylist[loop_index];

g2d.draw(new Line2D.Double(old_x, old_y, new_x, new_y));

x_pos += x_incr;

};

graph_req = false;

}

}

};

//add panels to frame

contentPane.add(toppanel);

contentPane.add(bottompanel);

toppanel.setBounds(0,0,700,50);

bottompanel.setBounds(0,50,700,450);

toppanel.add(dateinput);

toppanel.add(startbutton);

//***error occurs when I add ActionListener

startbutton.addActionListener(this);

public void actionPerformed(ActionEvent event)

{

if(event.getSource() == startbutton)

{

graph_req = true;

ylist[0] = 300;

ylist[1] = 200;

ylist[2] = 350;

ylist[3] = 170;

ylist[4] = 400;

ylist[5] = 220;

ylist[6] = 305;

ylist[7] = 145;

ylist[8] = 305;

ylist[9] = 300;

double db_incr = 550/ylist.length;

x_incr = (int) db_incr;

x_pos = 75 + x_incr;

for(int count_index = 0; count_index < ylist.length; count_index++)

{

if(ylist[count_index] > y_max)

{

y_max = ylist[count_index];

}

}

y_incr = y_max/ylist.length;

System.out.println(y_max);

System.out.println(y_incr);

repaint();

}

}

}

}

rayleighsounda at 2007-7-12 20:08:49 > top of Java-index,Desktop,Core GUI APIs...
# 3

init is a method, as is actionperformed

you have

init()

{

actionPerformed()

{

}

}

should be

init()

{

}

actionPerformed()

{

}

Michael_Dunna at 2007-7-12 20:08:49 > top of Java-index,Desktop,Core GUI APIs...
# 4
Well I won't make that (rather dumb) mistake again...Thanks much!
rayleighsounda at 2007-7-12 20:08:49 > top of Java-index,Desktop,Core GUI APIs...