Java Swing Status Bar Implementation

75% found this document useful (4 votes)
140 views28 pages
The document discusses Java Swing and provides examples of using different layout managers in Swing. It begins with a simple example of creating a JFrame and then discusses the anatomy of Sw…

Uploaded by

kumarharsh

Java Swing

Kumar Harshit, USW


A Simple Example

import [Link];
public class Simple extends JFrame {
public Simple() {
setSize(300, 200);
setTitle("Simple");

setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {


Simple simple = new Simple();
[Link](true);
}
}
Anatomy

 import [Link];
 This is a top level container, which is used to place other
components.

 setDefaultCloseOperation(EXIT_ON_CLOSE);
 This method will close the window.
Adding Buttons to a Frame
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class Buttons extends JFrame {


private Toolkit toolkit;
public Buttons() {
setTitle("Buttons");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
getContentPane().add(panel);
[Link](null);
JButton beep = new JButton("Beep");
[Link](150, 60, 80, 30);
//continued….
Adding Buttons - II
[Link](new ActionListener() { public void actionPerformed(ActionEvent event) { [Link](); } });

JButton close = new JButton("Close");


[Link](50, 60, 80, 30);
[Link](new ActionListener() { public void actionPerformed(ActionEvent event) { [Link](0); } });

[Link](beep);
[Link](close);
}

public static void main(String[] args) {


Buttons buttons = new Buttons();
[Link](true);
}
}
Anatomy of buttons example

 Three new things to notice


 Add Panel to a Frame
 Layout Management
 Event Handling
Buttons Example – Adding Panel

 JPanel panel = new JPanel();


getContentPane().add(panel);

- We create a panel using JPanel, and then add the panel to the Frame.
- Note that getContentPane() is a method of Jframe class.
- getContentPane() method returns an object of type Container.
- To the returned container object, add a panel, using add method of
Container class.
- Components like textbox, button can be added to the panel
Button Example – Layout
Management

 [Link](null);
- A layout manager is used for laying out components.
- By default, a panel has FlowLayout Manager.
- setLayout(null) takes off layout manager from the panel. This means, we can
layout components as we wish

 JButton beep = new JButton("Beep");


[Link](150, 60, 80, 30);
[Link](beep);
- 1st line, creates a button.
- 2nd line, places a button on the panel using setBounds method.
- 3rd line, adds a button to the panel.
Button Example – Event Handling

 [Link](new ActionListener() {
public void actionPerformed(ActionEvent event) {
[Link]();
}
});
- ActionListener is an interface, and is a sub-interface of EventListener.
- Above is an example of inner-class
- We are creating a class which has no name, but it implements ActionListener interface.
Inorder to do so, it also provides implementation for the actionPerformed() method.
- addActionLisener(), adds a Listener to the beep button.
- To make the above example easier, please see next slide.
Button Example – Event Handling

[Link](new ButtonActionListener());

class ButtonActionListener implements ActionListener{


public void actionPerformed(ActionEvent event){
[Link]("beep called");
}
}

- Create a new class ButtonActionListener, which implemetns Action Listener.


- [Link](new ButtonActionListener)
- this line adds a listener to the beep button, if an event occurs, an object of
ButtonActionListener is created, and actionPerformed() method is invoked
Adding Tooltip

 Inthe previous example, add the following


code
– [Link]("A button component");
Menu and Toolbars

 To implement a menu-bar, use three classes


– JMenubar
– JMenu
– JMenuItem
How Menu Works

 JMenuBar menubar = new JMenuBar();


 Create an object of type JMenuBar

 JMenu file = new JMenu("File");


 Create an object of type JMenu
 JMenuItem fileClose = new JMenuItem("Close", icon);

Create an object of type JMenuItem

 [Link](fileClose);
 Add menu-item to the menu.
 [Link](file);
 Add menu to the MenuBar object.
 setJMenuBar(menubar);
 Add menuBar to the Frame.
Menu - Code
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Menu extends JFrame {


public Menu() {
setTitle("JMenuBar");
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("[Link]");
JMenu file = new JMenu("File");
[Link](KeyEvent.VK_F);

//continued….. Next slide


Menu – Code – Contd….
JMenuItem fileClose = new JMenuItem("Close", icon);
[Link](KeyEvent.VK_C);
[Link]("Exit application");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent event) {
[Link](0);
});
[Link](fileClose);
[Link](file);
setJMenuBar(menubar);
setSize(250, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Menu();
}
}
SubMenu and Separator

 JMenuBar menubar = new JMenuBar();


 JMenu file = new JMenu("File");
 JMenu imp = new JMenu("Import");
 [Link](new JMenuItem(“import new feed….”));
 [Link](new JMenuItem(“import bookmarks….”));
– Here file and imp are both menu and contain further menu-items.
 [Link](new JMenuItem("New", iconNew));
 [Link](new JMenuItem(“Open", iconNew));
 [Link]()
– This adds a separator after new and open, which are menu-items of file.
 [Link](imp);
– This adds a sub-menu to the File Menu, as shown in fig.
How to create a PopUp Menu

 menu = new JPopupMenu();


 JMenuItem menuItemBeep = new JMenuItem("Beep");
 [Link](menuItemBeep);
 [Link](new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if ([Link]() == e.BUTTON3) {
[Link]([Link](), [Link](),
[Link]()); }}});
Swing Layout Management

 No Manager
 Flowlayout Manager
 Gridlayout Manager
 Borderlayout Manager
 Boxlayout Manager
