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); } });
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."