zoukankan      html  css  js  c++  java
  • e791. 为JSpinner定制编辑器

    This example replaces the default editor (a JFormattedTextField) in a spinner component with a custom editor. The custom editor is simply a panel that displays a color. The name of the color to display is stored in a SpinnerListModel.

        // Create a color spinner
        ColorSpinner spinner = new ColorSpinner(
            new String[]{"red", "green", "blue"});
        
        // Change the selected color
        spinner.setValue("blue");
        
        public class ColorSpinner extends JSpinner {
            public ColorSpinner(String[] colorNames) {
                super();
                setModel(new SpinnerListModel(colorNames));
                setEditor(new Editor(this));
            }
        
            public class Editor extends JPanel implements ChangeListener {
                int preferredWidth = 30;
                int preferredHeight = 16;
        
                Editor(JSpinner spinner) {
                    // Add the listener
                    spinner.addChangeListener(this);
        
                    // Set the preferred size
                    setPreferredSize(new Dimension(preferredWidth, preferredHeight));
        
                    // Display the current color
                    setColor((String)spinner.getValue());
                }
        
                // Handles changes to the value stored in the model
                public void stateChanged(ChangeEvent evt) {
                    JSpinner spinner = (JSpinner)evt.getSource();
        
                    // Get the new value
                    String value = (String)spinner.getValue();
        
                    // Update the displayed color
                    setColor(value);
                }
        
                // Updates the displayed color to 'colorName' which must be one
                // of the predefined colors in java.awt.Color.
                public void setColor(String colorName) {
                    try {
                        // Find the field and value of colorName
                        Field field = Class.forName("java.awt.Color").getField(colorName);
                        Color color = (Color)field.get(null);
        
                        // Display the color
                        setBackground(color);
                    } catch (Exception e) {
                    }
                }
            }
    
    Related Examples
  • 相关阅读:
    [GO] go使用etcd和watch方法进行实时的配置变更
    [GO]go context的deadline方法
    [GO]go使用contextCancel
    [GO]go使用etcd
    js控制复选框checkbox 只能单选
    JQuery.Ajax之错误调试帮助信息
    SQLServer2005创建定时作业任务
    JS/JQuery针对不同类型元素的操作(radio、select、checkbox)
    SQL Server跨库查询
    javax.net.ssl.SSLHandshakeException(Cas导入证书)
  • 原文地址:https://www.cnblogs.com/borter/p/9596252.html
Copyright © 2011-2022 走看看