No Layout Manager

 This mean no layout manager is used.


 Components are laid out using explicit
values.
 Example on slide 3.
FlowLayout Manager

 The manager puts component in a row, in


the order they are added.
 If they don’t fit in a row, they are appended
on the next row.
 The components can be added left to right or
vice versa.
 Three constructors
– FlowLayout() //by default, centered, 5px spaces
– FlowLayout(int align)
– FlowLayout(int align, int hgap, int vgap)
Example of FlowLayout Manager
public class FlowLayoutExample extends JFrame {
public FlowLayoutExample() {
setTitle("FlowLayout Example");
JPanel panel = new JPanel();
JTextArea area = new JTextArea("text area");
[Link](new Dimension(100, 100));
JButton button = new JButton("button");
[Link](button);
JTree tree = new JTree();
[Link](tree);
[Link](area);
add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true); }
public static void main(String[] args) { new FlowLayoutExample(); } }
Grid Layout Manager

 Itmakes a grid, and lays out components in


that grid.
 One component is placed in each rectangle.
Example: Grid Layout Manager

 Get a panel object


– JPanel p = new JPanel()
 Create grid layout manager
– GridLayout gl = new GridLayout()
 Add gridlayout to the panel
– [Link](gl)
 Constructor
– GridLayout(int rows, int cols, int hgap, int vgap)
Example – Grid Layout Manager

public class GridLayoutExample extends JFrame {


public GridLayoutExample() {
setTitle("GridLayout");
JPanel panel = new JPanel();
[Link]([Link](5, 5, 5, 5));
[Link](new GridLayout(5, 4, 5, 5));
String[] buttons = { "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2",
"3", "-", "0", ".", "=", "+" };
for (int i = 0; i < [Link]; i++) {
if (i == 2) [Link](new JLabel(buttons[i]));
else [Link](new JButton(buttons[i]));
}
add(panel);
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true); }
Border Layout Manager

 Divides spaces into 5 regions


– North, West, South, East, and Centre
 Can add only one component in one region
 If we want to add more than one component
in one reigion, then add a panel to that
region
Example: Border Layout
public class BorderLayoutExample extends JFrame {
public BorderLayoutExample() {
setTitle("BorderLayout");
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
[Link](file);
setJMenuBar(menubar);
JToolBar toolbar = new JToolBar();
[Link](false);
ImageIcon exit = new ImageIcon("[Link]");
JButton bexit = new JButton(exit);
[Link](new EmptyBorder(0 ,0, 0, 0));
[Link](bexit);
add(toolbar, [Link]);

//contd…
Example: Border Layout – Contd..
JToolBar vertical = new JToolBar([Link]);
[Link](false);
[Link](new Insets(10, 5, 5, 5));
ImageIcon select = new ImageIcon("[Link]");
ImageIcon freehand = new ImageIcon("[Link]");
ImageIcon shapeed = new ImageIcon("[Link]");
JButton selectb = new JButton(select);
[Link](new EmptyBorder(3, 0, 3, 0));
JButton freehandb = new JButton(freehand);
[Link](new EmptyBorder(3, 0, 3, 0));
JButton shapeedb = new JButton(shapeed);
[Link](new EmptyBorder(3, 0, 3, 0));
[Link](selectb);
[Link](freehandb);
[Link](shapeedb);
//contd…
Example: Border Layout – Contd..
add(vertical, [Link]);
add(new JTextArea(), [Link]);
JLabel statusbar = new JLabel(" Statusbar");
[Link](new Dimension(-1, 22));
[Link]([Link]());
add(statusbar, [Link]);
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutExample();
}
}

