Disabled ComboBox Example : 组合框 « Swing组件 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » Swing组件 » 组合框屏幕截图 
Disabled ComboBox Example
Disabled ComboBox Example

// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html


/* (swing1.1) */


import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

/**
 @version 1.0 12/26/98
 */
public class DisabledComboBoxExample extends JFrame {

  public DisabledComboBoxExample() {
    super("Disabled Item ComboBox Example");

    Object[] items = new ComboItem("A")new ComboItem("B"),
        new ComboItem("1"false)new ComboItem("2"false),
        new ComboItem("abc")new ComboItem("def") };

    JComboBox combo = new JComboBox(items);
    combo.setRenderer(new ComboRenderer());
    combo.addActionListener(new ComboListener(combo));

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(combo);
    setSize(300100);
    setVisible(true);
  }

  public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch (Exception evt) {}
  
    DisabledComboBoxExample frame = new DisabledComboBoxExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  class ComboRenderer extends JLabel implements ListCellRenderer {

    public ComboRenderer() {
      setOpaque(true);
      setBorder(new EmptyBorder(1111));
    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
      if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
      else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
      }
      if (!((CanEnablevalue).isEnabled()) {
        setBackground(list.getBackground());
        setForeground(UIManager.getColor("Label.disabledForeground"));
      }
      setFont(list.getFont());
      setText((value == null"" : value.toString());
      return this;
    }
  }

  class ComboListener implements ActionListener {
    JComboBox combo;

    Object currentItem;

    ComboListener(JComboBox combo) {
      this.combo = combo;
      combo.setSelectedIndex(0);
      currentItem = combo.getSelectedItem();
    }

    public void actionPerformed(ActionEvent e) {
      Object tempItem = combo.getSelectedItem();
      if (!((CanEnabletempItem).isEnabled()) {
        combo.setSelectedItem(currentItem);
      else {
        currentItem = tempItem;
      }
    }
  }

  class ComboItem implements CanEnable {
    Object obj;

    boolean isEnable;

    ComboItem(Object obj, boolean isEnable) {
      this.obj = obj;
      this.isEnable = isEnable;
    }

    ComboItem(Object obj) {
      this(obj, true);
    }

    public boolean isEnabled() {
      return isEnable;
    }

    public void setEnabled(boolean isEnable) {
      this.isEnable = isEnable;
    }

    public String toString() {
      return obj.toString();
    }
  }
}

interface CanEnable {

  public void setEnabled(boolean isEnable);

  public boolean isEnabled();

}

           
       
Related examples in the same category
1. 图形界面组合表图形界面组合表
2. 组合框的颜色选择器(仅限Windows颜色选择)组合框的颜色选择器(仅限Windows颜色选择)
3. MSN类似的图形界面组合框MSN类似的图形界面组合框
4. 图形界面自动完成下拉框图形界面自动完成下拉框
5. 自动完成下拉框
6. 阶梯组合框范例阶梯组合框范例
7. 块组合框范例块组合框范例
8. 工具提示组合框范例工具提示组合框范例
9. 组合框菜单范例组合框菜单范例
10. JComboBox :加入自动完成, Binay查询2JComboBox :加入自动完成, Binay查询2
11. JComboBox: adding automatic completion-Catching user inputJComboBox: adding automatic completion-Catching user input
12. JComboBox: adding automatic completion-Adding automatic selectionJComboBox: adding automatic completion-Adding automatic selection
13. JComboBox: adding automatic completion-Adding automatic completionJComboBox: adding automatic completion-Adding automatic completion
14. JComboBox: adding automatic completion-Fixed Auto SelectionJComboBox: adding automatic completion-Fixed Auto Selection
15. JComboBox: adding automatic completion-Case insensitive matchingJComboBox: adding automatic completion-Case insensitive matching
16. JComboBox: adding automatic completion-Prefer the currently selected itemJComboBox: adding automatic completion-Prefer the currently selected item
17. JComboBox: adding automatic completion-Ignore input that does not matchJComboBox: adding automatic completion-Ignore input that does not match
18. JComboBox: adding automatic completion-Highlight complete textJComboBox: adding automatic completion-Highlight complete text
19. JComboBox: adding automatic completion-Popup the item listJComboBox: adding automatic completion-Popup the item list
20. JComboBox: adding automatic completion-Cursor positionJComboBox: adding automatic completion-Cursor position
21. JComboBox: adding automatic completion-Handling the initial selectionJComboBox: adding automatic completion-Handling the initial selection
22. JComboBox: adding automatic completion-Handling focus lossJComboBox: adding automatic completion-Handling focus loss
23. JComboBox: adding automatic completion-BackspaceJComboBox: adding automatic completion-Backspace
24. JComboBox: adding automatic completion-Backspace 2JComboBox: adding automatic completion-Backspace 2
25. JComboBox: adding automatic completion-Pressing backspace at the beginningJComboBox: adding automatic completion-Pressing backspace at the beginning
26. JComboBox: adding automatic completion-Maximum MatchJComboBox: adding automatic completion-Maximum Match
27. JComboBox: adding automatic completion-Non-strict matchingJComboBox: adding automatic completion-Non-strict matching
28. JComboBox: adding automatic completion-Non-strict matching 2JComboBox: adding automatic completion-Non-strict matching 2
29. JComboBox: adding automatic completion-Binary Lookup and PerformanceJComboBox: adding automatic completion-Binary Lookup and Performance
30. JComboBox: adding automatic completion-Binay Lookup 2JComboBox: adding automatic completion-Binay Lookup 2
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.