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

General Programs On Swing and Java Collections: 1. Message Dialog (Joptionpane)

Uploaded by

shwetharoopesh25
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)
2 views5 pages

General Programs On Swing and Java Collections: 1. Message Dialog (Joptionpane)

Uploaded by

shwetharoopesh25
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

General Programs on Swing and Java Collections

1. Message Dialog (JOptionPane)


import [Link].*;
public class MsgDialog {
public static void main(String[] args) {
JFrame f = new JFrame();

[Link](f, "Welcome to Swing!");

[Link](300,300);
[Link](null);
[Link](true);
}
}
2. Input Dialog
import [Link].*;

public class InputDialogEx {


public static void main(String[] args) {
JFrame f = new JFrame();

String name = [Link](f, "Enter your name:");


[Link](f, "Hello " + name);

[Link](300,300);
[Link](null);
[Link](true);
}
}
3. Confirm Dialog
import [Link].*;

public class ConfirmDialogEx {


public static void main(String[] args) {
JFrame f = new JFrame();

int result = [Link](f, "Do you want to exit?");

if(result == JOptionPane.YES_OPTION) {
[Link](0);
}

[Link](300,300);
1
[Link](null);
[Link](true);
}
}
4. Menu Control (Basic Menu Bar)
import [Link].*;
import [Link].*;

public class MenuEx {


public static void main(String[] args) {
JFrame f = new JFrame("Menu Example");

JMenuBar mb = new JMenuBar();


JMenu m1 = new JMenu("File");

JMenuItem i1 = new JMenuItem("Open");


JMenuItem i2 = new JMenuItem("Exit");

[Link](i1);
[Link](i2);
[Link](m1);

[Link](mb);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](0);
}
});

[Link](400,300);
[Link](null);
[Link](true);
}
}
5. Menu + Dialog Combination
import [Link].*;
import [Link].*;

public class MenuDialogEx {


public static void main(String[] args) {
JFrame f = new JFrame("Menu with Dialog");

2
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("Help");
JMenuItem about = new JMenuItem("About");

[Link](about);
[Link](m);
[Link](mb);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](f, "This is a Swing App");
}
});

[Link](400,300);
[Link](null);
[Link](true);
}
}
ARRAYLIST PROGRAMS
1. ArrayList – Add and Display Elements
import [Link].*;

public class AL1 {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();

[Link]("Apple");
[Link]("Banana");
[Link]("Mango");

[Link]("Elements: " + list);


}
}
2. ArrayList – Remove and Search Element
import [Link].*;

public class AL2 {


public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();

[Link](10);
[Link](20);

3
[Link](30);

[Link](1); // remove index 1

if([Link](20))
[Link]("Found");
else
[Link]("Not Found");

[Link]("List: " + list);


}
}
LINKEDLIST PROGRAMS
3. LinkedList – Add First and Last
import [Link].*;

public class LL1 {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();

[Link]("B");
[Link]("A");
[Link]("C");

[Link]("LinkedList: " + list);


}
}
4. LinkedList – Remove Elements
import [Link].*;

public class LL2 {


public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();

[Link](100);
[Link](200);
[Link](300);

[Link]();
[Link]();

[Link]("After removal: " + list);


}

4
}
TREESET PROGRAMS
5. TreeSet – Store and Sort Elements
import [Link].*;

public class TS1 {


public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();

[Link](50);
[Link](10);
[Link](30);

[Link]("Sorted Set: " + set);


}
}
6. TreeSet – Remove and Check Element
import [Link].*;

public class TS2 {


public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>();

[Link]("Red");
[Link]("Green");
[Link]("Blue");

[Link]("Green");

if([Link]("Green"))
[Link]("Present");
else
[Link]("Not Present");

[Link]("Set: " + set);


}
}

You might also like