[java] Creating new Swing Classes

Started by
0 comments, last by mcd9 22 years, 9 months ago
Does anyone know of a good tutorial in creating your own swing components? I''ve had a bit of a play and can draw with the graphics stuff on it but I wanted to be able to tell when someone''s clicked on it (like a button lowers when clicked on) and if there are any general guidelines Thanks alot Ford! theres an infinate amount of monkeys outside wanting to talk to you about a script of Hamlet they created! www.mikeditum.co.uk
Ford! theres an infinate amount of monkeys outside wanting to talk to you about a script of Hamlet they created!www.mikeditum.co.uk
Advertisement
Hello, I''m not entirely sure if this is the best way to do it but: why don''t you just create a subclass of say a JButton and have that class implement the MouseListener Interface. So when the user clicks on your button it will fire a mouse event that you can handle.

Example:
  import javax.swing.*;import java.awt.event.*;public class MyButton extends JButton implements MouseListener {        public MyButton() {       addMouseListener(this);       // init    }    public void mouseClicked(MouseEvent e) {       // your component has been clicked, do something    }      public void mousePressed(MouseEvent e) {      // the mouse is being pressed in your component    }     public void mouseReleased(MouseEvent e) {      // mouse released    }    public void mouseEntered(MouseEvent e) {}    public void mouseExited(MouseEvent e) {}}  


I''ve used this method myself and it seems to work well for me, at least. However I haven''t used it on a JButton. (mostly JFrame and JPanel).

So, I hope this was at least a little helpful.
If anyone else has a better solution, I would be glad to see it.


Cheers, JP.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com

This topic is closed to new replies.

Advertisement