+ Reply to Thread
Results 1 to 2 of 2

Thread: create a custom button in java

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10

    create a custom button in java

    Is there a way to create a JButton with your own button graphic and not just with an image inside the button?

    If not, is there another way to create a custom button in java?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    15
    Here are the basic steps:

    Create a class that extends JComponent
    Call parent constructor super() in your constructors
    Make sure you class implements MouseListener
    Put this in the constructor:

    enableInputMethods(true);
    addMouseListener(this);
    Override these methods:

    public Dimension getPreferredSize()
    public Dimension getMinimumSize()
    public Dimension getMaximumSize()
    Override this method:

    public void paintComponent(Graphics g)
    The amount of space you have to work with when drawing your button is defined by getPreferredSize(), assuming getMinimumSize() and getMaximumSize() return the same value. I haven't experimented too much with this but, depending on the layout you use for your GUI your button could look completely different.

    And finally, the source code. In case I missed anything.

+ Reply to Thread

Bookmarks

Posting Permissions

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