Unit IV Swing Part B
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
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
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:
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
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.
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
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
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