Results 1 to 3 of 3
  1. #1
    gavo87 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default JButton and Command Pattern help

    Hi,

    I am making a GUI which implements a command pattern that will output a simple message saying "hello". I have a class Client within a package gmit. I have a button called connect which does the following:
    class connect implements ActionListener{

    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    Command command = (Command)e.getSource();
    command.run();

    }

    }

    every time i press the button i get the following error:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to gmit.Command
    at gmit.Client$connect.actionPerformed(Client.java:57 ).

    could someone please help me...
    Last edited by gavo87; 01-12-2011 at 05:57 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error is telling you what you're doing wrong. e.getSource returns the JButton object, and you can't use this as a Command object (whatever that is). You'll have to find another way to get the reference to your Command object, and we can't help you with this given the information presented so far. If you need more help, you'll need to give us more information including code with code tags please.
    Last edited by Fubarable; 01-12-2011 at 05:20 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 05:13 AM
  2. Better way of Command pattern implementation
    By Galian in forum Advanced Java
    Replies: 4
    Last Post: 11-06-2010, 07:34 PM
  3. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM
  4. Command design pattern
    By hannes in forum AWT / Swing
    Replies: 10
    Last Post: 01-28-2010, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •