Oops Co3
Oops Co3
Oops Co3
Button 4 Button 5
South
3. GridLayout: AGridLayout Manager places the components in arectangular grid.
Each component's position is identified by acolumn and row. Allthe cells in the grid
have the same size and width. The GridLayout class provides the following
Constructors.
GridLayout);
GridLayout(int rows, int columns);
GridLayout(int rows, int columns, int hor_gap, int ver_gap);
This creates a row*column grid layout with the specified horizontal and
not both. The
vertical gaps. In the constructor, either rows or columns can be zero, but
Thedefault
first constructor is equivalent toone row with any number of components.
gap between components is zero pixels.
components to be added to a
4. CardLayout: This layout manager allows multiple
container, but only one component is visibleat a time.
that
5. GridBagLayout: This layout manager is a powerful and flexible layout manager
allows components to be placed in cells of different sizes and shapes.
Button 1 Button 2
Button 3 Button 4
Button 5
Q9. Explain the steps for creating a simple swing GUlapplication with an example
1. Import the necessary Swing packages: To use Swing in aJava program, you
need to import the necessary packages, such as javax.swing and java.awt.
Create a JFrame object: The JFrame class represents the main window of a
2.
the constructor or by
Swing application. Youcan create a JFrame object using
extending the JFrame class.
3. Add components to the JFrame: Once you
have a JFrame object. you can
frame.add(panel):
ll set the properties of the frame
frame.setSize(300, 200):
frame. setDefaultCloseOperation(JFrame.EXIT ON CLOSE);
frame.setVisible(true):
ll add an event listener to the button
button.addActionListener(e -> {
JOptionPane.show Message Dialog( frame, "You clicked the button!");
);
Message
Youclicked the button!
OK
JLabel
Component
JL0st
JTable
Container JComponent
JComboBox
Panel
JSlider
Window
JMenu
Frame Applet
JButton
SwingExample()
JLabel( String s)
JLabel(lcon i )
JLabel(String s, lcon i. int horzontalAlignment )
Methods
String get Text() - Returns the text in the label
vOId setText) - Sets the text of label
void setiiorizontalAlignment(int alignment) - Al1gns the label's text in Xaxus
lcon getlcon() - Retums the mage the label displays
int getHonzontalAl1gnmentt)- Returns thc alignment of label's text in Xaxis
Example:
JLabel |l new JLatbel("Text" .
3. JTextField: The JTextField class is a text componcnt that
allows the c1ting of a
single line text
Constructors
JTextField() -Creates a new TextFicld
JTextFicld( String text) -TextField initial1sed with some text
JTextFicld( String text, int columns) - Initial1zed with some text and columns
JTextFicld(int columns) - Empty TextField with spec1fied number of columns
Example:
JTextFieldt! new JTextField"Text"):
4. JTextArea : The JTextAreaclass is a multi line region that displays text. Itallows the
cditing of multiple line text.
Constructors
JTextArea()
JTextArca(String s)
JTextArea(int row, int column)
JTextArea(String s, int row. int column)
Example:
JTextArea tl=new JTextArea("Text"):
(or)
JTextArea tl,t2; tl=new JTextArea(Text");
5. JCheckBox: The JCheckBox class is used to create a checkbox. It is
used to turn an
option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to
"off" or from "off" to "on".
Example:
JCheckBox cl=new JCheckBox(*Text");
6. JList: The object of the JList class represents a list of text items.
The list of text items
can be set up so that the user can choose one or more items from list of
items.
Example:
DefaultListModel || = new DefaultListModel<0:
Il.addElement("ltem1");
|1.addElement("Item2" );
11.addElement("Item3"):
11.addElement("Item4"):
JList list = new JList<>(1):
7. JPasswordField: The JPasswordFicld class is a text component specialised for
password entry. It allows the editing of a single line of text.
Example:
JPasswordField pwd = new JPasswordField():
pwd.setBounds( 100,50,80,30):
8. JRadioButton: The JRadioButton class is used to create a radio button. It is used to
choose one option from multiple options. It is widely used in exam systems or
quizzes. It should be added in ButtonGroup to select one radio button only.
Example:
ButtonGroup bg-new ButtonGroup():
JRadioButton rl=new JRadioButton("Male"):
JRadioButton r2-ncw JRadioButton("Fcmale");
bg.add(rl):
bg.add(r2):
9. JComboBox: The JComboBox class is used to show a popup menu of itcms. Item
selected bythe user is shownon the top of a menu.
Example:
String country[]={"India","Aus" "U.S.A","England","Ncwzcaland";:
JComboBox cb=new JComboBox(country);
cb.setBounds(50,50,90,20);
10. JTable: The JTable class is used to display data in tabular form. It is composed of
rows and columns.
Example:
String data|]|= "52 1" "Madhu""43400"}. "512", "Hari","54500").
509","Ganesh""70000"::
String column[]={"ID","NAME'" "SALARY"};
JTable j=new JTable(data,column);
jt.setBounds(30,40,200,300):
1. JPanel: The JPanel is a simplest container class. It provides space in which an
application can attach any other component.
Example:
JPanel panel=new JPanel();
pancl.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton bl=new JButton("Button 1");
bl.setBounds(50,100,80,30);
panel.add(bl );
12. JDialog: The JDialog control represents atop level window with a border and a title
used to take some form of input from the user. " Unlike JFrame, it doesn't have
maximise and minimise buttons.
Example:
JFrame f new JFrame():
JDialog d=new JDialog(f, "Dialog", true);
JButton ("OK"):
JButton b = new
d.add(b);
Delegation Event Model
Handling in Swing/Explain called the
Q14. Explain Event Swing is based on an approach
mechanism used by
Ihe event handling gencrates an cvcnt and scnds il to
onC of
this mechanism, a source
dclegation event model. In an event. Once the
event
until it recejves
listener simply waits
more listeners. Here the
then returns.
arrives, the listener processes the event and
SOurce Event
Handler
Example:
import javax.swing. *;
import java.awt.*;
import java.awt.event.*;
public class ButtonExample extends JFrame implements ActionListener
private JButton button:
public ButtonExample0{
setSize(200,200);
setTitle("Button Example");
button = new JButton("Click Me");
add(button);
button.addActionListener(this);
setVisible(true);
public KeyExample()
setSize(200,200);
setTitle("Key Example");
JTextField(20):
textField = new
add(textField):
textField. addKeyListener(this);
setVisible(true);