Swing Components in Java: Part 1

Introduction

 
Today in this article I will show you the basic Swing components of the Java language, including how to handle these components and how to work with these components. In this article, we will cover the following components.
  • Frame
  • Buttons
  • Combo Box
  • CheckBox
  • Radio Button
  • Menu Bar
  • Open File Dialog Box
  • Save File Dialog Box
  • Password Field
  • Text Field
  • Text Area
  • Progress Bar
Start
  • First, select "File" >> "New project" then select Java application and then "OK".
  • Then right-click on your package and click "Add new item" then add a new Frame from there.
  •  Drag the Menu bar from the pallet and right-click on the file then click "Add" from the pallet then click menu item.
  •  Drag a combo box from the pallet on the frame and go to the properties of the component and edit its models property and write like I C#, Java, PHP and so on.
  • Drag two buttons from the pallet as in the diagram below.
  • Drag a text field and text area from the pallet.
  • Drag checkboxes and radio buttons from the pallet on the Frame.
  • Drag a File Chooser from the pallet on Frame.
  • Double-click on the component then its action performed event will be generated.
MenuBar Item Event
 

Menu Bar Item events

 

1. Open File Dialog

  1. jMenuItem1 event    
  2. private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)     
  3. {    
  4.  int returnVal = jFileChooser1.showOpenDialog(this);    
  5.  if (returnVal == JFileChooser.APPROVE_OPTION)     
  6.  {    
  7.   File file = jFileChooser1.getSelectedFile();    
  8.   String file_name = file.toString();    
  9.   try     
  10.   {    
  11.    FileReader FR = new FileReader(file_name);    
  12.    jTextArea1.read(FR, evt);    
  13.   } catch (Exception e) {}    
  14.  }    
  15. }     

2. Save File Dialog Box

  1. jMenuItem2 event    
  2. private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt)     
  3. {    
  4.  int returnVal = jFileChooser1.showSaveDialog(this);    
  5.  if (returnVal == JFileChooser.APPROVE_OPTION)     
  6.  {    
  7.   File file = jFileChooser1.getSelectedFile();    
  8.   String file_name = file.toString();    
  9.   try     
  10.   {    
  11.    FileWriter FW = new FileWriter(file_name, false);    
  12.    String AllText = jTextArea1.getText();    
  13.    FW.write(jTextArea1.getText());    
  14.    FW.write(AllText);    
  15.    FW.flush();    
  16.    FW.close();    
  17.   }     
  18.   catch (Exception e) {}    
  19.  }    
  20. }    

3. Combo Box

  1. jButton1 event.    
  2. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)     
  3. {    
  4.  String s = (String) jComboBox1.getSelectedItem();    
  5.  jTextField1.setText(s);    
  6. }     

4. CheckBox and Radio Buttons

  1. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)     
  2. {    
  3.  String s1 = "";    
  4.  String s2 = "";    
  5.  if (jCheckBox1.isSelected())     
  6.  {    
  7.   s1 = s1 + "" + jCheckBox1.getText() + "\n";    
  8.  }    
  9.  if (jCheckBox2.isSelected())     
  10.  {    
  11.   s1 = s1 + "" + jCheckBox2.getText() + "\n";    
  12.  }    
  13.  if (jCheckBox3.isSelected())     
  14.  {    
  15.   s1 = s1 + "" + jCheckBox3.getText() + "\n";    
  16.  }    
  17.  if (jCheckBox4.isSelected())     
  18.  {    
  19.   s1 = s1 + "" + jCheckBox4.getText() + "\n";    
  20.  }    
  21. }      
jTextArea1.setText(s1);//here assign the all check box which are selected to the text area.
 
//here we check same for the Radio buttons.
 
Note: In Java, if we select multiple radio buttons then it will also select, although its task is that only one item is selected from multiple radio buttons so for this we will use a Button Group and add all the radio buttons to it.
 
