下面列出了javax.swing.AbstractButton#addItemListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Bind an abstract button (usually checkbox) to a SearchPattern option.
*
* @param option Option whose value the button should represent.
* @param button Button to control and display the option.
*/
public void bind(@NonNull final Option option,
@NonNull final AbstractButton button) {
Parameters.notNull("option", option); //NOI18N
Parameters.notNull("button", button); //NOI18N
if (bindings.containsKey(option)) {
throw new IllegalStateException(
"Already binded with option " + option); // NOI18N
}
bindings.put(option, button);
button.setSelected(getOption(option));
button.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
setOption(option, button.isSelected());
}
});
}
public static void addModalTooltip(AbstractButton button, String on, String off)
{
button.setToolTipText(button.isSelected() ? on : off);
button.addItemListener(l -> button.setToolTipText(button.isSelected() ? on : off));
}
private static void configure(AbstractButton button) {
RolloverButtonEventListener l = new RolloverButtonEventListener();
button.addMouseListener(l);
button.addItemListener(l);
if (button.getAction() != null) {
if (button.getIcon() != null) {
button.putClientProperty("hideActionText", Boolean.TRUE);
}
Object largeIcon = button.getAction().getValue("_largeIcon");
if (largeIcon instanceof Icon) {
button.setIcon((Icon) largeIcon);
}
}
Icon icon = button.getIcon();
int minWidth = BUTTON_MIN_SIZE;
int minHeight = BUTTON_MIN_SIZE;
if (icon != null) {
button.setText(null);
minWidth = Math.max(icon.getIconWidth(), BUTTON_MIN_SIZE);
minHeight = Math.max(icon.getIconHeight(), BUTTON_MIN_SIZE);
if (icon instanceof ImageIcon) {
button.setRolloverIcon(createRolloverIcon((ImageIcon) icon));
}
} else {
button.setText("[?]");
}
final int space = 3;
Dimension prefSize = new Dimension(minWidth + space, minHeight + space);
Dimension minSize = new Dimension(minWidth, minHeight);
Dimension maxSize = new Dimension(minWidth + space, minHeight + space);
button.setPreferredSize(prefSize);
button.setMaximumSize(maxSize);
button.setMinimumSize(minSize);
}
/**
*
* @param precludedNames keys that will not be considered for filters
* @param elements elements that will be considered for filters
* @param maxFactor controls the threshold for filter creation
* @param buttonSupplier provides toolkit controls for the filters
* @param paintFunction provides a way to individually color the control buttons
*/
private AttributeFilters(Collection<String> precludedNames, Set<? extends Attributed> elements,
double maxFactor,
Supplier<AbstractButton> buttonSupplier, Function<String, Paint> paintFunction) {
// count up the unique attribute values (skipping the 'precluded names' we know we don't want)
for (Attributed element : elements) {
Map<String, String> attributeMap = new HashMap<>(element.getAttributeMap());
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
if (!precludedNames.contains(entry.getKey())) {
multiset.add(entry.getValue());
}
}
}
if (maxFactor == 0) {
maxFactor = .01;
}
double threshold = Math.max(2, elements.size() * maxFactor);
// accept the values with cardinality above the max of 2 and maxFactor times the of the number elements.
multiset.removeIf(s -> multiset.count(s) < threshold);
// create a button for every element that was retained
multiset.elementSet();
for (String key : multiset.elementSet()) {
AbstractButton button = buttonSupplier.get();
button.setForeground((Color) paintFunction.apply(key));
button.setText(key);
button.addItemListener(item -> {
if (item.getStateChange() == ItemEvent.SELECTED) {
selectedTexts.add(button.getText());
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
this.selectedTexts, ItemEvent.SELECTED));
}
else if (item.getStateChange() == ItemEvent.DESELECTED) {
selectedTexts.remove(button.getText());
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
this.selectedTexts, ItemEvent.DESELECTED));
}
});
buttons.add(button);
}
}