AbstractButton: setForeground(Color fg) : AbstractButton « javax.swing « Java by API






AbstractButton: setForeground(Color fg)

  

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    ItemListener itemListener = new ItemListener() {
      public void itemStateChanged(ItemEvent itemEvent) {
        AbstractButton abstractButton = (AbstractButton)itemEvent.getSource();
        Color foreground = abstractButton.getForeground();
        Color background = abstractButton.getBackground();
        int state = itemEvent.getStateChange();
        if (state == ItemEvent.SELECTED) {
          abstractButton.setForeground(background);
          abstractButton.setBackground(foreground);
        }
      }
    };
    aCheckBox4.addItemListener(itemListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

   
    
  








Related examples in the same category

1.AbstractButton: addActionListener(ActionListener l)
2.AbstractButton: addChangeListener(ChangeListener l)
3.AbstractButton: addItemListener(ItemListener l)
4.AbstractButton: getActionCommand()
5.AbstractButton: setActionCommand(String actionCommand)
6.AbstractButton: setBackground(Color bg)
7.AbstractButton: setHorizontalAlignment(int alignment)
8.AbstractButton: setHorizontalTextPosition(int textPosition)
9.AbstractButton: setMnemonic(char mnemonic)