Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


[java] Creating new Swing Classes

This topic is 6225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites
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.

import javax.swing.*;
import java.awt.event.*;

public class MyButton extends JButton implements MouseListener {

public MyButton() {
// 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...

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!