KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA JTree list

Best books about JAVA programming:

JAVA JComboBox

This example shows how to extends JLabel and ListCellRenderer to disable serveral items in JComboBox. It includes 2 files: DisabledComboBoxExample.java and interface CanEnable.java



import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

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(300, 100);
setVisible(true);
}

public static void main (String args[]) {
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(1, 1, 1, 1));
}

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 (! ((CanEnable)value).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 (! ((CanEnable)tempItem).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();
}
}
}

 


 

 


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer