Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Layout Managers tutorials

  • A container uses a layout manager to determine both the location and size of the components within the container.

  • A container can be assigned one layout manager, which is done using the setLayout() method.

  • LayoutManager is an interface that all the layout managersí classes must implement.

  • You can create your own layout manager by writing a class that implements the methods of the LayoutManager interface or you can use one of the many layout managers of the AWT and Swing APIs, including the following:

 

Layout Manager

Description

java.awt.FlowLayout

FlowLayout lays out components in a left-to-right flow, with each component given its preferred size. A Panel has FlowLayout by

default.

java.awt.BorderLayout

BorderLayout divides a container into five regions, allowing one component to be added to each region. A Frame and the content pane of a JFrame have BorderLayout by default.

java.awt.GridLayout

GridLayout divides a container into a grid of rows and columns, with one component added to each region of the grid and each component having the same size.

java.awt.GridBagLayout

GridBagLayout divides a container into regions similar to

GridLayout, except that components do not need to be the same size.Components can span more than one row or column.

java.awt.CardLayout

In CardLayout each component added to the container is treated

as a card, with only one card being visible at a time similar to a deck of cards.

javax.swing.BoxLayout

BoxLayout allows components to be laid out vertically or horizontally. BoxLayout is similar to GridBagLayout, but it is generally easier to use.

 

 

FlowLayout

  • The java.awt.FlowLayout class represents a layout manager that aligns components in a left-to-right flow, such as words in a sentence.

  • Below is the simple program which demonstrate FlowLayout manager:

 

 

Example: Java program to demonstrate FlowLayout manager

 Sam Madam

//Program name FlowlayoutDemo.java

// Java program to demonstrate FlowLayout manager

import java.awt.*;

public class FlowlayoutDemo

{

public static void main(String[] args)

{

Frame f = new Frame("FlowLayout demo");

Button b1=new Button("Button1");

Button b2=new Button("Button2");

Button b3=new Button("Button3");

Button b4=new Button("Button4");

Checkbox c1=new Checkbox("Pick me", true);

Checkbox c2=new Checkbox("Pick me too", false);

f.setLayout(new FlowLayout());

f.add(b1);

f.add(b2);

f.add(b3);

f.add(c1);

f.add(c2);

f.add(new Label("Enter name here:"));

f.add(new TextField(20));

f.add(b4);

f.pack();

f.setVisible(true);

}

}

Output

 

Note: The pack() method sizes the Frame so all the components fit nicely, as you can see in the above output frame.

 

BorderLayout

  • BorderLayout class represents a BorderLayout manager, which divides a container into five different regions: north, south, east, west, and center.

  • Only one component can be added to a given region, and the size of the component is determined by the region it appears in.

  • Below is the simple program of BorderLayout manager:

 

 

Example: Java program to demonstrate borderlayout manager

 Sam Madam

// Program name BorderLayoutDemo.java

// Java program to demonstrate borderlayout manager

import java.awt.*;

public class BorderLayoutDemo

{

public static void main(String[] args)

{

Button north, south, east, west, center;

north = new Button("North");

south = new Button("South");

east = new Button("East");

west = new Button("West");

center = new Button("Center");

Frame fr=new Frame("BorderLayout Demo");

fr.add(north, BorderLayout.NORTH);

fr.add(south, BorderLayout.SOUTH);

fr.add(east, BorderLayout.EAST);

fr.add(west, BorderLayout.WEST);

fr.add(center, BorderLayout.CENTER);

fr.setSize(300, 250);

fr.setVisible(true);

}

}

Output

 

 

GridLayout

  • GridLayout class represents a layout manager that divides a container into a grid of rows and columns.

  • Below is the simple program for demonstrating gridlayout manager:

 

 

Example: Java program to demonstrate GridLayout manager

 Sam Madam

// Program name GridLayoutDemo.java

// Java program to demonstrate GridLayout manager

import java.awt.*;

public class GridLayoutDemo

{

public static void main(String[] args)

{

Frame fr=new Frame("GridLayout Demo");

Button b1,b2,b3,b4,b5,b6,b7,b8;

b1=new Button("One");

b2=new Button("Two");

b3=new Button("Three");

b4=new Button("Four");

b5=new Button("Five");

b6=new Button("Six");

b7=new Button("Seven");

b8=new Button("Eight");

fr.setLayout(new GridLayout(4, 4));

fr.add(b1);

fr.add(b2);

fr.add(b3);

fr.add(b4);

fr.add(b5);

fr.add(b6);

fr.add(b7);

fr.add(b8);

fr. setSize(300,200);

fr.setVisible(true);

}

}

Output

 

 

GridBagLayout

  • The GridBagLayout can be used to lay out components in a grid.

  • Unlike the GridLayout the the sizes of the grids do not need to be constant, and a component can occupy more or less than one row or column.

  • Below is the simple example of gridbag layout manager:

 

 

Example: Java program to demonstrate gridbag layout manager

 Sam Madam

//Program name GridBagLayoutDemo.java

// Java program to demonstrate gridbag layout manager

import java.awt.*;

public class GridBagLayoutDemo

{

private static final Insets insets = new Insets(0, 0, 0, 0);

 

public static void main(final String args[])

{

final Frame frame = new Frame("GridBagLayout");

frame.setLayout(new GridBagLayout());

Button button;

button = new Button("One");

addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Two");

addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Three");

addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Four");

addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Five");

addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Six");

addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

button = new Button("Seven");

addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

frame.setSize(500, 200);

frame.setVisible(true);

}

private static void addComponent(Container container, Component component, int gridx, int gridy,

int gridwidth, int gridheight, int anchor, int fill)

{

GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,anchor, fill, insets, 0, 0);

container.add(component, gbc);

}

}

Output

 

 

CardLayout

  • Each component added to the container is treated as a card, with only one card being visible at a time similar to a deck of cards.

 

Example: Java program to demonstrate CardLyout

 Sam Madam

//Program name CardLayoutDemo.java

//Java program to demonstrate CardLyout

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardLayoutDemo extends JFrame implements ActionListener{

CardLayout card;

Button b1,b2,b3;

Container c;

CardLayoutDemo()

{

c=getContentPane();

card=new CardLayout(40,30);

c.setLayout(card);

b1=new Button("One");

b2=new Button("Two");

b3=new Button("Three");

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

c.add("a",b1);

c.add("b",b2);

c.add("c",b3);

}

public void actionPerformed(ActionEvent e)

{

card.next(c);

}

public static void main(String[] args) {

CardLayoutDemo cl=new CardLayoutDemo();

cl.setSize(200,150);

cl.setVisible(true);

Frame f=new Frame("CardLayout Demo");

cl.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

Output

 

Now click on the button

 

 

 

BoxLayout

  • BoxLayout allows components to be laid out vertically or horizontally. BoxLayout is similar to GridBagLayout.

  • Below is the simple program which demonstrate box layout:

 

 

Example: Java program to demonstrate box layout manager

 Sam Madam

// Program name BoxLayoutDemo.java

//Java program to demonstrate box layout manager

import javax.swing.*;

public class BoxLayoutDemo

{

public static void main(String [] args)

{

Box box = new Box(BoxLayout.Y_AXIS);

box.add(new JButton("Button1"));

box.add(new JCheckBox("Check here."));

box.add(new JButton("Button2"));

box.add(new JLabel("Enter your name:"));

box.add(new JTextField());

JFrame f=new JFrame("BoxLayout Demo");

f.add(box);

f.setSize(300, 150);

f.setVisible(true);

}

}

Output

Pr.Pg border                                              Next Pg