Creating a GUI application on Apache NetBeans IDE 12.4 Java 16.0.1
"What is Swing controls?"
Java Swing controls is a part of Java Foundation Classes (JFC) that is used to create window-based applications.
Swing GUI components allow the designer to specify a uniform look and feel for a GUI application across all platforms, or to use each platform's custom look and feel.
Let's talk about Palette, as you can see in the figure that set of component tools are displayed by which of frame is designed.
The component tools which are used to create the frame is called Java GUI Toolkit.
The palette is composed of many graphical controls that are offered by Java Swing contains the tools that you can use to draw controls on your forms/ Windows/ frames. The area on the frame where GUI component are placed is called content pane.
As you can see in the above image, many controls listed on the swing control palette. Let's understand each section briefly, the role and purpose of some commonly used controls.
Basic graphical controls of Swing
Role and purposes of some basic graphical controls of Swing
- jFrame provides the basic attributes and behaviours of a window. A frame is a displayed as a separate window with its own title bar.
- jLabel allows un-editable text (i.e., is is the user control change) or icon to be displayed.
- jButton provides button capabilities. And action event is generated when button is pushed.
- jCheckBox provide checkbox. Checkboxes are used to allow a user select multiple choices. For example a student can select 5 subject out of 17 choices. Checkbox shows a X in box in front of it when selected.
- jList is a list of items from which A selection can be made. From a list, multiple elements can be selected.
- jComboBox provides a drop down list of items from which A selection can be made or new items can be added. It is a combination of textfield and a list
- jPanel is a supporting container that cannot be displayed on its own but must be added to another container. Fennel can be added to content pane of a frame. They help to organize the components in a GUI.
- jRadioButton provides radio buttons. Radio buttons are option buttons that can be turned on or off. The radio buttons provide mutually exclusive options.
To create a new GUI application project:
- Click file-> new project command. Alternately, you can click new project icon in the tool bar or press Control + shift + N.
- In the categories pane, select the Java with Maven node and in the projects pane, Java application and click next.
- Enter the desired name in the project name field and specify the project location.
- Suggested: name the project as DataForm.
- Then click on the finish button.
- After that it now the NetBeans IDE will create the project folder on your system in a destination location. This folder contains all of the project's associated files.
- Now you need to first add a frame window to your project where you can add desired components and functionality.
- For this, on the top left pane, under projects tab, right click name i.e., data form and select new. From the submenu, select JFrame Form.
- In the next dialog, specify the name of the new frame being added in the box next to Class name and click Finish.
Now in the design view of the frame, you can add Desire components from the swing controls under palette and work further.
After JFrame form created click on the design view of the window.
Now simply, draw Label controls on your frame by dragging it from Swing controls section of the palette.
Change its name as you can see in the above image.
Drag and drop all the controls from swing controls in your frame, and then set text property for these label As given below:
Adjust all the controls in your frame and then add a text area to your frame and give it a size as per the image.
After adjusting all your frame, the design window is look like the below:
String text1 = jTextField1.getText();
String text2 = jTextField2.getText();
String text3 = jTextField3.getText();
String text4 = jTextField4.getText();
String text5 = jTextField5.getText();
jTextArea1.append("Student Details\n");
jTextArea1.append("Name: "+text1+ " "+text2+ " "+text3+"\n");
jTextArea1.append("Class: "+text4+" "+text5);
Now, come back to the design window. Now double click on the boundary of the clear button in the design space. The code editor window will get opened. In it, without touching the cursor, simply type the following code.
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextArea1.setText("");
Now save your work by pressing Cont + S and press F6 to run your project. You may also click the run icon on the toolbar.
See your application Run. After entering details in the text fields, click Generate button.
The details you have entered in the text fields are displayed in the text area.
For clearing all the text fields simply click the Clear button and all your details will reset back to blank.
Following given whole code of the project.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Lenovo
*/
public class data extends javax.swing.JFrame {
/**
* Creates new form data
*/
public data() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
label1 = new java.awt.Label();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
label1.setFont(new java.awt.Font("Eras Light ITC", 1, 18)); // NOI18N
label1.setText("Data Form");
jLabel1.setFont(new java.awt.Font("Bell MT", 0, 14)); // NOI18N
jLabel1.setText("Title Mr/Mrs");
jLabel2.setFont(new java.awt.Font("Bell MT", 0, 18)); // NOI18N
jLabel2.setText("First name");
jLabel3.setFont(new java.awt.Font("Bell MT", 0, 18)); // NOI18N
jLabel3.setText("Last name");
jLabel4.setFont(new java.awt.Font("Bell MT", 0, 18)); // NOI18N
jLabel4.setText("Class");
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextField2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextField3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextField4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel5.setFont(new java.awt.Font("Bell MT", 0, 18)); // NOI18N
jLabel5.setText("Section");
jTextField5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton1.setText("Generate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton2.setText("Clear ");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(128, 128, 128)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
.addComponent(jLabel5)
.addGap(14, 14, 14)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(28, 28, 28)
.addComponent(jButton2)))
.addGap(17, 17, 17)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(17, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(148, 148, 148))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(175, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String text1 = jTextField1.getText();
String text2 = jTextField2.getText();
String text3 = jTextField3.getText();
String text4 = jTextField4.getText();
String text5 = jTextField5.getText();
jTextArea1.append("Student Details\n");
jTextArea1.append("Name: "+text1+ " "+text2+ " "+text3+"\n");
jTextArea1.append("Class: "+text4+" "+text5); // TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextArea1.setText("");
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new data().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private java.awt.Label label1;
// End of variables declaration
}



0 Comments
Have any query? Want any type of module?
Please comment it down and let me know.