0% found this document useful (0 votes)
20 views5 pages

Ajp 6

This document contains the code for a Java program that uses a JComboBox to allow a user to select an Indian state from a dropdown list and displays the currently selected state. It also includes code that uses a JScrollPane to add a vertical scrollbar to a JTextArea component when its text extends beyond the visible area. The output sections are empty, suggesting the programs were run successfully but no output was included.

Uploaded by

Ganesh Ekambe
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)
20 views5 pages

Ajp 6

This document contains the code for a Java program that uses a JComboBox to allow a user to select an Indian state from a dropdown list and displays the currently selected state. It also includes code that uses a JScrollPane to add a vertical scrollbar to a JTextArea component when its text extends beyond the visible area. The output sections are empty, suggesting the programs were run successfully but no output was included.

Uploaded by

Ganesh Ekambe
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/ 5

Name of Student: Ekambe Ganesh Roll No.

: 53

Experiment No.: 06 DOS:

Program code
1.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Pr6 extends JFrame implements ActionListener {


private JComboBox<String> cityComboBox;
private JLabel currentCityLabel;

public Pr6() {
setTitle("City ComboBox Example");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
String[] cities = {"Solapur", "Pune", "Banglore", "Mumbai"};
cityComboBox = new JComboBox<>(cities);
cityComboBox.addActionListener(this);
currentCityLabel = new JLabel("You are in ");
add(cityComboBox);
add(currentCityLabel);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cityComboBox) {
String selectedCity = (String) cityComboBox.getSelectedItem();
currentCityLabel.setText("You are in " + selectedCity);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
Pr6 example = new Pr6();
example.setVisible(true);
});
}
}
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 06 DOS:

Output
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 06 DOS:

Exercise
1. Write a programme to develop a frame to select the different states
of India using JComboBox .

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Pr6Ex1 extends JFrame implements ActionListener {


private JComboBox<String> stateComboBox;
private JLabel selectedStateLabel;
public Pr6Ex1() {
setTitle("Indian State Selection");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
String[] states = {
"Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh",
"Goa", "Gujarat", "Haryana", "Himachal Pradesh", "Jharkhand",
"Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra", "Manipur",
"Meghalaya", "Mizoram", "Nagaland", "Odisha", "Punjab",
"Rajasthan", "Sikkim", "Tamil Nadu", "Telangana", "Tripura",
"Uttar Pradesh", "Uttarakhand", "West Bengal"
};
stateComboBox = new JComboBox<>(states);
stateComboBox.addActionListener(this);
selectedStateLabel = new JLabel("Selected State: ");
add(new JLabel("Select a state: "));
add(stateComboBox);
add(selectedStateLabel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == stateComboBox) {
String selectedState = (String) stateComboBox.getSelectedItem();
selectedStateLabel.setText("Selected State: " + selectedState);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 06 DOS:

Pr6Ex1 frame = new Pr6Ex1();


frame.setVisible(true);
});
}
}

Output
Name of Student: Ekambe Ganesh Roll No.: 53

Experiment No.: 06 DOS:

2. Develop a programme to demonstrate the use of ScrollPane in Swings.

import javax.swing.*;
public class Pr6Ex2 extends JFrame {
public Pr6Ex2() {
setTitle("ScrollPane Example");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(10, 30);
textArea.setText("Hello, Good Morning I am Ekambe Ganesh \n"
+ "I am studied in Gramin Management Csmpus Vishnupuri , Nanded.");
JScrollPane scrollPane = new JScrollPane(textArea);

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AL
WAYS);
add(scrollPane);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Pr6Ex2 example = new Pr6Ex2();
example.setVisible(true);
});
}
}

Output

You might also like