[go: up one dir, main page]

0% found this document useful (0 votes)
5 views9 pages

Advanced Java - Sahana K

The document is an activity report on Swing applications as part of the Advanced Java course at Visvesvaraya Technological University. It covers the use of JScrollPane, JComboBox, and JTabbedPane components in Java Swing, providing explanations and code examples for each. The report is submitted by Ms. Sahana Kubasad under the guidance of Mrs. Rajeshwari S.G.
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)
5 views9 pages

Advanced Java - Sahana K

The document is an activity report on Swing applications as part of the Advanced Java course at Visvesvaraya Technological University. It covers the use of JScrollPane, JComboBox, and JTabbedPane components in Java Swing, providing explanations and code examples for each. The report is submitted by Ms. Sahana Kubasad under the guidance of Mrs. Rajeshwari S.G.
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/ 9

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELAGAVI – 590018

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Activity Report on

“SWING APPLICATIONS”
Course: Advanced Java(BCS613D)

Submitted by

Ms.Sahana Kubasad USN:2KA22CS040

Under the guidance of


Mrs. Rajeshwari S.G

SMT.KAMALA AND SRIVENKAPPA M. AGADI COLLEGE OF


ENGINEERING AND TECHNOLOGY LAXMESHWAR-582116

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)

JFrame jfrm = new JFrame("JScrollPaneDemo");


jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(400, 400);
//Createapanelandadd400buttonstoit.
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(20, 20));

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);

// Display the frame.


jfrm.setVisible(true);
}
publicstaticvoidmain(String[]args){
//Createtheframeontheeventdispatchingthread.
SwingUtilities.invokeLater(
new Runnable() {
publicvoidrun(){
newJScrollPaneDemo();
}
}
);
}
}
Outputfromthescrollpaneexampleisshownhere:

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>

Here,Erepresents thetypeoftheitems inthecombo box.

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.

Inadditiontopassinganarrayofitemstobedisplayedinthedrop-downlist,itemscanbe dynamically added to


the list of choices via the addItem( ) method, shown here:
voidaddItem(Eobj)

Here,objistheobjecttobeaddedtothecombobox.Thismethodmustbeusedonlywith mutable combo boxes.

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.

Onewaytoobtaintheitemselectedinthelististocall getSelectedItem( ) onthecombobox. Itis shown


here:
ObjectgetSelectedItem()
Youwillneedtocastthereturnedvalueintothetypeofobjectstoredinthelist.
The following example demonstrates the combo box. The combo box contains entries for
"Hourglass", "Analog", "Digital", and "Stopwatch". When a timepiece is selected, an icon-based
label is updated to display it. You can see how little code is required to use this powerful
component.

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 encapsulates a tabbed pane. It manages a set of components by linking them


with tabs. Selecting a tab causes the component associated with that tab to come to the
forefront. Tabbed panes are very common in the modern GUI, and you have no doubt used
them many times. Given the complex nature of a tabbed pane, they are surprisingly easy to
create and use.

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

Tabsare addedbycallingaddTab().).Hereis oneofits forms:

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.

Thegeneralprocedureto useatabbed paneisoutlinedhere:

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);

// Create the tabbed pane.JTabbedPanejtp


= new JTabbedPane();
jtp.addTab("Cities", new CitiesPanel());
jtp.addTab("Colors",newColorsPanel());
jtp.addTab("Flavors",newFlavorsPanel());
jfrm.add(jtp);

// Display the frame.


jfrm.setVisible(true);
}
publicstaticvoidmain(String[]args){
//Createtheframeontheeventdispatchingthread.
SwingUtilities.invokeLater( newRunnable() {
public void run() {
newJTabbedPaneDemo();
}
} );
}
}
//Makethepanelsthatwillbeaddedtothetabbedpane. class
CitiesPanel extends JPanel {

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

Demonstrated Activity out on swing


application

SKSVMACET,Lakshmeshwar Page 8

You might also like