Aug 19, 2014

How to Using JCheckBox of SWING (JAVA GUI)





This post is 'How to using JCheckBox'.

If You run SWING, can see below picture.



I made 1 JCheckBox.

And change the name.


'Variable' is variable name in Source.

'text' is check box name in GUI.

Go to 'Source'

serverSWING.java

  1. public class serverSWING implements ActionListener,ItemListener{
  2.     private JFrame frame;
  3.     private JTextField portNum;
  4.     private JButton btnServerOpen,btnClear,btnSend;
  5.     public static JEditorPane commText;
  6.     ServerSocket ss;
  7.     Socket socket;
  8.     InetAddress ia;
  9.     Thread server;
  10.     public static JButton btnServerClose;
  11.     public static boolean disconnected=true;
  12.     private JEditorPane sendText;
  13.     public static boolean echoEnable=false;
  14.     private JCheckBox checkEcho;
  15.     private JScrollPane scrollPane;
  16. }
Line 1 : 'ItemListener' is for JCheckBox listener.

Line 14 : declared JCheckBox.

This is initailize.

  1. checkEcho = new JCheckBox("Echo Mode");
  2. checkEcho.setBounds(49337511923);
  3. frame.getContentPane().add(checkEcho);
  4. checkEcho.addItemListener(this);
Line 4 : registration to listener.

This is auto adding listener code.

  1.     @Override
  2.     public void itemStateChanged(ItemEvent e) {
  3.         if(e.getSource()==checkEcho){
  4.             if(ItemEvent.SELECTED==e.getStateChange())
  5.                 echoEnable=true;
  6.             else
  7.                 echoEnable=false;
  8.            
  9.             System.out.println("now echoEnable: "+echoEnable);
  10.             checkEcho();
  11.         }
  12.        
  13.     }
Line 3 : 'e.getSource()' is JCheckBox's name.

Line 4: 'ItemEvent.SELECTED' mean checked JCheckBox.

No comments:

Post a Comment