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. .NET Core2.0 环境下MVC模式的支付宝扫码支付接口-沙箱环境开发测试

    所有配置以及相关信息均可以从PC支付中获取 使用的生成二维码的组件名为QRCoder,该组件引用了一个第三方实现的System.Drawing类库,和支付宝官网类似 当面付SDK为Alipay.Aop ...

  2. (原创)c++11改进我们的模式之改进命令模式

    模式虽然精妙,却难完美,比如观察者模式中观察者生命周期的问题:比如访问者模式中循环依赖的问题等等:其它很多模式也存在这样那样的一些不足之处,如使用场景受限.实现复杂.不够简洁.不够通用等.但我觉得不足 ...

  3. (原创)C++11改进我们的程序之move和完美转发

    本次要讲的是右值引用相关的几个函数:std::move, std::forward和成员的emplace_back,通过这些函数我们可以避免不必要的拷贝,提高程序性能.move是将对象的状态或者所有权 ...

  4. 每日英语:KFC's Crisis in China Tests Ingenuity of Man Who Built Brand

    Sam Su for years ran one of the highest-flying foreign business operations in China. These days, he' ...

  5. tensorflow笔记5:tensorflow的基本运作,函数

    转载:https://blog.csdn.net/lenbow/article/details/52152766 1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段 ...

  6. PBR Metallic/Roughness工作流中Albedo与F0的计算方法

    首先简单回顾一下典型的纯金属与绝缘体的PBR属性: 纯金属: Albedo(diff): 0 F0(spec): >0.3 (or 0.5, epic/allegorithmic etc.) M ...

  7. MyBean-关于plugMap共享对象

    plugMap实现了对象的存储,使用setObject,和getObject来对对象进行存储   内部其实是一个列表,而且他会在释放的时候会情况尝试释放所有的对象,所以如果你共享的对象提前进行了释放, ...

  8. Eigen教程(5)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 块操作 块是matrix或array中的矩形子部分. 使用块 函数.block(), ...

  9. numpy 学习总结

    numpy 学习总结 作者:csj更新时间:01.09 email:59888745@qq.com 说明:因内容较多,会不断更新 xxx学习总结: 回主目录:2017 年学习记录和总结 #生成数组/使 ...

  10. [note]What I’ve learnt from working on startups

    What I've learnt from working on startups 从失败里学到了什么,六次创业失败. 企业家不是与生俱来的,也是靠学来的. 想的太多,做的太少.