|u0001u0002u0003u0004u0005u0006u0007
au0001u0002u0003u0004u0005u0002u0003u0006u0007b	
u0004u000bf
iu0002u0003u0005b	
u000bu0002f
b	
u000b
àu0001u0002u0003u0004u0005u0006u0007bu0007	
u000bfà
u000e
u000fu0010u0003u0007u0011u0012u0005
u0001u0013u0014u0015àu0016u0005u0016u0015u0007u000bu000bu0005u0017àu0001u0015u0011u0005u0011	u0004u0011
u0018u000bu0005u000fu0010u0003u0007u0011u0005u0019u0005
u0001u0013u0014u0015àu0016u0005u0017àu0001u0015u0011u001au001bu0005u0019u0005
u000bu0011u0004u0017àu001cu0011u001au001du001eu001eu001fu0005 u001eu001eu001bu0012u0005
u000bu0011u0004!àu0004u0015u0011u001a
iu0006u000eu000fbu0010u0002
V bu0001u000eu000fu0003	u0004u0010u0002u0011u0002u0012u0013u0006u0014bu0015u0016u0013u0017u0018u0003u0002u0001u0019u001au0004
V u001bu0007bu0006u0004bu0006u0004u0002u0004	u000fu000eu0004u001cu0019u0011u0019u001cu0004u001du000fu0015	u0002bu0015u0019u0003
u0004u0014u0007bu001du0007u0004bu0006u0004u0006u0019u001eu0004	u000fu0004u000eu001cu0002u001du0019u0004u000f	u0007u0019u0003u0004
u001du000fu0001u000eu000fu0015u0019u0015	u0006u0013
V u0006u0019	u001fu0019 u0002u001c	!u001c
iu0005u0006u0007u0002u0011u0012u000eu000eu000fu0006u0013u0002u000eu000fu0002u0002u0014u0015bu000b
bu0001u000eu000fu0003	u0004u0010u0002u0011u0002u0013u0002u0014	u0013u0019u0011u0019u0015	u0013+u001d	bu000fu0015$u0011u0019u0015	u001au0004
bu0001u000eu000fu0003	u0004u0010u0002u0011u0002u0013u0002u0014	u0013u0019u0011u0019u0015	u0013+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003u001au0004
bu0001u000eu000fu0003	u0004u0010u0002u0011u0002u0012u0013u0006u0014bu0015u0016u0013u0017,		u000fu0015u001au0004
iu0005u0006u0007u0002u0011u0012u000eu000eu000fu0006u0013u0002u0016 u0017u0017
.u0019u0019u000eu0013u0002u001eu001e+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#u0015u0019u0014u0004+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#*u0004/u0004u000e.u001cbu001du0004u0011u000fbu001eu0004u0002u001d	bu000fu0015-u0019u0003 u000fu0003u0001u0019u001e#+u001d	bu000fu0015$u0011u0019u0015	u0004u0019u0011u0019u0015	*u0004/u0004	u000fu000fu001c0b	u0013.u0019u0019u000e#
iu0006u000eu000fbu0010u0002u000fu0018u0002u0019u0012u000eu000eu000fu0006u0013u0002u000b
b	
u000b
V u001bu0007u0003u0019u0019u0004u0015u0019u0014u0004	u0007bu0015u0016u0006u0004	u000fu0004u0015u000f	bu001du0019
V +u001eu001eu0004-u0002u0015u0019u001cu0004	u000fu0004u0002u0004u0018u0003u0002u0001u0019
V )u00027u000f	u0004Bu0002u0015u0002u0016u0019u0001u0019u0015	
V $u0011u0019u0015	u0004u0005u0002u0015u001eu001cbu0015u0016
u0011u0012u000eu000eu000fu0006u0013u0002f
b	
u000bu0002u001a iu0005u0006u0007u0002u001bu0006u000b
V u0017-u0002u0015u0019u001cu0004u000eu0002u0015u0019u001cu00046u0004u0015u0019u0014u0004u0017-u0002u0015u0019u001c#*u001au0004
u0016u0019	!u000fu0015	u0019u0015	-u0002u0015u0019#*u0013u0002u001eu001e#u000eu0002u0015u0019u001c*u001a
C

u0019u0004u001du0003u0019u0002	u0019u0004u0002u0004u000eu0002u0015u0019u001cu0004u0006bu0015u0016u0004u0017-u0002u0015u0019u001c
u0004u0002u0015u001eu0004
u0011u0012u000eu000eu000fu0006u0002f
b	
u000bu0002u001a u001cu0010u000fu0012u000eu0002
u001du0006u0007u000bbu000bu0006u000e
V u000eu0002u0015u0019u001cu0013u0006u0019	)u00027u000f	#u0015u001cu001c*u001au0004
C
+u0004u001cu00027u000f	u0004u0001u0002u0015u0002u0016u0019u0003u0004bu0006u0004u0006u0019u001eu0004 u000fu0003u0004u001cu00027bu0015u0016u0004u000f	u0004u001du000fu0001u000eu000fu0015u0019u0015	u0006u0013
C
,7u0004u001eu0019 u0002u001c	
u0004u0002u0004u000eu0002u0015
u0011u0012u000eu000eu000fu0006u0002f
b	
u000bu0002u001a fu0001u000bu0006u000eu0002u001eu0006
u0005u0006u0007
V .u0019u0019u000eu0013u0002u001eu001e+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#u0015u0019u0014u0004+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#*u0004/u0004
u000e.u001cbu001du0004u0011u000fbu001eu0004u0002u001d	bu000fu0015-u0019u0003 u000fu0003u0001u0019u001e#+u001d	bu000fu0015$u0011u0019u0015	u0004u0019u0011u0019u0015	*u0004/
u0011u0012u000eu000eu000fu0006u0002f
b	
u000bu0002u001a fu0001u000bu0006u000eu0002u001eu0006
u0005u0006u0007
.u0019u0019u000eu0013u0002u001eu001e+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#u0015u0019u0014u0004,		u000fu0015+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003#**u001a
u001du001cu0002u0006u0006u0004,		u000fu0015+u001d	bu000fu0015)bu0006	u0019u0015u0019u0003u0004bu0001u000eu001cu0019u0001u0019u0015	u0006u0004+u001d	bu000fu0015)b

You might also like