FlowLayout as Layout Manager in Java

This is a tutorial in which we will going to create a program that will have a FlowLayout as Layout Manager in Java. A FlowLayout as a layout manager provides a layout that is simple and used as default by the JPanel. It makes it every component seen according to its preferred size and arranges them in horizontal wrapping lines so that they will have spacing. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of flowLayout.java. 2. Import the following packages:
  1. import javax.swing.*; // used to access the JFrame, JRadioButton, ButtonGroup, and Container class
  2. import java.awt.*; // used to access the FlowLayout class
  3. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
3. In your class, extends it to JFrame and implement it with an ActionListener because we will use such event in the radio buttons. Initialize the following variables below:
  1. public class flowLayout extends JFrame implements ActionListener {
  2. JRadioButton rbLeft = new JRadioButton("Left");
  3. JRadioButton rbCenter = new JRadioButton("Center");
  4. JRadioButton rbRight = new JRadioButton("Right");
  5.  
  6. Container con = getContentPane();
  7. FlowLayout layout = new FlowLayout();
As you what you have seen the code above, we have 3 radio buttons labeled Left, Right, and Center button. We also initialize variable bg as ButtonGroup so that we can group the radio buttons. We also make variable con as a Container that will provide the pane as a container of the components. And to use the flow layout, we have variable layout declared as a FlowLayout class. 4. Now, we will make a constructor named flowLayout. Inside of this, we add the radio button components by using the add method of our container variable.
  1. con.add(rbLeft);
  2. con.add(rbCenter);
  3. con.add(rbRight);
To group the radio buttons, we will use the add method of the ButtonGroup variable.
  1. bg.add(rbLeft);
  2. bg.add(rbRight);
  3. bg.add(rbCenter);
We will add then the action listener of the radio buttons by using the addActionListener method and the parameter this inside the radio button. The this keyword indicates that it is in the current file.
  1. rbLeft.addActionListener(this);
  2. rbRight.addActionListener(this);
  3. rbCenter.addActionListener(this);
Then use the setLayout method of the container and put the variable layout as a FlowLayout inside of it.
  1. con.setLayout(layout);
  2. setDefaultCloseOperation(EXIT_ON_CLOSE);
5. Now, we will create a listener and the event for the radio buttons. Have this code below:
  1. public void actionPerformed(ActionEvent e) {
  2. Object source = e.getSource();
  3. if(source == rbLeft)
  4. {
  5. layout.setAlignment(FlowLayout.LEFT);
  6. }
  7. else if (source == rbRight){
  8. layout.setAlignment(FlowLayout.RIGHT);
  9.  
  10. }
  11. else{
  12. layout.setAlignment(FlowLayout.CENTER);
  13. }
  14. layout.layoutContainer(con);
  15. }
In every radio buttons, their is a corresponding alignment for the FlowLayoutusing the setAlignment method. The layoutContainer method indicates that the layout used in the container is the FlowLayour. 6. Now, in your main, instantiate the class name or the constructor inside and make it named as frame as a variable. Lastly set the size, visibility, and title of the frame.
  1. public static void main(String[] args) {
  2. flowLayout frame = new flowLayout();
  3. frame.setSize(300, 100);
  4. frame.setVisible(true);
  5. frame.setTitle("FlowLayout as Layout Manager");
  6. }
Output: outputoutputoutput Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access the JFrame, JRadioButton, ButtonGroup, and Container class
  2. import java.awt.*; // used to access the FlowLayout class
  3. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  4.  
  5.  
  6. public class flowLayout extends JFrame implements ActionListener {
  7. JRadioButton rbLeft = new JRadioButton("Left");
  8. JRadioButton rbCenter = new JRadioButton("Center");
  9. JRadioButton rbRight = new JRadioButton("Right Button");
  10.  
  11. Container con = getContentPane();
  12. FlowLayout layout = new FlowLayout();
  13.  
  14.  
  15. public flowLayout() {
  16.  
  17. con.add(rbLeft);
  18. con.add(rbCenter);
  19. con.add(rbRight);
  20.  
  21.  
  22. bg.add(rbLeft);
  23. bg.add(rbRight);
  24. bg.add(rbCenter);
  25.  
  26. rbLeft.addActionListener(this);
  27. rbRight.addActionListener(this);
  28. rbCenter.addActionListener(this);
  29.  
  30.  
  31. con.setLayout(layout);
  32. setDefaultCloseOperation(EXIT_ON_CLOSE);
  33. }
  34. public void actionPerformed(ActionEvent e) {
  35. Object source = e.getSource();
  36. if(source == rbLeft)
  37. {
  38. layout.setAlignment(FlowLayout.LEFT);
  39. }
  40. else if (source == rbRight){
  41. layout.setAlignment(FlowLayout.RIGHT);
  42.  
  43. }
  44. else{
  45. layout.setAlignment(FlowLayout.CENTER);
  46. }
  47. layout.layoutContainer(con);
  48. }
  49.  
  50. public static void main(String[] args) {
  51. flowLayout frame = new flowLayout();
  52. frame.setSize(300, 100);
  53. frame.setVisible(true);
  54. frame.setTitle("FlowLayout as Layout Manager");
  55. }
  56. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment