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

<p>If not, is there another way to create a custom <code>JButton</code> in java?</p>

[{'answer_id': 2181, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 3, 'selected': False, 'text': "<p>I haven't done SWING development since my early CS classes but if it wasn't built in you could just inherit <code>javax.swing.AbstractButton</code> and create your own. Should be pretty simple to wire something together with their existing framework.</p>\n"}, {'answer_id': 2210, 'author': 'rjohnston', 'author_id': 246, 'author_profile': 'https://Stackoverflow.com/users/246', 'pm_score': 4, 'selected': False, 'text': '<p>You could always try the Synth look &amp; feel. You provide an xml file that acts as a sort of stylesheet, along with any images you want to use. The code might look like this:</p>\n\n<pre><code>try {\n    SynthLookAndFeel synth = new SynthLookAndFeel();\n    Class aClass = MainFrame.class;\n    InputStream stream = aClass.getResourceAsStream("\\\\default.xml");\n\n    if (stream == null) {\n        System.err.println("Missing configuration file");\n        System.exit(-1);                \n    }\n\n    synth.load(stream, aClass);\n\n    UIManager.setLookAndFeel(synth);\n} catch (ParseException pe) {\n    System.err.println("Bad configuration file");\n    pe.printStackTrace();\n    System.exit(-2);\n} catch (UnsupportedLookAndFeelException ulfe) {\n    System.err.println("Old JRE in use. Get a new one");\n    System.exit(-3);\n}\n</code></pre>\n\n<p>From there, go on and add your JButton like you normally would. The only change is that you use the setName(string) method to identify what the button should map to in the xml file.</p>\n\n<p>The xml file might look like this:</p>\n\n<pre><code>&lt;synth&gt;\n    &lt;style id="button"&gt;\n        &lt;font name="DIALOG" size="12" style="BOLD"/&gt;\n        &lt;state value="MOUSE_OVER"&gt;\n            &lt;imagePainter method="buttonBackground" path="dirt.png" sourceInsets="2 2 2 2"/&gt;\n            &lt;insets top="2" botton="2" right="2" left="2"/&gt;\n        &lt;/state&gt;\n        &lt;state value="ENABLED"&gt;\n            &lt;imagePainter method="buttonBackground" path="dirt.png" sourceInsets="2 2 2 2"/&gt;\n            &lt;insets top="2" botton="2" right="2" left="2"/&gt;\n        &lt;/state&gt;\n    &lt;/style&gt;\n    &lt;bind style="button" type="name" key="dirt"/&gt;\n&lt;/synth&gt;\n</code></pre>\n\n<p>The bind element there specifies what to map to (in this example, it will apply that styling to any buttons whose name property has been set to "dirt").</p>\n\n<p>And a couple of useful links:</p>\n\n<p><a href="http://javadesktop.org/articles/synth/" rel="noreferrer">http://javadesktop.org/articles/synth/</a></p>\n\n<p><a href="http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/synth.html" rel="noreferrer">http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/synth.html</a></p>\n'}, {'answer_id': 2225, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 5, 'selected': False, 'text': '<p>Yes, this is possible. One of the main pros for using Swing is the ease with which the abstract controls can be created and manipulates.</p>\n\n<p>Here is a quick and dirty way to extend the existing JButton class to draw a circle to the right of the text.</p>\n\n<pre><code>package test;\n\nimport java.awt.Color;\nimport java.awt.Container;\nimport java.awt.Dimension;\nimport java.awt.FlowLayout;\nimport java.awt.Graphics;\n\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\n\npublic class MyButton extends JButton {\n\n    private static final long serialVersionUID = 1L;\n\n    private Color circleColor = Color.BLACK;\n\n    public MyButton(String label) {\n        super(label);\n    }\n\n    @Override\n    protected void paintComponent(Graphics g) {\n        super.paintComponent(g);\n\n        Dimension originalSize = super.getPreferredSize();\n        int gap = (int) (originalSize.height * 0.2);\n        int x = originalSize.width + gap;\n        int y = gap;\n        int diameter = originalSize.height - (gap * 2);\n\n        g.setColor(circleColor);\n        g.fillOval(x, y, diameter, diameter);\n    }\n\n    @Override\n    public Dimension getPreferredSize() {\n        Dimension size = super.getPreferredSize();\n        size.width += size.height;\n        return size;\n    }\n\n    /*Test the button*/\n    public static void main(String[] args) {\n        MyButton button = new MyButton("Hello, World!");\n\n        JFrame frame = new JFrame();\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.setSize(400, 400);\n\n        Container contentPane = frame.getContentPane();\n        contentPane.setLayout(new FlowLayout());\n        contentPane.add(button);\n\n        frame.setVisible(true);\n    }\n\n}\n</code></pre>\n\n<p>Note that by overriding <strong>paintComponent</strong> that the contents of the button can be changed, but that the border is painted by the <strong>paintBorder</strong> method. The <strong>getPreferredSize</strong> method also needs to be managed in order to dynamically support changes to the content. Care needs to be taken when measuring font metrics and image dimensions.</p>\n\n<p>For creating a control that you can rely on, the above code is not the correct approach. Dimensions and colours are dynamic in Swing and are dependent on the look and feel being used. Even the default <em>Metal</em> look has changed across JRE versions. It would be better to implement <strong>AbstractButton</strong> and conform to the guidelines set out by the Swing API. A good starting point is to look at the <strong>javax.swing.LookAndFeel</strong> and <strong>javax.swing.UIManager</strong> classes.</p>\n\n<p><a href="http://docs.oracle.com/javase/8/docs/api/javax/swing/LookAndFeel.html" rel="noreferrer">http://docs.oracle.com/javase/8/docs/api/javax/swing/LookAndFeel.html</a></p>\n\n<p><a href="http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html" rel="noreferrer">http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html</a></p>\n\n<p>Understanding the anatomy of LookAndFeel is useful for writing controls:\n<a href="http://wayback.archive.org/web/20090309070901/http://java.sun.com/products/jfc/tsc/articles/sce/index.html" rel="noreferrer">Creating a Custom Look and Feel</a></p>\n'}, {'answer_id': 2245, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 8, 'selected': True, 'text': '<p>When I was first learning Java we had to make Yahtzee and I thought it would be cool to create custom Swing components and containers instead of just drawing everything on one <code>JPanel</code>.  The benefit of extending <code>Swing</code> components, of course, is to have the ability to add support for keyboard shortcuts and other accessibility features that you can\'t do just by having a <code>paint()</code> method print a pretty picture.  It may not be done the best way however, but it may be a good starting point for you.</p>\n\n<p>Edit 8/6 - If it wasn\'t apparent from the images, each Die is a button you can click. This will move it to the <code>DiceContainer</code> below.  Looking at the source code you can see that each Die button is drawn dynamically, based on its value.</p>\n\n<p><img src="https://i.stack.imgur.com/pgyQp.jpg" alt="alt text"><br>\n<img src="https://i.stack.imgur.com/jkYRd.jpg" alt="alt text"><br>\n<img src="https://i.stack.imgur.com/9BI34.jpg" alt="alt text"></p>\n\n<p>Here are the basic steps:</p>\n\n<ol>\n<li>Create a class that extends <code>JComponent</code></li>\n<li>Call parent constructor <code>super()</code> in your constructors</li>\n<li>Make sure you class implements <code>MouseListener</code></li>\n<li><p>Put this in the constructor:</p>\n\n<pre><code>enableInputMethods(true);   \naddMouseListener(this);\n</code></pre></li>\n<li><p>Override these methods:</p>\n\n<pre><code>public Dimension getPreferredSize()  \npublic Dimension getMinimumSize()  \npublic Dimension getMaximumSize()\n</code></pre></li>\n<li><p>Override this method:</p>\n\n<pre><code>public void paintComponent(Graphics g)\n</code></pre></li>\n</ol>\n\n<p>The amount of space you have to work with when drawing your button is defined by <code>getPreferredSize()</code>, assuming <code>getMinimumSize()</code> and <code>getMaximumSize()</code> 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.</p>\n\n<p>And finally, the <a href="https://github.com/kdeloach/labs/blob/master/java/yahtzee/src/Dice.java" rel="noreferrer">source code</a>.  In case I missed anything.  </p>\n'}, {'answer_id': 18261, 'author': 'AngelOfCake', 'author_id': 1732, 'author_profile': 'https://Stackoverflow.com/users/1732', 'pm_score': 3, 'selected': False, 'text': "<p>I'm probably going a million miles in the wrong direct (but i'm only young :P ). but couldn't you add the graphic to a panel and then a mouselistener to the graphic object so that when the user on the graphic your action is preformed.</p>\n"}]
['https://Stackoverflow.com/questions/2158', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/340/']