This exhaustive example illustrates JCheckBox in Java Swing for beginners. This also covers all core methods of the javax.swing.JCheckBox class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JCheckBoxExample extends JFrame
{
JPanel p;
JCheckBox c1,c2,c3,c4,c5,c6,c7,c8;
ImageIcon i1,i2,i3,i4,i5;
Font f;
JLabel label;
ButtonGroup bg=new ButtonGroup();
public JCheckBoxExample()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("JCheckBox Demo");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create image icons
i1=new ImageIcon("checkbox.png");
i2=new ImageIcon("checkboxselected.png");
i3=new ImageIcon("checkboxpressed.png");
i4=new ImageIcon("checkboxrollover.png");
i5=new ImageIcon("checkboxrolloverselected.png");
// Create a Font
f=new Font("Myriad Pro",Font.PLAIN,16);
// Create JLabel
label=new JLabel();
// Add the label to SOUTH (down) side
add(label,BorderLayout.SOUTH);
// Create JPanel p
p=new JPanel();
// Set the layout for JPanel
p.setLayout(new FlowLayout());
// First constructor
// Takes no parameters
c1=new JCheckBox();
// Create an action that produces beep sound
Action a=new javax.swing.text.DefaultEditorKit.BeepAction();
// Second constructor
// Takes Action object
c2=new JCheckBox(a);
// Third constructor
// Takes ImageIcon
// Icon appears instead
// of the check/uncheck shape
c3=new JCheckBox(i1);
// Create JCheckBox
// Takes ImageIcon which
// is the default icon
// (icon when not selected)
// and boolean value indicating
// selected/unselected
c4=new JCheckBox(i1,true);
// Fifth constructor
// Takes a String which appears
// on the JCheckBox
c5=new JCheckBox("Checkbox 5");
// Sixth constructor
// Takes String which appears on
// the JCheckBox, and a boolean
// indicating selected/unselected
c6=new JCheckBox("Checkbox 6",true);
// Seventh constructor
// Take a string that appears
// on the JCheckBox with an icon
c7=new JCheckBox("Checkbox 7",i1);
// Eighth constructor
// Takes a string that appears
// on JCheckBox with an ImageIcon
// (the default icon) and the boolean
// indicating selected/unselected
c8=new JCheckBox("Checkbox 8",i1,true);
// Set some action
c5.setAction(a);
// Set some text
c1.setText("Checkbox 1");
c2.setText("Checkbox 2");
c3.setText("Checkbox 3");
// Set some font
c1.setFont(f);
c2.setFont(f);
c3.setFont(f);
c4.setFont(f);
c5.setFont(f);
c6.setFont(f);
c7.setFont(f);
c8.setFont(f);
// Set selected icon
c7.setSelectedIcon(i2);
c8.setSelectedIcon(i2);
// Set focus painted false
// This removes the border
// around the text
// Default is true
c1.setFocusPainted(false);
c5.setFocusPainted(false);
c7.setFocusPainted(false);
c8.setFocusPainted(false);
// Set some cursor
// The cursor is default cursor
c7.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Set the icon text gap
// This gap is relative
// to the position of the
// text on the JCheckBox
// i.e. if the text is aligned
// vertically, the icon text gap
// is vertical, if horizontal, it
// is horizontal
// Default is 4
c8.setIconTextGap(10);
// Set vertical text
// position
c8.setVerticalTextPosition(SwingConstants.TOP);
// Set horizontal text position
c8.setHorizontalTextPosition(SwingConstants.CENTER);
// Set pressed icon
// This icon appears when the
// mouse/space bar/mnemonic
// is pressed on the JCheckBox
c8.setPressedIcon(i3);
// Set rollover icon
// This icon appears when
// the mouse is hover on the
// JCheckBox
c8.setRolloverIcon(i4);
// Set roll over selected icon
// This icon appears when the
// mouse is hover on the JCheckBox
// when it is selected
c8.setRolloverSelectedIcon(i5);
// Set mnemonic
// 56 is ascii value for 8
// c8.setMnemonic(56);
// Alternatively, if you
// don't know the ascii
// Call the setMnemonic(int)
// not setMnemonic(char)
// To work with mnemonic
// the user must press 8
// with Alt pressed
c8.setMnemonic((int)'8');
// Note: Another method called
// setMnenomic(char) is obsolete
// This method only handles char
// values between a and z or A and Z
// as per the documentation
// Changing displayed mnemonic
// index. This changes the underlined
// letter
c8.setDisplayedMnemonicIndex(2);
// Set border painted
c1.setBorderPainted(true);
// Adding to ButtonGroup
// Usually JRadioButtons are
// added to a ButtonGroup
// Adding to a button group
// will make only one of the
// JCheckBox in the ButtonGroup
// selectable, if another
// one is selected, the selected
// one is deselected
bg.add(c1);
bg.add(c2);
// Set border painted
// By default this is false
// for JCheckBox
c1.setBorderPainted(true);
c2.setBorderPainted(true);
c3.setBorderPainted(true);
c4.setBorderPainted(true);
c5.setBorderPainted(true);
// Set content area filled
// It is true by default
// If false, the background
// cannot be seen
c1.setContentAreaFilled(true);
c2.setContentAreaFilled(true);
// Set roll over enabled
c1.setRolloverEnabled(true);
// Disable c4
c4.setEnabled(false);
// Set the background
c1.setBackground(Color.DARK_GRAY);
// Set the foreground
c1.setForeground(Color.WHITE);
// Set margin
// The method setMargin() takes
// java.awt.Insets object which takes
// top,left,bottom.right respectively
// Default is 2,2,2,2
// This sets the top,left,bottom,right
// space between border and icon,text
c1.setMargin(new Insets(10,10,10,10));
// Set a custom border for c2
c2.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
// Set opaque to false
// By default it is true
// If not opaque, the background
// is not visible (if painted)
c2.setOpaque(false);
// Now set the background
// This is not visible
c2.setBackground(Color.RED);
// Set auto scrolls
// to true, the default value
// is false.
// When auto scrolls is true
// mouseDragged events will be generated
// even outside the bounds of the
// component to which MouseMotionListener
// is registered, provided that the mouse is
// dragged from the point within the component's
// bounds.
c1.setAutoscrolls(true);
// Set focusable false
// This component will not
// receive focus
c1.setFocusable(false);
// Handle mouseDragged
c1.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent me)
{
System.out.println("Mouse dragged.");
}
});
// Add all check boxes
// to the JPanel
p.add(c1);
p.add(c2);
p.add(c3);
p.add(c4);
p.add(c5);
p.add(c6);
p.add(c7);
p.add(c8);
// Set tool tip text
// Get all the components of p
// Should be done after adding
// JCheckBoxes to p
// since, before they are added
// no.of components is 0
Component[] c=p.getComponents();
for(int i=0;i<c.length;i++)
{
// ClassCastException not raised
// Since only JCheckBoxes are
// added to the panel
JCheckBox k=(JCheckBox)c[i];
// Set the tooltip text
// This appears mouse is hovered
// on k
k.setToolTipText("Checkbox "+(i+1));
}
// Add the JPanel
add(p);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new JCheckBoxExample();
}
});
}
}
In this way, we can simply create a JCheckBox in Java. Isn't that easy?
Also see using serialization on JCheckbox
No comments:
Post a Comment