Java
  Home arrow Java arrow Page 3 - Creating Control Buttons with NetBeans IDE
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Creating Control Buttons with NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-05-17

    Table of Contents:
  • Creating Control Buttons with NetBeans IDE
  • The nucleus of JToggleButton
  • Working with JCheckBox
  • The nucleus of JCheckBox
  • Working with JRadioButton

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating Control Buttons with NetBeans IDE - Working with JCheckBox


    (Page 3 of 5 )

    In this section, we shall develop a small application with two check boxes, a button and a text box. The first check box stands for bold and the second for italic. The only difference between the above sample and this example is that we are using check boxes (instead of toggle buttons) and we wanted to apply the style only after clicking on the button.

    Currently, I named the project "SampleJavaApplication1" and the form (or JFrame) "Sample04."

    When the form (or JFrame) is created with "Sample04," the code behind it (only the constructor) would look something like the following:

      public class Sample04 extends javax.swing.JFrame {
       
    /** Creates new form Sample04 */
       
    public Sample04() {
         
    initComponents();
       
    }

    Make changes to the above code fragment in such a way that it looks similar to the following:

      public class Sample04 extends javax.swing.JFrame {
        /** Creates new form Sample04 */
        public Sample04() {
          initComponents();
          this.setSize(300,200);
       
    }

    In the above code, I explicitly defined the initial size of the frame. Before dropping all controls onto the form, set the layout to "null layout" (fig2), to ease our development for this article. Later in my upcoming articles, I shall introduce you to the other powerful and more efficient layouts. When you complete your form design, it should look something like the following (fig2).

    For convenience in writing understandable code, I named those controls as follows:

      chkBold
      chkItalic
      btnShow
      txtMsg

    I also gave a value to the Frame property "title" (using the property window), namely "Checkboxes: a Demo."

    The code behind JCheckBox

    Let us go through the code created behind JCheckBox. The entire code created by IDE is as follows:

        // Variables declaration - do not modify
       
    private javax.swing.JButton btnShow;
        private javax.swing.JCheckBox chkBold;
        private javax.swing.JCheckBox chkItalic;
        private javax.swing.JTextField txtMsg;

      private void initComponents() {
          chkBold = new javax.swing.JCheckBox();
          chkItalic = new javax.swing.JCheckBox();
          btnShow = new javax.swing.JButton();
          txtMsg = new javax.swing.JTextField();

          getContentPane().setLayout(null);

      setDefaultCloseOperation(
            javax.swing.WindowConstants.EXIT_ON_CLOSE);
          setTitle("Checkboxes: a demo");
          chkBold.setText("Bold");
          getContentPane().add(chkBold);
          chkBold.setBounds(40, 10, 130, 23);

          chkItalic.setText("Italic");
          getContentPane().add(chkItalic);
          chkItalic.setBounds(40, 30, 130, 23);

          btnShow.setText("Show");
          btnShow.addActionListener(new
              java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent
    evt)
      {
              btnShowActionPerformed(evt);
            }
          });

          getContentPane().add(btnShow);
          btnShow.setBounds(40, 60, 130, 23);

          txtMsg.setText("This is some message");
          getContentPane().add(txtMsg);
          txtMsg.setBounds(60, 100, 240, 20);

          pack();
        }

    The explanation for the above code is very similar to that of the second section.

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys! another extension to the articles on NetBeans IDE. Enjoy and give me...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT