0% found this document useful (0 votes)
17 views18 pages

Unit IV Swing Part B

The document provides an overview of various Swing components in Java, including JOptionPane, JScrollBar, JMenuBar, JProgressBar, JSlider, JDialog, JPanel, JScrollPane, and JToolBar. Each component is described with its class declaration, common constructors, methods, and examples of usage. The document serves as a reference for creating GUI applications using these Swing components.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Unit IV Swing Part B

The document provides an overview of various Swing components in Java, including JOptionPane, JScrollBar, JMenuBar, JProgressBar, JSlider, JDialog, JPanel, JScrollPane, and JToolBar. Each component is described with its class declaration, common constructors, methods, and examples of usage. The document serves as a reference for creating GUI applications using these Swing components.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit IV Swing Part B

JOptionPane
The JOptionPane class is used to provide standard dialog boxes such as message dialog box,
confirm dialog box and input dialog box. These dialog boxes are used to display information or
get input from the user. The JOptionPane class inherits JComponent class.
JOptionPane class declaration
public class JOptionPane extends JComponent implements Accessible

Common Constructors of JOptionPane class


Constructor Description
JOptionPane() It is used to create a JOptionPane with a test message.
It is used to create an instance of JOptionPane to display a
JOptionPane(Object message)
message.
JOptionPane(Object message, int It is used to create an instance of JOptionPane to display a
messageType message with specified message type and default options.

Common Methods of JOptionPane class


Methods Description
It is used to create and return a new
JDialog createDialog(String title)
parentless JDialog with the specified title.
static void showMessageDialog(Component It is used to create an information-
parentComponent, Object message) message dialog titled "Message".
static void showMessageDialog(Component
It is used to create a message dialog with
parentComponent, Object message, String title, int
given title and messageType.
messageType)
It is used to create a dialog with the
static int showConfirmDialog(Component
options Yes, No and Cancel; with the
parentComponent, Object message)
title, Select an Option.
It is used to show a question-message
static String showInputDialog(Component
dialog requesting input from the user
parentComponent, Object message)
parented to parentComponent.
It is used to set the input value that was
void setInputValue(Object newValue)
selected or input by the user.

JOptionPane Example: showMessageDialog()


import javax.swing.*;
public class OptionPaneExample
{
JFrame f;
Page 1 of 18
OptionPaneExample()
{
f=new JFrame();
JOptionPane.showMessageDialog(f,"Hello, Welcome to Java.");
}
public static void main(String[] args)
{
new OptionPaneExample();
}
}
Output:

JOptionPane Example: showMessageDialog()


import javax.swing.*;
public class OptionPaneExample1
{
JFrame f;
OptionPaneExample1()
{
f=new JFrame();
JOptionPane.showMessageDialog(f,"Successfully
Updated.","Alert",JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args)
{
new OptionPaneExample1();
}
}
Output:

Page 2 of 18
JOptionPane Example: showInputDialog()
import javax.swing.*;
public class OptionPaneExample2
{
JFrame f;
OptionPaneExample2()
{
f=new JFrame();
String name=JOptionPane.showInputDialog(f,"Enter Name");
}
public static void main(String[] args)
{
new OptionPaneExample2();
}
}

JScrollBar
The object of JScrollbar class is used to add horizontal and vertical scrollbar. It is an
implementation of a scrollbar. It inherits JComponent class.
JScrollBar class declaration
public class JScrollBar extends JComponent implements Adjustable, Accessible

Commonly used Constructors:


Constructor Description
JScrollBar() Creates a vertical scrollbar with the initial values.
Creates a scrollbar with the specified orientation and
JScrollBar(int orientation)
the initial values.
JScrollBar(int orientation, int value, int Creates a scrollbar with the specified orientation,
extent, int min, int max) value, extent, minimum, and maximum.

JScrollBar Example
import javax.swing.*;
class ScrollBarExample
{
ScrollBarExample()
Page 3 of 18
{
JFrame f= new JFrame("Scrollbar Example");
JScrollBar s=new JScrollBar();
s.setBounds(100,100, 50,100);
f.add(s);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ScrollBarExample();
}
}
Output:

JMenuBar, JMenu and JMenuItem


The JMenuBar class is used to display menubar on the window or frame. It may have several
menus. The object of JMenu class is a pull down menu component which is displayed from the
menu bar. It inherits the JMenuItem class. The object of JMenuItem class adds a simple labeled
menu item. The items used in a menu must belong to the JMenuItem or any of its subclass.

JMenuBar class declaration


public class JMenuBar extends JComponent implements MenuElement, Accessible
JMenu class declaration
public class JMenu extends JMenuItem implements MenuElement, Accessible

Page 4 of 18
JMenuItem class declaration
public class JMenuItem extends AbstractButton implements Accessible, MenuElement
Java JMenuItem and JMenu Example
import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample()
{
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
Output:

Page 5 of 18
JProgressBar
The JProgressBar class is used to display the progress of the task. It inherits JComponent class.
JProgressBar class declaration
public class JProgressBar extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:


Constructor Description
JProgressBar() It is used to create a horizontal progress bar but no string text.
JProgressBar(int min, It is used to create a horizontal progress bar with the specified minimum
int max) and maximum value.
It is used to create a progress bar with the specified orientation, it can be
JProgressBar(int
either Vertical or Horizontal by using SwingConstants.VERTICAL and
orient)
SwingConstants.HORIZONTAL constants.
JProgressBar(int
It is used to create a progress bar with the specified orientation,
orient, int min, int
minimum and maximum value.
max)

Commonly used Methods:


Method Description
void
It is used to determine whether string should be displayed.
setStringPainted(boolean b)
void setString(String s) It is used to set value to the progress string.

Page 6 of 18
It is used to set the orientation, it may be either vertical or horizontal
void setOrientation(int
by using SwingConstants.VERTICAL and
orientation)
SwingConstants.HORIZONTAL constants.
void setValue(int value) It is used to set the current value on the progress bar.
JProgressBar Example
import javax.swing.*;
public class ProgressBarExample extends JFrame
{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample()
{
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
}
public void iterate()
{
while(i<=2000)
{
jb.setValue(i);
i=i+20;
try
{
Thread.sleep(150);
}
catch(Exception e)
{}
}
}
public static void main(String[] args)
{
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}
}
Output:
Page 7 of 18
JSlider
The Java JSlider class is used to create the slider. By using JSlider, a user can select a value from
a specific range.

Commonly used Constructors of JSlider class


Constructor Description
JSlider() creates a slider with the initial value of 50 and range of 0 to 100.
creates a slider with the specified orientation set by either
JSlider(int orientation) JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to
100 and initial value 50.
JSlider(int min, int max) creates a horizontal slider using the given min and max.
JSlider(int min, int max,
creates a horizontal slider using the given min, max and value.
int value)
JSlider(int orientation, int
creates a slider using the given orientation, min, max and value.
min, int max, int value)

Commonly used Methods of JSlider class


Method Description
public void setMinorTickSpacing(int n) is used to set the minor tick spacing to the slider.
public void setMajorTickSpacing(int n) is used to set the major tick spacing to the slider.
public void setPaintTicks(boolean b) is used to determine whether tick marks are painted.
public void setPaintLabels(boolean b) is used to determine whether labels are painted.
public void setPaintTracks(boolean b) is used to determine whether track is painted.

Java JSlider Example


import javax.swing.*;
public class SliderExample1 extends JFrame
{
public SliderExample1()
{
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
Page 8 of 18
JPanel panel=new JPanel();
panel.add(slider);
add(panel);
}
public static void main(String s[])
{
SliderExample1 frame=new SliderExample1();
frame.pack();
frame.setVisible(true);
}
}
Output:

JSlider Example: painting ticks


import javax.swing.*;
public class SliderExample2 extends JFrame
{
public SliderExample2()
{
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
slider.setMinorTickSpacing(2);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
JPanel panel=new JPanel();
panel.add(slider);
add(panel);
}
public static void main(String s[])
{
SliderExample2 frame=new SliderExample2();
frame.pack();
frame.setVisible(true);
}
}
Output:

Page 9 of 18
Dialog
The JDialog control represents a top level window with a border and a title used to take some
form of input from the user. It inherits the Dialog class. Unlike JFrame, it doesn't have maximize
and minimize buttons.
JDialog class declaration
public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContai
ner

Commonly used Constructors:


Constructor Description
It is used to create a modeless dialog without a title and
JDialog()
without a specified Frame owner.
It is used to create a modeless dialog with specified Frame
JDialog(Frame owner)
as its owner and an empty title.
JDialog(Frame owner, String title, It is used to create a dialog with the specified title, owner
boolean modal) Frame and modality.

JDialog Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogExample
{
private static JDialog d;
DialogExample()
{
JFrame f= new JFrame();
d = new JDialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
JButton b = new JButton ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
Page 10 of 18
});
d.add( new JLabel ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
Output:

JPanel
The JPanel is a simplest container class. It provides space in which an application can attach any
other component. It inherits the JComponents class. It doesn't have title bar.
JPanel class declaration
public class JPanel extends JComponent implements Accessible

Commonly used Constructors:


Constructor Description
It is used to create a new JPanel with a double buffer and a flow
JPanel()
layout.
JPanel(boolean It is used to create a new JPanel with FlowLayout and the
isDoubleBuffered) specified buffering strategy.
It is used to create a new JPanel with the specified layout
JPanel(LayoutManager layout)
manager.

JPanel Example
import java.awt.*;
Page 11 of 18
import javax.swing.*;
public class PanelExample
{
PanelExample()
{
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PanelExample();
}
}
Output:

Page 12 of 18
JScrollPane
A JscrollPane is used to make scrollable view of a component. When screen size is limited, we
use a scroll pane to display a large component or a component whose size can change
dynamically.

Constructors
Constructor Purpose
JScrollPane()
JScrollPane(Component) It creates a scroll pane. The Component parameter, when present,
JScrollPane(int, int) sets the scroll pane's client. The two int parameters, when present,
set the vertical and horizontal scroll bar policies (respectively).
JScrollPane(Component,
int, int)

Useful Methods
Modifier Method Description
void setColumnHeaderView(Component) It sets the column header for the scroll pane.
void setRowHeaderView(Component) It sets the row header for the scroll pane.
void setCorner(String, Component) It sets or gets the specified corner. The int
parameter specifies which corner and must be
one of the following constants defined in
ScrollPaneConstants:
UPPER_LEFT_CORNER,
UPPER_RIGHT_CORNER,
Component getCorner(String) LOWER_LEFT_CORNER,
LOWER_RIGHT_CORNER,
LOWER_LEADING_CORNER,
LOWER_TRAILING_CORNER,
UPPER_LEADING_CORNER,
UPPER_TRAILING_CORNER.
void setViewportView(Component) Set the scroll pane's client.

JScrollPane Example
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JScrollPaneExample
{
//private static final long serialVersionUID = 1L;
private static void createAndShowGUI()
Page 13 of 18
{
// Create and set up the window.
final JFrame frame = new JFrame("Scroll Pane Example");
// Display the window.
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set flow layout for the frame
frame.getContentPane().setLayout(new FlowLayout());
JTextArea textArea = new JTextArea(20, 20);
JScrollPane scrollableTextArea = new JScrollPane(textArea);
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTA
L_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SC
ROLLBAR_ALWAYS);
frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Output:

Page 14 of 18
ToolBar
JToolBar container allows us to group other components, usually buttons with icons in a row or
column. JToolBar provides a component which is useful for displaying commonly used actions
or controls.
Nested Classes
Modifier and
Class Description
Type
This class implements accessibility support for
protected class JToolBar.AccessibleJToolBar
the JToolBar class.
static class JToolBar.Separator A toolbar-specific separator.

Constructors
Constructor Description
It creates a new tool bar; orientation defaults to
JToolBar()
HORIZONTAL.
JToolBar(int orientation) It creates a new tool bar with the specified orientation.
JToolBar(String name) It creates a new tool bar with the specified name.
JToolBar(String name, int It creates a new tool bar with a specified name and
orientation) orientation.

Useful Methods
Modifier and Type Method Description
It adds a new JButton which
JButton add(Action a)
dispatches the action.
addImpl(Component comp, Object If a JButton is being added, it is
protected void
constraints, int index) initially set to be disabled.
It appends a separator of default
void addSeparator()
size to the end of the tool bar.
It returns a properly configured
PropertyChangeListener which
updates the control as changes
protected createActionChangeListener(JButton
to the Action occur, or null if
PropertyChangeListener b)
the default property change
listener for the control is
desired.
Factory method which creates
protected JButton createActionComponent(Action a) the JButton for Actions added to
the JToolBar.

Page 15 of 18
It returns the tool bar's current
ToolBarUI getUI()
UI.
It sets the L&F object that
void setUI(ToolBarUI ui)
renders this component.
It sets the orientation of the tool
void setOrientation(int o)
bar.
JToolBar Example
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class JToolBarExample
{
public static void main(final String args[])
{
JFrame myframe = new JFrame("JToolBar Example");
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton button = new JButton("File");
toolbar.add(button);
toolbar.addSeparator();
toolbar.add(new JButton("Edit"));
toolbar.add(new JComboBox(new String[] { "Opt-1", "Opt-2", "Opt-3",
"Opt-4" }));
Container contentPane = myframe.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane mypane = new JScrollPane(textArea);
contentPane.add(mypane, BorderLayout.EAST);
myframe.setSize(450, 250);
myframe.setVisible(true);
}
}
Output:

Page 16 of 18
JInternalFrame
JInternalFrame is a lightweight object that provides features similar to native window.
JInternalFrame jn = new JInternalFrame(“InternalFrame”,true,true,true);
The first argument specifies the title to be displayed by the internal frame. The rest of the
arguments specify whether the internal frame should contain decorations allowing the user to
resize, close, and maximize the internal frame . The default value for each boolean argument
is false, which means that the operation is not allowed.
Example:
import javax.swing.*;
import java.awt.*;
class JInternalFrameTest extends JFrame
{
JInternalFrameTest()
{
setTitle("JInternalFrame");
setJInternalFrame();
setSize(700,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setJInternalFrame()
{
JInternalFrame jn=new JInternalFrame("InternalFrame",true,true,true);
jn.setLayout(new FlowLayout());
jn.add(new Button("JButton"));
jn.setVisible(true);
add(jn);
}
}
public class Javaapp
Page 17 of 18
{
public static void main(String[] args)
{
JInternalFrameTest jn=new JInternalFrameTest();
}
}
Output:

Source:
1. https://www.javatpoint.com/java-tutorial
2. https://www.tutorialspoint.com/java/index.htm
3. https://www.geeksforgeeks.org/java/?ref=ghm

Page 18 of 18

You might also like