Button problem, can't get the bubble to...

I can't get my button to delete the text in a text area.
My code:
package events;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Main extends JPanel
        implements MouseListener {
    JTextArea textArea;
    static final String NEWLINE = System.getProperty("line.separator");
    public static void main(String[] args) {      
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MouseEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new Main();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    public Main() {
        super(new GridLayout(0,1));
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 75));
        add(scrollPane);
        Button button = new Button("Clear");
        textArea.add(button);
        button.setVisible(true);
        button.setSize(100, 100);
        button.setLocation(100, 100);
        addMouseListener(this);
        setPreferredSize(new Dimension(450, 450));
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    public void buttonClick(WindowEvent e){ //THIS is where i'm having trouble
        textArea.setText("");
    void eventOutput(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                + e.getComponent().getClass().getName()
                + "." + NEWLINE);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    public void mousePressed(MouseEvent e) {
        eventOutput("Mouse pressed (# of clicks: "
                + e.getClickCount() + ")", e);
    public void mouseReleased(MouseEvent e) {
        eventOutput("Mouse released (# of clicks: "
                + e.getClickCount() + ")", e);
    public void mouseEntered(MouseEvent e) {
        eventOutput("Mouse entered", e);
    public void mouseExited(MouseEvent e) {
        eventOutput("Mouse exited", e);
    public void mouseClicked(MouseEvent e) {
        eventOutput("Mouse clicked (# of clicks: "
                + e.getClickCount() + ")", e);
I'm having trouble HERE
  public void buttonClick(WindowEvent e){
        textArea.setText("");
The button is declared here
Button button = new Button("Clear"); //Name
        textArea.add(button); //Adding the button to text area
        button.setVisible(true); //Needs to be visible
        button.setSize(100, 100); //Square
        button.setLocation(100, 100); //Middle of TextArea-----------------------------------------------------------
All help appreciated!!

yawmark wrote:
Isn't that declaring the function for the button? Not really. You'll need to have an object listen for button events.
[http://java.sun.com/docs/books/tutorial/uiswing/events/]
Also:
1. Don't mix AWT components with Swing components.
2. Please post Swing questions in the Swing forums.
~I agree with yawmark on this. It even states in the API that Swing and AWT components should NEVER be used together. Swing is JVM generated, so it will not work with the OS generated AWT well.

Similar Messages

Maybe you are looking for