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. mysql 配置路径

    1)关于配置文件路径 有时候,我发现虽然尝试修改了配置文件的一些变量,但是并没有生效.后来才发现原来是因为修改的文件并非MySQL服务器读取的配置文件. 如果不清楚MySQL当前使用的配置文件路径,可 ...

  2. 【ARM】2410裸机系列-按键查询式控制led

    开发环境   硬件平台:FS2410 主机:Ubuntu 12.04 LTS LED灯原理图 按键原理图 按键的接线资源 KSCAN0 -> GPE11    KSCAN1 -> GPG6 ...

  3. 【嵌入式】使用Cross Toolchain构建交叉工具链

    Preface 前面编译linux内核的时候,用各种cross版本都不行啊,真是纠结,于是就想着自己也要会编译交叉工具的方法,然后各种尝试,各种问题啊,最后还是没解决(还有其它事情),步骤我都走熟了, ...

  4. Notepad++中支持Markdown

    最近在学习Markdown语言的使用,很想在XP主机上使用Markdown的离线编辑器,但MarkdownPad.作业部分的离线客户端都不能再XP上运行,需要.Net 4.5 以上版本,可惜一台老主机 ...

  5. #define SIG_DFL ((void(*)(int))0)

    在linux内的信号处理函数中,有#define SIG_DFL ((void(*)(int))0)和#define SIG_IGN ((void(*)(int))1)两个宏定义.要理解这两个宏定义, ...

  6. django模型创建

    定义模型 模型,属性,表,字段之间的关系 一个模型类在数据库中对应一张表,在模型类中定义的属性,对应该模型对照表中的一个字段 定义属性:见下文 创建模型类 元选项 在模型类中定义Meta类,用于设置元 ...

  7. Python操作SQLAlchemy之连表操作

    多对一连表操作 首先有两个知识点: 改变数据输出的方式:可以在表的类中定义一个特殊成员:__repr__,return一个自定义的由字符串拼接的数据连接方式. 数据库中表关系之间除了MySQL中标准的 ...

  8. 通用后台管理系统(ExtJS 4.2 + Spring MVC 3.2 + Hibernate)

    通用后台管理系统(ExtJS 4.2 +Spring MVC 3.2 + Hibernate) 开发语言JAVA 成品成品 前端技术extjs 数据库mysql,sql server,oracle 系 ...

  9. red hat enterprise linux 6

    i386(32位):http://rhel.ieesee.net/uingei/rhel-server-6.3-i386-dvd.iso迅雷快传:http://kuai.xunlei.com/d/PF ...

  10. 慢慢来,比较快。四年Android,共勉。

    四年阿,我特么以为我才17岁,不想已经27了,操蛋阿.加班熬夜,我的黑眼圈,黑头泛滥.如果木有自拍神器,我是决计不会爆照的!! 第一年,学了七天android找上了第一份工作,后来接了个外包,是做智能 ...