Home arrow Java arrow Creating Control Buttons with NetBeans IDE
JAVA

Creating Control Buttons with NetBeans IDE


This article focuses on programming with the ToggleButtons, CheckBoxes and RadioButtons of JFC in Java using NetBeans IDE.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 25
May 17, 2006
TABLE OF CONTENTS:
  1. · Creating Control Buttons with NetBeans IDE
  2. · The nucleus of JToggleButton
  3. · Working with JCheckBox
  4. · The nucleus of JCheckBox
  5. · Working with JRadioButton

print this article
SEARCH DEVARTICLES

Creating Control Buttons with NetBeans IDE
(Page 1 of 5 )


A downloadable file for this article is available here.

I already introduced NetBeans IDE in my previous article on developing Java applications. We also managed to develop a "Hello World" application within the same article. If you are new to NetBeans IDE, I strongly suggest you go through that article first, before proceeding with this one.

The entire code for this article is freely available in the form of a zip file. That downloadable solution is developed using NetBeans 4.1 IDE. I didn't really test it in any other version.

Working with JToggleButton

In this section, we shall develop a small application with two toggle buttons and a text box. When the first button is toggled, it should switch all of the information available in the text box to bold. The second button should work similarly for italic. Currently, I named the project "SampleJavaApplication1" and the form (or JFrame) "Sample03."

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

  public class Sample03 extends javax.swing.JFrame {

 

    /** Creates new form Sample03 */
   
public Sample03() {
     
initComponents();
   
}

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

  public class Sample03 extends javax.swing.JFrame {

 

    /** Creates new form Sample03 */
   
public Sample03() {
     
initComponents();
     
this.setSize(300,200);
   
}

In the above code, I explicitly defined the initial size of the frame. Before dropping all controls on the form, set the layout to "null layout" (fig1), 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 (fig1).

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

  tbtnBold
  tbtnItalic
  txtMsg

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

The code behind JToggleButton

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

    // Variables declaration - do not modify
    private javax.swing.JToggleButton tbtnBold;
    private javax.swing.JToggleButton tbtnItalic;
    private javax.swing.JTextField txtMsg;

  private void initComponents() {
      tbtnBold = new javax.swing.JToggleButton();
      tbtnItalic = new javax.swing.JToggleButton();
      txtMsg = new javax.swing.JTextField();

      getContentPane().setLayout(null);

  setDefaultCloseOperation
        (javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setTitle("ToggleButtons: a Demo");
      tbtnBold.setText("Bold");
      tbtnBold.addActionListener(new
          java.awt.event.ActionListener()
  {
        public void actionPerformed(
            java.awt.event.ActionEvent evt)
  {
          tbtnBoldActionPerformed(evt);
       
}
      });

      getContentPane().add(tbtnBold);
      tbtnBold.setBounds(40, 20, 60, 23);

      tbtnItalic.setText("Italic");
      tbtnItalic.addActionListener(new
  java.awt.event.ActionListener() {
        public void actionPerformed(
            java.awt.event.ActionEvent evt)
  {
          tbtnItalicActionPerformed(evt);
        }
      });

      getContentPane().add(tbtnItalic);
      tbtnItalic.setBounds(110, 20, 60, 23);

      txtMsg.setText("This is some Info");
      getContentPane().add(txtMsg);
      txtMsg.setBounds(40, 60, 220, 20);

      pack();
  }

According to the above code, initially we create the references of the controls. The "initcomponents" would really create the objects to those references and set the layout of the content pane to "null layout." After that we work with toggle buttons by adding "eventing" to them and attaching them to the content pane. Finally we add the textbox.

I already explained the above type of code in detail in my previous article. If you are unfamiliar with the above code, please refer to my previous article, "Developing Controls in NetBeans IDE."


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials