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

e791. 为JSpinner定制编辑器的更多相关文章

  1. Atom 编辑器试用

    简介 它号称"21世纪可黑客的文本编辑器".GitHub支持并开源,并支持跨平台.和brackets编辑器一样基于浏览器开发,意味着你可以使用less(包含css)来定制编辑器界面 ...

  2. Python 开发的 IDE 和代码编辑器,你选择的哪一个?

    为了方便,我会分两个大类去说明,一类是适用于软件开发的比较通用的编辑器或 IDE ,我们可以通过插件等形式支持 Python 的开发,另一个是专注于 Python 开发的编辑器或 IDE . 不过在此 ...

  3. Html引入百度富文本编辑器ueditor

    在日常工作用,肯定有用到富文本编辑器的时候,富文本编辑器功能强大使用方便,我用的是百度富文本编辑器,首先需要下载好百度编辑器的demo, 然后创建ueditor.html文件,引入百度编辑器,然后在h ...

  4. Html引入百度富文本编辑器ueditor及自定义工具栏

    在日常工作用,肯定有用到富文本编辑器的时候,富文本编辑器功能强大使用方便,我用的是百度富文本编辑器,首先需要下载好百度编辑器的demo, 然后创建ueditor.html文件,引入百度编辑器,然后在h ...

  5. notepad++与vivado关联

    notepad++与vivado关联 打开vivado软件,选择菜单栏“Tools——>Options…”,在弹出的对话框中,选择General选项卡,如图1所示. 图1  选择General选 ...

  6. 【转载】UEditor前端配置项说明

    UEditor 的配置项分为两类:前端配置项 和 后端配置项 后端配置项具体看这个文档L:后端配置项说明 本文档介绍如何通过设置前端配置项,定制编辑器的特性,配置方法主要通过修改ueditor.con ...

  7. 关于Eclipse Modeling Framework 实现模型驱动开发,第一部分

    ======================================EMF第二篇文章========================= 用 Eclipse Modeling Framework ...

  8. 重拾《 两周自制脚本语言 》- Eclipse插件实现语法高亮

    源码库: program-in-chinese/stone-editor-eclipse 参考: FAQ How do I write an editor for my own language? D ...

  9. Java 由浅入深GUI编程实战练习(三)

    一,項目介紹 1.可以查看年,月,日等功能.能获取今天的日期,并且能够通过下拉年,月的列表. 2.当程序运行时,显示的时间是系统当前时间. 3.可以手动输入时间,确定后系统跳转到制定的时间. 4.提供 ...

随机推荐

  1. Linux下找不到so文件的解决办法

    http://www.cnblogs.com/xudong-bupt/p/3698294.html 如果使用自己手动生成的动态链接库.so文件,但是这个.so文件,没有加入库文件搜索路劲中,程序运行时 ...

  2. 每日英语:Making the Most of Your Lunch Hour

    More Americans are eating lunch at their desks or even forgoing it altogether. Is passing up a prope ...

  3. jdango 部署之nginx+uwsgi

    1,安装pip sudo apt-get install python-pip 2,利用 pip 安装 Django sudo pip install Django 3,检测Django是否安装成功 ...

  4. jquery的extend函数

    var extend = (function () { var isObjFunc = function (name) {//返回的是一个函数 var toString = Object.protot ...

  5. Python提取MD5

    使用Python的hashlib模块提取MD5,网上参考,觉得这个还不错,可以作为模块直接使用. # -*- coding: utf-8 -*- import hashlib import sys i ...

  6. Python3求英文文档中每个单词出现的次数并排序

    [本文出自天外归云的博客园] 题目要求: 1.统计英文文档中每个单词出现的次数. 2.统计结果先按次数降序排序,再按单词首字母降序排序. 3.需要考虑大文件的读取. 我的解法如下: import ch ...

  7. android之滑屏的实现

    ★理论知识 ●View在屏幕上显示出来要先经过measure(计算)和layout(布局).  ◆当控件的父元素正要放置该控件时调用,父元素会问子控件“你想要用多大的地方?”,然后传入两个参数widt ...

  8. ParseUrl

    #!/usr/bin/python # coding:utf-8 import re import urlparse # 解析url def ParseUrl(url): if not re.sear ...

  9. python各类项目模块记录

    看看下面这些项目,并试着用它们实现一些东西出来:• The Django Tutorial 试着用 Django Web Framework 创建一个 web 应用.• SciPy 如果你对科学,数学 ...

  10. JVM——代空间的划分

    首先看在JVM的堆中,按代的划分: Young:主要是用来存放新生的对象. Old:主要存放应用程序中生命周期长的内存对象. Permanent:是指内存的永久保存区域,主要存放Class和Meta的 ...