Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    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> <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> <pre><code>package test; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; public class MyButton extends JButton { private static final long serialVersionUID = 1L; private Color circleColor = Color.BLACK; public MyButton(String label) { super(label); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Dimension originalSize = super.getPreferredSize(); int gap = (int) (originalSize.height * 0.2); int x = originalSize.width + gap; int y = gap; int diameter = originalSize.height - (gap * 2); g.setColor(circleColor); g.fillOval(x, y, diameter, diameter); } @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); size.width += size.height; return size; } /*Test the button*/ public static void main(String[] args) { MyButton button = new MyButton("Hello, World!"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(button); frame.setVisible(true); } } </code></pre> <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> <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> <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> <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> <p>Understanding the anatomy of LookAndFeel is useful for writing controls: <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload