Jcombobox渲染实例
来源:互联网 发布:笑脸雾化器口感数据 编辑:程序博客网 时间:2024/06/13 08:21
首先写个渲染类:
package goodutils.framworks;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
public class JcomboboxListcellRender implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if(value instanceof JPanel){
if(isSelected){
((JPanel)value).setBackground(Color.LIGHT_GRAY);
}else {
((JPanel)value).setBackground(Color.white);
}
return (JPanel)value;
}
return null;
}
}
然后写个jpanel类:主要的目的是封装jcombox中的对象;便于管理
package goodutils.framworks;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
//下拉框中的对象
public class JComboboxItemJpanel extends JPanel {
private JLabel statuName;
private JLabel stateIcon;
public JComboboxItemJpanel(String statuname,String imagepath) {
setLayout(new FlowLayout());
statuName=new JLabel(statuname);
stateIcon=new JLabel();
stateIcon.setIcon(getImageIcon(imagepath));
add(getStatuName());
add(getStateIcon());
}
//导入图片
public ImageIcon getImageIcon(String imagepath){
ImageIcon image= new ImageIcon(getClass().getResource(imagepath));
return image;
}
public JLabel getStatuName() {
if(statuName==null){
statuName=new JLabel("状态");
}
return statuName;
}
public void setStatuName(JLabel statuName) {
this.statuName = statuName;
}
public JLabel getStateIcon() {
if(stateIcon==null){
stateIcon=new JLabel("图标");
}
return stateIcon;
}
public void setStateIcon(JLabel stateIcon) {
this.stateIcon = stateIcon;
}
}
调用的时候写法如下:
comboBox_status = new JComboBox(new JPanel[]{
new JComboboxItemJpanel("在线","/image/logo.gif"),
new JComboboxItemJpanel("隐身","/image/logo.gif"),
new JComboboxItemJpanel("离开","/image/logo.gif"),
new JComboboxItemJpanel("忙碌","/image/logo.gif")
});
comboBox_status.setPreferredSize(new Dimension(60, 30));
comboBox_status.setRenderer(new JcomboboxListcellRender());
panel_5.add(comboBox_status);
注意红色的部分必须加上,这句话的目的是给jcombobox添加渲染器,如果不加的话,对象打印的是他的tostring方法:
- Jcombobox渲染实例
- JComboBox 渲染(2012.09.29)
- JComboBox 组合框条目渲染(JComboBox添加对象选项)
- JComboBox 组合框条目渲染(JComboBox添加对象选项)
- JComboBox
- JcomboBox
- JCombobox
- java教程:JComboBox(选择框)组件使用实例|方法
- meteor 渲染template实例
- 实例渲染Instanced Rendering
- Spring渲染Velocity模版实例
- jComboBox级联
- Swing-JComboBox
- JComboBox,JList
- JCombobox使用方法
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
- ExtJs之Grid渲染function实例暗记
- osg入门系列-渲染状态实例
- 防止因为自动缓存,导致flash调用http请求返回304的问题
- DM机制
- 因计算机磁盘硬件配置问题windows无法启动及HP系列机器不能使用GHOST的解决方法
- 保存一篇关于Vi的好文章
- jquery easyui tabs的刷新问题
- Jcombobox渲染实例
- poj 3752字母旋转游戏
- 限制URLConnection抓取本地Caches数据
- Wireshark图解教程
- C# .Net Winform窗口特效处理
- CRC16算法
- *nix系统下验证Qt 4安装正确与否的方法和步骤
- jQuery文件使用google服务器的,以减少服务器的压力
- 页面正常,mysql数据库乱码问题