Add the following code to your code.
  1. jButton2 event.private void groupButton()     
  2. {    
  3.  ButtonGroup bg1 = new ButtonGroup();    
  4.  bg1.add(jRadioButton1);    
  5.  bg1.add(jRadioButton2);    
  6.  bg1.add(jRadioButton3);    
  7. }    
  8. if (jRadioButton1.isSelected())     
  9. {    
  10.  s2 = jRadioButton1.getText();    
  11. }    
  12. if (jRadioButton2.isSelected())     
  13. {    
  14.  s2 = jRadioButton2.getText();    
  15. }    
  16. if (jRadioButton3.isSelected())    
  17. {    
  18.  s2 = jRadioButton3.getText();    
  19. }    
  20. JOptionPane.showMessageDialog(null, s2);    
  21. }      

Progress Bar

Sample of JProgressBar
  1. private void progresbar()     
  2. {    
  3.  JFrame f = new JFrame("JProgressBar Sample");    
  4.  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  5.  Container content = f.getContentPane();    
  6.  JProgressBar progressBar = new JProgressBar();    
  7.  progressBar.setValue(31);    
  8.  progressBar.setStringPainted(true);    
  9.  Border border = BorderFactory.createTitledBorder("Reading...");    
  10.  progressBar.setBorder(border);    
  11.  content.add(progressBar, BorderLayout.NORTH);    
  12.  f.setSize(300100);    
  13.  f.setVisible(true);    
  14. }     

Password Field

Password Field
  1. private void passwordfield()     
  2. {    
  3.  final JFrame frame = new JFrame("JPassword Usage Demo");    
  4.  JLabel jlbPassword = new JLabel("Enter the password: ");    
  5.  JPasswordField jpwName = new JPasswordField(10);    
  6.  jpwName.setEchoChar('*');    
  7.  jpwName.addActionListener(new ActionListener()     
  8.  {    
  9.   public void actionPerformed(ActionEvent e)     
  10.   {    
  11.    JPasswordField input = (JPasswordField) e.getSource();    
  12.    char[] password = input.getPassword();    
  13.    if (isPasswordCorrect(password)) {    
  14.     JOptionPane.showMessageDialog(frame, "Correct  password.");    
  15.    } else {    
  16.     JOptionPane.showMessageDialog(frame, "Sorry. Try again.""Error Message", JOptionPane.ERROR_MESSAGE);    
  17.    }    
  18.   }    
  19.  });    
  20.  JPanel jplContentPane = new JPanel(new BorderLayout());    
  21.  jplContentPane.setBorder(BorderFactory.createEmptyBorder(20202020));    
  22.  jplContentPane.add(jlbPassword, BorderLayout.WEST);    
  23.  jplContentPane.add(jpwName, BorderLayout.CENTER);    
  24.  frame.setContentPane(jplContentPane);    
  25.  frame.addWindowListener(new WindowAdapter() {    
  26.   public void windowClosing(WindowEvent e) {    
  27.    System.exit(0);    
  28.   }    
  29.  });    
  30.  frame.pack();    
  31.  frame.setVisible(true);    
  32. }    
  33. Function to check password is correct.private static boolean isPasswordCorrect(char[] inputPassword)     
  34. {    
  35.  char[] actualPassword =     
  36.  {    
  37.   'e',    
  38.   'h',    
  39.   't',    
  40.   'e',    
  41.   's',    
  42.   'h',    
  43.   'a',    
  44.   'm'    
  45.  };    
  46.  if (inputPassword.length != actualPassword.length)    
  47.   return false;    
  48.  for (int i = 0; i < inputPassword.length; i++)    
  49.   if (inputPassword[i] != actualPassword[i])    
  50.    return false;    
  51.  return true;    
  52. }     

Main Function   

  1. public static void main(String[] args)     
  2. {    
  3.     FP1 fp1=new FP1();            
  4.     fp1.progresbar();    
  5.     fp1.passwordfield();    
  6. }    


Similar Articles