Ajp 6
Ajp 6
: 53
Program code
1.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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);
}
}
Output
Name of Student: Ekambe Ganesh Roll No.: 53
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;
Output
Name of Student: Ekambe Ganesh Roll No.: 53
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