Java Swing component custom CheckBox example, swingcheckbox

Source: Internet
Author: User

Java Swing component custom CheckBox example, swingcheckbox

This example describes how to customize the CheckBox for Java Swing components. We will share this with you for your reference. The details are as follows:

Let's take a look at the running effect:

The Code is as follows:

package themedemo;import java.awt.BasicStroke;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Graphics2D;import java.awt.GridLayout;import java.awt.RenderingHints;import java.util.Map;import javax.swing.BorderFactory;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Painter;import javax.swing.SwingUtilities;import javax.swing.UIDefaults;import javax.swing.UIManager;import javax.swing.WindowConstants;public class CheckBoxSkinDemo {  public static void main(String[] args) {    SwingUtilities.invokeLater(new Runnable() {      public void run() {        for (UIManager.LookAndFeelInfo laf : UIManager            .getInstalledLookAndFeels()) {          if ("Nimbus".equals(laf.getName())) {            try {              UIManager.setLookAndFeel(laf.getClassName());            } catch (Exception e) {              e.printStackTrace();            }          }        }        for (Map.Entry<Object, Object> entry : UIManager            .getLookAndFeelDefaults().entrySet()) {          if ((entry.getKey().toString()).startsWith("CheckBox")) {            System.out.println(entry.getKey() + " = "                + entry.getValue());          }        }        JFrame frame = new JFrame("www.jb51.net - CheckBox Skining Demo");        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        frame.getContentPane().setLayout(new BorderLayout());        JPanel panel = new JPanel(new GridLayout(0, 1, 20, 20));        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));        panel.setBackground(Color.darkGray);        UIDefaults checkBoxDefaults = new UIDefaults();        checkBoxDefaults.put("CheckBox.iconPainter",            new Painter<JComponent>() {              public void paint(Graphics2D g, JComponent c,                  int w, int h) {                g.setRenderingHint(                    RenderingHints.KEY_ANTIALIASING,                    RenderingHints.VALUE_ANTIALIAS_ON);                g.setStroke(new BasicStroke(2f));                g.setColor(Color.WHITE);                g.fillRect(1, 1, w - 4, h - 4);                g.setColor(Color.LIGHT_GRAY);                g.drawRect(1, 1, w - 4, h - 4);              }            });        checkBoxDefaults.put("CheckBox[Selected].iconPainter",            new Painter<JComponent>() {              public void paint(Graphics2D g, JComponent c,                  int w, int h) {                g.setRenderingHint(                    RenderingHints.KEY_ANTIALIASING,                    RenderingHints.VALUE_ANTIALIAS_ON);                g.setStroke(new BasicStroke(2f));                g.setColor(Color.WHITE);                g.fillRect(1, 1, w - 4, h - 4);                g.setColor(Color.DARK_GRAY);                g.drawPolyline(new int[] { 2, w / 3, w - 2 },                    new int[] { h / 2 - 1, h - 4, 0 }, 3);                g.setColor(Color.LIGHT_GRAY);                g.drawRect(1, 1, w - 4, h - 4);              }            });        JCheckBox checkBox = new JCheckBox("myCheckBox");        panel.add(checkBox);        checkBox.putClientProperty("Nimbus.Overrides", checkBoxDefaults);        checkBox.putClientProperty("Nimbus.Overrides.InheritDefaults",            false);        // Add a normal themed slider for comparison        JCheckBox normalCheckBox = new JCheckBox("normalCheckBox");        panel.add(normalCheckBox);        frame.getContentPane().add(panel, BorderLayout.CENTER);        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);      }    });  }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.