Advanced Java - Sahana K
Advanced Java - Sahana K
BELAGAVI – 590018
Activity Report on
“SWING APPLICATIONS”
Course: Advanced Java(BCS613D)
Submitted by
AY2024-2025
AdvancedJava(BCS613D)
Swing Application
1. JScrollPane
A JscrollPane is used to create a scrollable component view. We use a scroll pane to display a
large component or a component whose size can change dynamically when the screen size is
constrained.Split panes and tabbed panes are two other containers for saving screen space.
JScrollPane is a lightweight container that automatically handles the scrolling of another
component. The component being scrolled can be either an individual component, such as a table,
or a group of components contained within another lightweight container, such as a JPanel. In
either case, if the object being scrolled is larger than the viewable area, horizontal and/or vertical
scroll bars are automatically provided, and the component can be scrolled through the pane.
Because JScrollPane automates scrolling, it usually eliminates the need to manage individual
scroll bars. The viewable area of a scroll pane is called the viewport. It is a window in which the
component being scrolled is displayed. Thus, the viewport displays the visible portion of the
component being scrolled.
The scroll bars scroll the component through the viewport. In its default behavior, a JScrollPane
will dynamically add or remove a scroll bar as needed.For example, if the component is taller than
the viewport, a vertical scroll bar is added. If the component will completely fit within the
viewport, the scroll bars are removed.
JScrollPanedefinesseveralconstructors.Oneamongtheseis:
JScrollPane(Componentcomp)
The component to be scrolled is specified by comp. Scroll bars are automatically displayed when
the content of the pane exceeds the dimensions of the viewport
Herearethestepstofollowtouseascrollpane:
1. Createthecomponenttobescrolled.
2. CreateaninstanceofJScrollPane,passingtoittheobjecttoscroll.
3. Addthescrollpanetothecontent pane.
The following example illustrates a scroll pane. First, a JPanel object is created, and 400 buttons are added
toit,arrangedinto20columns.Thispanel isthenaddedtoascroll pane,andthescroll paneisaddedtothe content
pane. Because the panel is larger than the viewport, vertical and horizontal scroll bars appear
automatically. You can use the scroll bars to scroll the buttons into view.
//DemonstrateJScrollPane.
import java.awt.*;
importjavax.swing.*;
publicclassJScrollPaneDemo{
public JScrollPaneDemo() {
//SetuptheJFrame.Usethedefault BorderLayot.
SKSVMACET,Lakshmeshwar Page 1
AdvancedJava(BCS613D)
intb= 0;
for(inti=0;i<20;i++){ for(intj
=0;j<20;j++){
jp.add(newJButton("Button"+b));
++b;
}
}
//Createthescrollpane.
JScrollPanejsp=newJScrollPane(jp);
// Addthescrollpanetothecontentpane.
//Becausethedefaultborderlayoutisused,
//thescrollpanewillbeaddedtothecenter.
jfrm.add(jsp, BorderLayout.CENTER);
SKSVMACET,Lakshmeshwar Page 2
AdvancedJava(BCS613D)
2. JComboBox
Swing provides a combo box (a combination of a text field and a drop-down list) through the
JComboBox class. A combo box normally displays one entry, but it will also display a dropdown
list that allows a userto select a different entry. You can also create a combo box that lets the user
enter a selection into the text field.
In the past, the items in a JComboBox were represented as Object references. However,
beginning with JDK 7, JComboBox was made generic and is now declared like this:
classJComboBox<E>
TheJComboBoxconstructorusedbytheexampleisshownhere:
JComboBox(E[]items)
Here, items is an array that initializes the combo box. Other constructors are available.
JComboBoxusestheComboBoxModel.Mutablecomboboxes(thosewhoseentriescanbe changed)
use the MutableComboBoxModel.
JComboBox generates an action event when the user selects an item from the list. JComboBox
also generates an item event when the state of selection changes, which occurs when an item is
selected or deselected. Thus, changing a selection means that two item events will occur: one for
the deselected item and another for the selected item. Often, it is sufficient to simply listen for
action events, but both event types are available for your use.
SKSVMACET,Lakshmeshwar Page 3
AdvancedJava(BCS613D)
//DemonstrateJComboBox. import
java.awt.*;
importjava.awt.event.*;
import javax.swing.*;
publicclassJComboBoxDemo{
String[]timepieces={ "Hourglass","Analog","Digital","Stopwatch"};
publicJComboBoxDemo(){
// Set uptheJFrame.
JFrame jfrm = new JFrame("JCombBoxDemo");
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(400, 250);
//Instantiateacomboboxandaddittothecontentpane.
JComboBox jcb = new JComboBox(timepieces);
jfrm.add(jcb);
//Createalabelandaddittothecontent pane.
JLabeljlab=newJLabel(newImageIcon("hourglass.png"));
jfrm.add(jlab);
// Handle selections.
jcb.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){ String
s = (String) jcb.getSelectedItem(); jlab.setIcon(new
ImageIcon(s + ".png"));
}
});
// Display the frame.
jfrm.setVisible(true);
}
publicstaticvoidmain(String[]args){
//Createtheframeontheeventdispatchingthread.
SwingUtilities.invokeLater(
new Runnable() {
publicvoidrun(){
newJComboBoxDemo();
}
}
);
}
}
SKSVMACET,Lakshmeshwar Page 4
AdvancedJava(BCS613D)
Outputfromthecomboboxexampleisshownhere:
3. JTabbedPane
JTabbedPane defines three constructors. We will use its default constructor, which createsan
empty control with the tabs positioned across the top of the pane. The other two constructors
let you specifythe location of the tabs, which can be along any of the four sides. JTabbedPane
uses the SingleSelectionModel
voidaddTab(Stringname,Componentcomp)
Here, name is the name for the tab, and comp is the component that should be added to the tab.
Often, the component added to a tab is a JPanel that contains a group of related components. This
technique allows a tab to hold a set of components.
1. CreateaninstanceofJTabbedPane.
2. Addeach tab bycallingaddTab().
3. Addthetabbedpanetothecontent pane
SKSVMACET,Lakshmeshwar Page 5
AdvancedJava(BCS613D)
The following example illustrates a tabbed pane. The first tab is titled "Cities" and contains
four buttons. Each button displays the name of a city. The second tab is titled "Colors" and
contains three check boxes. Each check box displays the name of a color. The third tab is
titled "Flavors" and contains one combo box. This enables the user to select one of three
flavors.
//DemonstrateJTabbedPane.
importjavax.swing.*; import
java.awt.*;
publicclassJTabbedPaneDemo{
public JTabbedPane {
//Set uptheJFrame.
JFramejfrm = new JFrame("JTabbedPaneDemo");
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(400, 200);
publicCitiesPanel(){
JButtonb1=newJButton("NewYork"); add(b1);
JButtonb2=newJButton("London");
SKSVMACET,Lakshmeshwar Page 6
AdvancedJava(BCS613D)
add(b2);
JButtonb3=newJButton("HongKong"); add(b3);
JButtonb4=newJButton("Tokyo");
add(b4);
}
}
classColorsPanelextendsJPanel{
public ColorsPanel() {
JCheckBoxcb1=newJCheckBox("Red");
add(cb1);
JCheckBoxcb2=newJCheckBox("Green");
add(cb2);
JCheckBoxcb3=newJCheckBox("Blue");
add(cb3);
}
}
class FlavorsPanel extends JPanel {
public FlavorsPanel() {
JComboBoxjcb=newJComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}
}
Outputfrom thetabbed paneexampleis showninthefollowingthree illustrations:
SKSVMACET,Lakshmeshwar Page 7
AdvancedJava(BCS613D)
Photo gallery
SKSVMACET,Lakshmeshwar Page 8