Gui

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Lecture Notes: Developing GUIs (javax.

swing)
Q1. Q6.
import javax.swing.*; import javax.swing.*;
class Calculator1{
public static void main(String args[]){ class DemoJPanel extends JFrame{
JFrame cal=new JFrame("Calculator"); JPanel pane;
} DemoJPanel(){
} pane=new JPanel();
add(pane);//add panel to frame
Q2. setTitle("JPanelDemo");//set the title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
import javax.swing.*;
setSize(200,300);
class Calculator2{
setVisible(true);
public static void main(String args[]){
}
JFrame cal=new JFrame("Calculator");
}
cal.setVisible(true); //cal.show();
class MyDemoPanel{
}
public static void main(String []args){
}
new DemoJPanel();
}
Q3. }
import javax.swing.*;
class Calculator{
public static void main(String args[]){
Q7.
import javax.swing.*;
JFrame cal=new JFrame("Calculator");
cal.setSize(350,300);
class DemoLabel extends JFrame{
cal.setVisible(true);
JLabel l;
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
DemoLabel(){
}
l=new JLabel("This is a Label");
add(l);
Q4. setSize(100,100);
import javax.swing.*; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class SimpleJFrame extends JFrame{ show();
SimpleJFrame(){ }
setSize(200,300);//set the size }
show();//show the frame class MyLabel{
} public static void main(String []args){
} new DemoLabel();
class DemoMySimpleFrame{ }
public static void main(String []args){ }
new SimpleJFrame();
}
}
Q8.
import javax.swing.*;
import java.awt.*;
Q5.
import javax.swing.*; class DemoLabel extends JFrame{
JLabel l;
class DemoJFrame extends JFrame{
DemoJFrame(){ DemoLabel(){
super("JFrameDemo");//set the title l=new JLabel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); l.setText("This is a Label");
setSize(200,300); add(l);
setVisible(true);//show the frame setSize(100,100);
} show();
} }
class DemoMySimpleFrame{ public static void main(String []args){
public static void main(String []args){ new DemoLabel();
new DemoJFrame(); }
} }
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 1


Lecture Notes: Developing GUIs (javax.swing)
Q9. Q12.
import javax.swing.*; import javax.swing.*;
import java.awt.*; class DemoJLabel extends JFrame{
JLabel lbl1;
class DemoLabel extends JFrame{ JLabel lbl2;
JLabel l; JPanel pane;

DemoLabel(){ DemoJLabel(){
l=new JLabel("Label",new ImageIcon("im2.jpg"),2); lbl1=new JLabel("My Label");
add(l); lbl2=new JLabel("Your Label");
setSize(200,200); pane=new JPanel();
show();
} pane.add(lbl1);//add label to pane
} pane.add(lbl2);//add label to pane
class MyDemoLabel{
public static void main(String []args){ getContentPane().add(pane);//add panel to frame
new DemoLabel(); //add(pane);
} setTitle("JLabelDemo");
} setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Q10. setVisible(true);
}
import javax.swing.*;
}
class DemoLabel extends JFrame{
class MyDemoLabel{
JLabel l;
public static void main(String []args){
DemoJLabel jlabel=new DemoJLabel();
DemoLabel(){
}
l=new JLabel("Label");
}
l.setIcon(new ImageIcon("im2.jpg"));
l.setHorizontalTextPosition(JLabel.LEFT);
add(l); Q13.
pack(); //Auto arrange import javax.swing.*;
show(); class GoodLabel extends JFrame{
} JPanel pane;
public static void main(String []args){ JLabel lbl;
new DemoLabel();
} GoodLabel(){
} pane=new JPanel();
lbl=new JLabel();
Q11.
import javax.swing.*; lbl.setText("Good");//display the message
class DemoJLabel extends JFrame{ lbl.setIcon(new ImageIcon("icon.gif"));//set Icon
JLabel lbl1;
JLabel lbl2; pane.add(lbl);

DemoJLabel(){ getContentPane().add(pane);
lbl1=new JLabel("My Label"); setTitle("JLabelDemo");
lbl2=new JLabel("Your Label"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(lbl1);//add label to Jframe setSize(200,100);
add(lbl2);//add label to Jframe setVisible(true);
}
setTitle("JLabelDemo"); }
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); class DemoGoodLabel{
pack(); public static void main(String []args){
setVisible(true); GoodLabel jlabel=new GoodLabel();
} }
}
}
class MyDemoLabel{
public static void main(String []args){
DemoJLabel jlabel=new DemoJLabel();
}
}

2 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q14. Q17.
import javax.swing.*; import javax.swing.*;
class Calculator{ class DemoJButton extends JFrame{
public static void main(String args[]){ JPanel pane;
JFrame cal=new JFrame("Calculator"); JButton yes;
JButton b1=new JButton("Yes"); JButton no;
JButton b2=new JButton("No"); JButton cancel;

cal.add(b1); DemoJButton(){
cal.add(b2); pane=new JPanel();
cal.setSize(350,300); yes=new JButton("Yes");
cal.setVisible(true); no=new JButton("No");
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cancel=new JButton("Cancel");
}
} pane.add(yes);
pane.add(no);
Q15. pane.add(cancel);
import javax.swing.*;
getContentPane().add(pane);
setTitle("JButtonDemo");
class Calculator{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String args[]){
pack();
JFrame cal=new JFrame("Calculator");
setVisible(true);
JButton b1=new JButton("Yes");
}
JButton b2=new JButton("No");
}
class DemoButton{
JPanel pane=new JPanel();
public static void main(String []args){
pane.add(b1); //Adds Button to the JFram Container
DemoJButton jButton=new DemoJButton();
pane.add(b2);
}
cal.getContentPane().add(pane);
}
cal.setSize(350,300);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cal.setVisible(true); Q18.
} import javax.swing.*;
} class GoodJButton extends JFrame{
JPanel pane;
Q16. JButton btn;
import javax.swing.*;
GoodJButton(){
import java.awt.*;
pane=new JPanel();
class Calculator{
btn=new JButton();
public static void main(String args[]){
JFrame cal=new JFrame("Calculator");
btn.setText("Good");
JButton b1=new JButton("Yes");
btn.setIcon(new ImageIcon("icon.gif"));
JButton b2=new JButton("No");
pane.add(btn);
cal.setLayout(new FlowLayout());
//java.awt.FlowLayout
getContentPane().add(pane);
cal.add(b1); //Adds Button to the JFram Container
setTitle("JButtonDemo");
cal.add(b2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cal.setSize(350,300);
pack();
cal.setVisible(true);
setVisible(true);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
public static void main(String []args){
GoodJButton jbutton=new GoodJButton();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 3


Lecture Notes: Developing GUIs (javax.swing)
Q19. Q21. Introducing JTextField
import javax.swing.*; import javax.swing.*;
class DemoJTextField extends JFrame{ class JTextFieldDemo extends JFrame {
JPanel pane; JTextField name, Address, City;
JTextField txt;
public JTextFieldDemo() {
DemoJTextField(){ super("Text Filed Frame");
pane=new JPanel(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt=new JTextField("My TextField"); JPanel panel = new JPanel();
JTextField name=new JTextField("This is a text field",20);
pane.add(txt); name.setEditable(false);
getContentPane().add(pane); JLabel address=new JLabel("Address");
setTitle("DemoJTextField"); JLabel city=new JLabel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon labelIcon = new ImageIcon("MR.gif");
setSize(200,100); JLabel sex=new
setVisible(true); JLabel("Sex",labelIcon,SwingConstants.LEFT);
}
panel.add(name);
public static void main(String []args){ panel.add(address);
new DemoJTextField(); panel.add(city);
} panel.add(sex);
} // add the panel to a frame
add(panel);
Q20. pack();
setVisible(true);
import javax.swing.*;
}
class JTextFieldDemo extends JFrame {
JTextField name, Address, City;
public static void main(String[] arguments) {
new JTextFieldDemo();
public JTextFieldDemo() {
}
super("Text Filed Frame");
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextField name=new JTextField(); Q22. Introducing JTextArea
name.setColumns(8); import javax.swing.*;
name.setText("Name"); class DemoJTextArea extends JFrame{
JLabel address=new JLabel("Address"); JPanel pane;
JLabel city=new JLabel(); JTextArea txt;
ImageIcon labelIcon = new ImageIcon("MR.gif");
JLabel sex=new DemoJTextArea(){
JLabel("Sex",labelIcon,SwingConstants.LEFT); pane=new JPanel();
txt=new JTextArea(20,30);//inizialize the size
panel.add(name);
panel.add(address); pane.add(txt);
panel.add(city);
panel.add(sex); getContentPane().add(pane);
// add the panel to a frame setTitle("Notepad");
add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); setSize(400,400);
setVisible(true); setVisible(true);
} }
public static void main(String []args){
public static void main(String[] arguments) { new DemoJTextArea();
new JTextFieldDemo(); }
} }
}

4 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q23. Set to JScrollPane Q25. Demo RadioButton
import javax.swing.*; import java.awt.*;
class GoodJTextArea extends JFrame{ import javax.swing.*;
JPanel pane;
JScrollPane sp; class DemoRadioButton extends JFrame {
JTextArea txt; JPanel p;
JRadioButton plain, bold, italic, boldItalic;
GoodJTextArea(){
pane=new JPanel(); DemoRadioButton(){
txt=new JTextArea(20,30); super( "RadioButton Test" );
sp=new JScrollPane(txt);//add textarea to th scrollpane p=new JPanel();

pane.add(sp); // Create radio buttons


plain = new JRadioButton( "Plain", true );
getContentPane().add(pane); p.add( plain );
setTitle("JLabelDemo"); bold = new JRadioButton( "Bold", false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.add( bold );
setSize(400,400); italic = new JRadioButton( "Italic", false );
setVisible(true); p.add( italic );
} boldItalic = new JRadioButton( "Bold/Italic", false );
} p.add( boldItalic );
class Demo{
public static void main(String []args){ add(p);
GoodJTextArea jTextArea=new GoodJTextArea(); pack();
} show();
} }
public static void main( String args[] ){
Q24. Demo JTextArea Append Text DemoRadioButton rd = new DemoRadioButton();
}
import javax.swing.*;
}
class GoodJTextArea extends JFrame{
JPanel pane;
JScrollPane sp; Q26. Demo CheckBoxes
JTextArea txt; import javax.swing.*;
class DemoCheckBox extends JFrame {
GoodJTextArea(){ JPanel p;
pane=new JPanel(); JCheckBox plain, bold, italic, boldItalic;
txt=new JTextArea(20,30);
txt.append("IJTS"); DemoCheckBox(){
sp=new JScrollPane(txt);//add textarea to th scrollpane super( "RadioButton Test" );
p=new JPanel();
pane.add(sp); // Create JCheckBoxes
txt.append("\nSun Cetified Java Programmer"); plain = new JCheckBox( "Plain");
getContentPane().add(pane); p.add( plain );
setTitle("JLabelDemo"); bold = new JCheckBox( "Bold");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.add( bold );
setSize(400,400); italic = new JCheckBox( "Italic");
setVisible(true); p.add( italic );
} boldItalic = new JCheckBox( "Bold/Italic");
p.add( boldItalic );
public static void main(String []args){
GoodJTextArea jTextArea=new GoodJTextArea(); add(p);
} pack();
} show();
}

public static void main( String args[] ){


DemoCheckBox ch = new DemoCheckBox();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 5


Lecture Notes: Developing GUIs (javax.swing)
Q27. Demo CheckBoxes Q29. Demonstrate JComboBox
import javax.swing.*; import java.awt.*;
class DemoCheckBox extends JFrame { import javax.swing.*;
JPanel p;
JCheckBox plain, bold, italic, boldItalic; class DemoComboBox extends JFrame {
JPanel p;
DemoComboBox() {
DemoCheckBox(){ super("ComboBox Example");
super( "RadioButton Test" ); p=new JPanel();

p=new JPanel(); JComboBox combo = new JComboBox();


plain = new JCheckBox( "Plain", true );
p.add( plain ); p.add(combo);
bold = new JCheckBox( "Bold", false); getContentPane().add(p);
p.add( bold ); pack();
italic = new JCheckBox( "Italic", false ); setVisible(true);
p.add( italic ); }
boldItalic = new JCheckBox( "Bold/Italic", false );
p.add( boldItalic ); public static void main (String args[]) {
DemoComboBox com=new DemoComboBox();
add(p); }
setSize( 300, 100 ); }
show();
}
Q30. Demonstrate JComboBox
public static void main( String args[] ){
import java.awt.*;
DemoCheckBox ch = new DemoCheckBox();
import javax.swing.*;
}
}
class DemoComboBox extends JFrame {
JPanel p;
Q28. CheckBox, RadioButton String[] items = {"Kamal" ,"Ajith" ,"Nalin" };
import javax.swing.*;
import java.awt.*; DemoComboBox() {
class DemoCheckBox extends JFrame { super("ComboBox Example");
JPanel p; p=new JPanel();
JCheckBox plain, bold, italic, boldItalic; JComboBox combo = new JComboBox(items);

DemoCheckBox(){ p.add(combo);
super( "RadioButton Test" ); getContentPane().add(p);
p=new JPanel(); setSize(200, 140);
setVisible(true);
plain = new JCheckBox( "Plain"); }

plain.setIcon(new ImageIcon("im2.gif")); public static void main (String args[]) {


DemoComboBox com=new DemoComboBox();
p.add( plain ); }
bold = new JCheckBox( "Bold"); }
p.add( bold );
italic = new JCheckBox( "Italic");
p.add( italic );
boldItalic = new JCheckBox( "Bold/Italic");
p.add( boldItalic );

add(p);
setSize( 300, 100 );
show();
}
public static void main( String args[] ){
DemoCheckBox ch = new DemoCheckBox();
}
}

6 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q31. Demonstrate JComboBox Q33. Demonstrate JComboBox
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
class DemoComboBox extends JFrame {
class DemoComboBox extends JFrame { JPanel p;
JPanel p; DemoComboBox() {
super("ComboBox Example");
DemoComboBox() { p=new JPanel();
super("ComboBox Example");
p=new JPanel(); JComboBox combo = new JComboBox();
JComboBox combo = new JComboBox();
combo.addItem("A");
combo.addItem("A"); combo.addItem("B");
combo.addItem("B"); combo.addItem("C");
combo.addItem("C"); combo.addItem("D");
combo.addItem("D"); combo.addItem("E");
combo.addItem("E"); combo.addItem("F");
combo.addItem("F"); combo.addItem("G");
combo.addItem("H");
p.add(combo); combo.addItem("I");
getContentPane().add(p); combo.addItem("J");
setSize(200, 140); combo.setSelectedIndex(3);
setVisible(true); p.add(combo);
} getContentPane().add(p);

public static void main (String args[]) { setSize(200, 140);


DemoComboBox com=new DemoComboBox(); setVisible(true);
} }
}
public static void main (String args[]) {
Q32. Demonstrate JComboBox DemoComboBox com=new DemoComboBox();
}
import java.awt.*;
}
import javax.swing.*;
class DemoComboBox extends JFrame {
JPanel p; Q34. Demonstrate JListBox
DemoComboBox() { import java.awt.*;
super("ComboBox Example"); import javax.swing.*;
p=new JPanel();
JComboBox combo = new JComboBox(); class DemoList extends JFrame{
combo.addItem("A"); JPanel p;
combo.addItem("B"); JList list;
combo.addItem("C");
combo.addItem("D"); DemoList(){
combo.addItem("E"); p=new JPanel();
combo.addItem("F"); String[] words
combo.addItem("G"); ={"EEE","DDD","SSS","AAA","bb","CC","RR","PP","AAA","bb
combo.addItem("H"); ","CC"};
combo.addItem("I"); list = new JList(words);
combo.addItem("J"); p.add(list);
combo.setMaximumRowCount(2); getContentPane().add(p);
p.add(combo); setTitle("ListTest");
getContentPane().add(p); setSize(100, 200);
setSize(200, 140); show();
setVisible(true); }
} public static void main(String []args){
new DemoList();
public static void main (String args[]) { }
DemoComboBox com=new DemoComboBox(); }
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 7


Lecture Notes: Developing GUIs (javax.swing)
Q35. Demonstrate JListBox Q37. Demonstrate passowrdJComboBox
import java.awt.*; import javax.swing.*;
import javax.swing.*;
class Password extends JFrame {
class DemoList extends JFrame{ JPasswordField pass;
JPanel p;
JList list; Password(){
JScrollPane scrollPane; pass=new JPasswordField(5);
add(pass);
DemoList(){ setSize(100,50);
p=new JPanel(); show();
String[] words }
={"www","rrr","ttt","ef","AAA","bb","CC","www","rrr","ttt","jjj", public static void main(String []args){
"AAA","bb","CC"}; new Password();
list = new JList(words); }
scrollPane = new JScrollPane(list); }
p.add(scrollPane);
getContentPane().add(p);
Q38. Demonstrate JPassword
setTitle("ListTest");
import javax.swing.*;
setSize(100, 200);
class AskPassword extends JFrame {
show();
PasswordFrame pass = new PasswordFrame();
}
public AskPassword() {
public static void main(String []args){
super("Ask Password");
new DemoList();
setSize(540, 80);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
setVisible(true);
pass.setVisible(true);
Q36. }
import javax.swing.*;
class Subscriptions extends JFrame { public static void main(String[] arguments) {
String[] subs = { "0xDECAFBAD", "Cafe au Lait", AskPassword ask = new AskPassword();
"Hack the Planet", "Ideoplex", "Inessential", "Markpasc", }
"Postneo", "RC3", "Scripting News", "Workbench" }; }
JList subList = new JList(subs); class PasswordFrame extends JFrame {
public PasswordFrame() {
public Subscriptions() { super("Password");
super("Subscriptions"); setSize(210, 130);
setSize(150, 300); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(); JPanel pane = new JPanel();
JLabel subLabel = new JLabel("RSS Subscriptions:"); JLabel usernameLabel = new JLabel("Username: ");
panel.add(subLabel); JTextField username = new JTextField(8);
subList.setVisibleRowCount(8); JLabel passwordLabel = new JLabel("Password: ");
JScrollPane scroller = new JScrollPane(subList); JPasswordField password = new JPasswordField(8);
panel.add(scroller); pane.add(usernameLabel);
add(panel); pane.add(username);
setVisible(true); pane.add(passwordLabel);
} pane.add(password);
setContentPane(pane);
public static void main(String[] arguments) { setVisible(false);
Subscriptions app = new Subscriptions(); }
} }
}

8 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q39. JOptionPane –Message Dialog Q42. JOptionPane –Input Dialog
static void showMessageDialog(Component parentComponent, static String showInternalInputDialog(Component
Object message) parentComponent, Object message)

import javax.swing.*;
class DemoJOptionPane1{ import javax.swing.*;
public static void main(String []args){ class DemoJOptionPane1{
JOptionPane.showMessageDialog(null, "Press Ok"); public static void main(String []args){
} ImageIcon image=new ImageIcon("Wow.gif");
} String num1=JOptionPane.showInputDialog(null, "Input First
Numer");
String num2=JOptionPane.showInputDialog(null, "Input
Q40. JOptionPane –Message Dialog Second Numer");
static void showMessageDialog(Component parentComponent, int x1=Integer.parseInt(num1);
Object message, String title,
int messageType) int x2=Integer.parseInt(num2);

int tot=x1+x2;
import javax.swing.*; String message="Total :"+tot;
class DemoJOptionPane1{ JOptionPane.showMessageDialog(null, message);
public static void main(String []args){ }
JOptionPane.showMessageDialog(null, "Press Ok","This is }
messagebox",0);
}
} Q43. JOptionPane –Input Dialog
static String showInputDialog(Component
parentComponent, Object message,
Type of message boxes : Object initialSelectionValue)
JOptionPane.ERROR_MESSAGE - 0
JOptionPane.PLAIN_MESSAGE - -1
JOptionPane.QUESTION_MESSAGE - import javax.swing.*;
JOptionPane.WARNING_MESSAGE - 2 class DemoJOptionPane1{
JOptionPane.INFORMATION_MESSAGE - 1 public static void main(String []args){
ImageIcon image=new ImageIcon("Wow.gif");
String num1=JOptionPane.showInputDialog(null, "Input First
Q41. JOptionPane –Message Dialog Numer","000");
showInternalMessageDialog(Component parentComponent, String num2=JOptionPane.showInputDialog(null, "Input
Object message, String title, int messageType, Icon icon) Second Numer","000");
int x1=Integer.parseInt(num1);
int x2=Integer.parseInt(num2);
import javax.swing.*;
class DemoJOptionPane1{
int tot=x1+x2;
public static void main(String []args){
String message="Total :"+tot;
ImageIcon image=new ImageIcon("Wow.gif");
JOptionPane.showMessageDialog(null, message);
JOptionPane.showMessageDialog(null, "alert", "alert",
}
JOptionPane.ERROR_MESSAGE,image);
}
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 9


Lecture Notes: Developing GUIs (javax.swing)
Q44. JOptionPane –Message Dialog Q47. JOptionPane –Confirm Dialog
public static String import javax.swing.*;
showInputDialog(Component parentComponent,
Object message,String title, int messageType)
class DemoJOptionPane2{
public static void main(String []args){
int res=JOptionPane.showConfirmDialog(null,"choose
import javax.swing.*;
one","choose one", JOptionPane.YES_NO_CANCEL_OPTION );
class DemoJOptionPane1{
if(res==JOptionPane.YES_OPTION)
public static void main(String []args){ JOptionPane.showMessageDialog(null, "You pressed Yes
ImageIcon image=new ImageIcon("Wow.gif");
Button");
String num1=JOptionPane.showInputDialog(null, "Input First
if(res==JOptionPane.NO_OPTION)
Numer","Number Input Box",2); JOptionPane.showMessageDialog(null, "You pressed No
String num2=JOptionPane.showInputDialog(null, "Input
Button");
Second Numer","Number Input Box",2); if(res==JOptionPane.CANCEL_OPTION)
int x1=Integer.parseInt(num1); JOptionPane.showMessageDialog(null, "You pressed
int x2=Integer.parseInt(num2);
Canecl Button");
}
int tot=x1+x2; }
String message="Total :"+tot;
JOptionPane.showMessageDialog(null, message);
} Q48.
} import javax.swing.*;
class DemoJOptionPane3{
Q45. JOptionPane –Confirm Dialog public static void main(String []args){
import javax.swing.*; Object[] options = { "OK", "CANCEL","Cancel","Ok" };
class DemoJOptionPane2{ JOptionPane.showOptionDialog(null, "Click OK to continue",
public static void main(String []args){ "Warning",JOptionPane.DEFAULT_OPTION,
JOptionPane.showConfirmDialog(null,"choose one"); JOptionPane.WARNING_MESSAGE,null, options, options[0]);
}
} }
}

Q46. JOptionPane –Confirm Dialog


import javax.swing.*; Q49.
class DemoJOptionPane2{ import java.awt.event.*;
public static void main(String []args){ import javax.swing.*;
int res=JOptionPane.showConfirmDialog(null,"choose one"); class TitleFrame extends JFrame {
if(res==0) public TitleFrame() {
JOptionPane.showMessageDialog(null, "You pressed Yes super("Title Frame");
Button"); setSize(300, 200);
if(res==1) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(null, "You pressed No setVisible(true);
Button"); String response = JOptionPane.showInputDialog(this,"Enter a
if(res==2) Title for the Frame:");
JOptionPane.showMessageDialog(null, "You pressed setTitle(response);
Canecl Button"); }
} public static void main(String[] arguments) {
} JFrame frame = new TitleFrame();
}
}

Q50.
import javax.swing.*;

class DemoJOptionPane5{
public static void main(String []args){
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE,
null,possibleValues, possibleValues[0]);
}
}

10 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q51. Q52. Introducing JMenus
import java.awt.GridLayout; import javax.swing.*;
import javax.swing.*;
class FeedInfo extends JFrame { class DemoMenu extends JFrame{
private JLabel nameLabel = new JLabel("Name: ", JPanel pane;
SwingConstants.RIGHT); JMenuBar mb;
private JTextField name;
private JLabel urlLabel = new JLabel("URL: ", JMenu mFile;
SwingConstants.RIGHT); JMenu mEdit;
private JTextField url; JMenu mSearch;
private JLabel typeLabel = new JLabel("Type: ",
SwingConstants.RIGHT); DemoMenu(){
private JTextField type; pane=new JPanel();
public FeedInfo() { mb=new JMenuBar();
super("Feed Information");
setSize(400, 105); mFile=new JMenu("File");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mEdit=new JMenu("Edit");
setLookAndFeel(); mSearch=new JMenu("Search");
// Site name
String response1 = JOptionPane.showInputDialog(null, mb.add(mEdit);
"Enter the site name:"); mb.add(mFile);
name = new JTextField(response1, 20); mb.add(mSearch);
// Site address
String response2 = JOptionPane.showInputDialog(null, pane.add(mb);
"Enter the site address:"); getContentPane().add(pane);
url = new JTextField(response2, 20); setTitle("JLabelDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Site type setSize(300,200);
String[] choices = { "Personal", "Commercial", "Unknown" }; setVisible(true);
int response3 = JOptionPane.showOptionDialog(null, }
"What type of site is it?",
"Site Type", public static void main(String []args){
0, DemoMenu mnu=new DemoMenu();
JOptionPane.QUESTION_MESSAGE, }
null, }
choices,
choices[0]);
type = new JTextField(choices[response3], 20);
setLayout(new GridLayout(3, 2));
add(nameLabel);
add(name);
add(urlLabel);
add(url);
add(typeLabel);
add(type);
setLookAndFeel();
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println("Couldn't use the system "
+ "look and feel: " + e);
}
}
public static void main(String[] arguments) {
FeedInfo frame = new FeedInfo();
}
}
©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 11
Lecture Notes: Developing GUIs (javax.swing)
Q53. JMenu Q54. JMenu
import javax.swing.*; import javax.swing.*;
import java.awt.*;
class DemoMenu extends JFrame{ class GoodMenu extends JFrame{
JPanel pane; JPanel pane;
JMenuBar mb; JMenuBar mb;

JMenu mFile; JMenu mFile;


JMenu mEdit; JMenu mEdit;
JMenu mSearch; JMenu mSearch;

JMenuItem fNew; JMenuItem fNew;


JMenuItem fOpen; JMenuItem fOpen;
JMenuItem fExit; JMenuItem fExit;

JMenuItem eCopy; JMenuItem eCopy;


JMenuItem ePaste; JMenuItem ePaste;
JMenuItem eDelete; JMenuItem eDelete;

JMenuItem sFind; JMenuItem sFind;

DemoMenu(){ GoodMenu(){
pane=new JPanel(); pane=new JPanel();
mb=new JMenuBar();
mb=new JMenuBar();
mFile=new JMenu("File");
fNew=new JMenuItem("New"); mFile=new JMenu("File");
fOpen=new JMenuItem("Open"); fNew=new JMenuItem("New");
fExit=new JMenuItem("Exit"); fOpen=new JMenuItem("Open");
fExit=new JMenuItem("Exit");
mFile.add(fNew);
mFile.add(fOpen); mFile.setMnemonic(KeyEvent.VK_F);
mFile.add(fExit);
fNew.setMnemonic(KeyEvent.VK_N);
mEdit=new JMenu("Edit"); fOpen.setMnemonic(KeyEvent.VK_O);
eCopy=new JMenuItem("Copy"); fExit.setMnemonic(KeyEvent.VK_E);
ePaste=new JMenuItem("Paste");
eDelete=new JMenuItem("Delete"); fNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N
,Event.CTRL_MASK));
mEdit.add(eCopy); fOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_
mEdit.add(ePaste); O,Event.CTRL_MASK));
mEdit.add(eDelete); fExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
Event.CTRL_MASK));
mSearch=new JMenu("Search");
sFind=new JMenuItem("Find"); mFile.add(fNew);
mFile.add(fOpen);
mSearch.add(sFind); mFile.add(fExit);
mb.add(mEdit);
mb.add(mFile); mEdit=new JMenu("Edit");
mb.add(mSearch); eCopy=new JMenuItem("Copy");
ePaste=new JMenuItem("Paste");
setJMenuBar(mb); //set JMenuBar to JFrame eDelete=new JMenuItem("Delete");

setTitle("JLabelDemo"); mEdit.setMnemonic(KeyEvent.VK_E);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); eCopy.setMnemonic(KeyEvent.VK_C);
setSize(200,100); ePaste.setMnemonic(KeyEvent.VK_P);
setVisible(true); eDelete.setMnemonic(KeyEvent.VK_D);
}
public static void main(String []args){
DemoMenu mnu=new DemoMenu();
}
}

12 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
eCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Q56.
Event.CTRL_MASK)); import java.awt.*;
ePaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P import javax.swing.*;
,Event.CTRL_MASK)); import javax.swing.table.*;
eDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_
D,Event.CTRL_MASK)); class DemoTable extends JFrame{
DemoTable(){
mEdit.add(eCopy);
mEdit.add(ePaste); setTitle("Table");
mEdit.add(eDelete); setSize(400 , 200);
mSearch=new JMenu("Search"); JTable table = new JTable(cells ,columnNames);
sFind=new JMenuItem("Find");
mSearch.setMnemonic(KeyEvent.VK_S); getContentPane().add(new JScrollPane(table));
}
sFind.setMnemonic(KeyEvent.VK_V);
private Object[][] cells ={
sFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V {"Amal", "Walisara", new Integer(38),"Male"},
,Event.CTRL_MASK)); {"Kamal", "Miliya", new Integer(55),"Male" },
{"Wasana", "Panadura", new Integer(66),"Female"},
mSearch.add(sFind); {"Gihan", "Wel", new Integer(18),"Male"},
mb.add(mEdit); {"Nimal", "Kalutara", new Integer(25),"Male"},
mb.add(mFile); };
mb.add(mSearch); private String[] columnNames ={
"Name", "Address", "Age", "Sex"
setJMenuBar(mb); };
}
setTitle("Demonstrate Menu"); class PlanetTable{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String[] args){
setSize(400,200); JFrame frame = new DemoTable();
setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} frame.show();
}
public static void main(String []args){ }
GoodMenu mnu=new GoodMenu();
}
}
Q57. Introducing Window
import javax.swing.*;
Q55. Inroducing Table class DemoWindow {
import javax.swing.*;
public static void main(String []args){
class SimpleTable extends JFrame{
JWindow win=new JWindow();
JTable t1;
win.setVisible(true);
JPanel p1;
}
}
SimpleTable(){
p1=new JPanel();
t1=new JTable(3,3);
p1.add(t1);
add(p1);
setSize(200,100);
show();
}
public static void main(String []args){
new SimpleTable();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 13


Lecture Notes: Developing GUIs (javax.swing)
Q58. Introducing Window Q62. Introducing Progressbar
import javax.swing.*; import java.awt.*;
import java.awt.*; import javax.swing.*;
class Progress extends JFrame {
class DemoWindow extends JWindow{ JProgressBar current;
DemoWindow(){ JTextArea out;
setSize(200,200); JButton find;
show(); Thread runner;
} int num = 0;

void visible(){ public Progress() {


hide(); super("Progress");
} setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String []args){ setLayout(new FlowLayout());
DemoWindow d= new DemoWindow(); current = new JProgressBar(0, 2000);
try{ current.setValue(0);
Thread.sleep(2000); current.setStringPainted(true);
}catch(Exception e){ } add(current);
d.visible(); }
} public void iterate() {
} while (num < 2000) {
current.setValue(num);
Q59. Introducing Window try {
Thread.sleep(1000);
import javax.swing.*;
} catch (InterruptedException e) { }
class DemoWindow {
num += 95;
public static void main(String []args){
}
JWindow win=new JWindow();
}
win.setSize(100,100);
win.setVisible(true);
public static void main(String[] arguments) {
}
Progress frame = new Progress();
}
frame.pack();
frame.setVisible(true);
Q60. Introducing Internal Frame frame.iterate();
import javax.swing.*; }
class DemoInternalFrame{ }
public static void main(String []args){
JFrame f=new JFrame();
JInternalFrame inter=new JInternalFrame();
f.add(inter);
inter.show();
f.show();
}
}

Q61. Introducing Internal Frame


import javax.swing.*;
class DemoInternalFrame extends JFrame{
JInternalFrame inter;

DemoInternalFrame(){
inter=new JInternalFrame("inter",true,true);
add(inter);
inter.show();
setSize(400,400);
show();
}
public static void main(String []args){
DemoInternalFrame d=new DemoInternalFrame();
}
}

14 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q63. Introducing Progressbar Q64. Introducing Progressbar
import java.awt.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.*;

class Progress2 extends JFrame { class Slider extends JFrame {


public Slider() {
JProgressBar current; super("Slider");
JTextField numField; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton find; JSlider pickNum = new JSlider(JSlider.HORIZONTAL, 0,
Thread runner; 30, 5);
int num = 0; pickNum.setMajorTickSpacing(10);
pickNum.setMinorTickSpacing(1);
public Progress2() { pickNum.setPaintTicks(true);
super("Progress2"); pickNum.setPaintLabels(true);
setSize(190, 80); add(pickNum);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout()); public static void main(String[] args) {
numField = new JTextField("" + num, 8); Slider frame = new Slider();
pane.add(numField); frame.pack();
current = new JProgressBar(0, 2000); frame.setVisible(true);
current.setValue(0); }
current.setStringPainted(true); }
pane.add(current);
setContentPane(pane);
Q65.
setVisible(true);
import javax.swing.*;
}
class Tabbed extends JFrame{
public void iterate() {
JTabbedPane tb;
while (num < 2000) {
current.setValue(num);
Tabbed(){
numField.setText("" + num);
tb=new JTabbedPane();
try { Thread.sleep(1000);
add(tb);
}catch (InterruptedException e) { }
setSize(400,400);
num += 95;
setVisible(true);
}
}
}
public static void main(String []args){
public static void main(String[] arguments) {
new Tabbed();
Progress2 frame = new Progress2();
}
frame.iterate();
}
}
}

Q66.
import javax.swing.*;
class Tabbed extends JFrame{
JTabbedPane tb;

Tabbed(){
tb=new JTabbedPane();
tb.addTab("A",new JButton("aaa"));
tb.addTab("B",new JButton("bbb"));
tb.addTab("C",new JButton("ccc"));
add(tb);

setSize(400,400);
setVisible(true);
}
public static void main(String []args){
new Tabbed();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 15


Lecture Notes: Developing GUIs (javax.swing)
Q67. Q69. Event handling (ActionEvent)
import javax.swing.*; import java.awt.event.*;
class Tabbed extends JFrame{ import javax.swing.*;
JTabbedPane tb;
class MyEvent implements ActionListener{
Tabbed(){ public void actionPerformed(ActionEvent e){
tb=new JTabbedPane(); System.exit(0);
tb.addTab("A",new JButton("aaa")); }
tb.addTab("B",new JButton("bbb")); }
tb.addTab("C",new JButton("ccc")); class Demo extends JFrame{
tb.setTabPlacement(JTabbedPane.LEFT); JPanel p;
add(tb); JButton btn;
setSize(400,400);
setVisible(true); Demo(){
} p=new JPanel();
public static void main(String []args){ btn=new JButton("Exit");
new Tabbed(); btn.addActionListener(new MyEvent());
} p.add(btn);
} add(p);
setSize(400,400);
Q68. Introducing event handling show();
}
import java.awt.event.*;
import javax.swing.*;
public static void main(String []args){
new Demo();
class Event1 implements ActionListener{
}
public void actionPerformed(ActionEvent e){
}
JOptionPane.showMessageDialog(null, "Thanks, you r
learning event handling..");
}

}
class Demo extends JFrame{
JPanel p;
JButton btn;

Demo(){
p=new JPanel();
btn=new JButton("Press");
btn.addActionListener(new Event1());
p.add(btn);
add(p);
setSize(100,100);
show();
}

public static void main(String []args){


new Demo();
}
}

16 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q70. Q71. Compile Error
import java.awt.event.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.*;
class MyYesEvent implements ActionListener{
public void actionPerformed(ActionEvent e){ class MyButtonAction implements ActionListener{
JOptionPane.showMessageDialog(null, "You pressed Yes public void actionPerformed(ActionEvent evt){
Button"); if(evt.getSource()==yes)
} JOptionPane.showMessageDialog(null, "You pressed Yes
} Button");
class MyNoEvent implements ActionListener{ if(evt.getSource()==no)
public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null, "You pressed No
JOptionPane.showMessageDialog(null, "You pressed No Button");
Button"); if(evt.getSource()==cancel)
} JOptionPane.showMessageDialog(null, "You pressed
} Cancel Button");
class MyCancelEvent implements ActionListener{ if(evt.getSource()==exit)
public void actionPerformed(ActionEvent e){ System.exit(0);
JOptionPane.showMessageDialog(null, "You pressed Cancel }
Button"); }
}
}
class MyExitEvent implements ActionListener{ class Demo extends JFrame{
public void actionPerformed(ActionEvent e){ JPanel p;
System.exit(0); JButton yes;
} JButton no;
} JButton cancel;
JButton exit;
class Demo extends JFrame{
JPanel p; Demo(){
JButton yes; p=new JPanel();
JButton no; yes=new JButton("Yes");
JButton cancel; no=new JButton("No");
JButton exit; cancel=new JButton("Cancel");
exit=new JButton("Exit");
Demo(){
p=new JPanel(); MyButtonAction bt=new MyButtonAction();
yes=new JButton("Yes"); yes.addActionListener(bt);
no=new JButton("No"); no.addActionListener(bt);
cancel=new JButton("Cancel"); cancel.addActionListener(bt);
exit=new JButton("Exit"); exit.addActionListener(bt);

yes.addActionListener(new MyYesEvent()); p.add(yes);


no.addActionListener(new MyNoEvent()); p.add(no);
cancel.addActionListener(new MyCancelEvent()); p.add(cancel);
exit.addActionListener(new MyExitEvent()); p.add(exit);

p.add(yes); add(p);
p.add(no); setSize(400,400);
p.add(cancel); show();
p.add(exit); }

add(p); public static void main(String []args){


setSize(400,400); new Demo();
show(); }
} }

public static void main(String []args){


new Demo();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 17


Lecture Notes: Developing GUIs (javax.swing)
Q72. Introducing Inner-classes Q73. Using Anonymous Inner Class
import java.awt.event.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.*;
class Demo extends JFrame{ class Demo extends JFrame{
JPanel p; JPanel p;
JButton yes; JButton yes;
JButton no; JButton no;
JButton cancel; JButton cancel;
JButton exit; JButton exit;

Demo(){ Demo(){
p=new JPanel(); p=new JPanel();
yes=new JButton("Yes"); yes=new JButton("Yes");
no=new JButton("No"); no=new JButton("No");
cancel=new JButton("Cancel"); cancel=new JButton("Cancel");
exit=new JButton("Exit"); exit=new JButton("Exit");

MyButtonAction bt=new MyButtonAction(); yes.addActionListener(new ActionListener(){


yes.addActionListener(bt); public void actionPerformed(ActionEvent evt){
no.addActionListener(bt); JOptionPane.showMessageDialog(null, "You
cancel.addActionListener(bt); pressed Yes Button");
exit.addActionListener(bt); }
});
p.add(yes); no.addActionListener(new ActionListener(){
p.add(no); public void actionPerformed(ActionEvent evt){
p.add(cancel); JOptionPane.showMessageDialog(null, "You
p.add(exit); pressed No Button");
}
add(p); });
setSize(400,400); cancel.addActionListener(new ActionListener(){
show(); public void actionPerformed(ActionEvent evt){
} JOptionPane.showMessageDialog(null, "You
class MyButtonAction implements ActionListener{ pressed Cancel Button");
public void actionPerformed(ActionEvent evt){ }
if(evt.getSource()==yes) });
JOptionPane.showMessageDialog(null, "You pressed exit.addActionListener(new ActionListener(){
Yes Button"); public void actionPerformed(ActionEvent evt){
if(evt.getSource()==no) System.exit(0);
JOptionPane.showMessageDialog(null, "You pressed }
No Button"); });
if(evt.getSource()==cancel)
JOptionPane.showMessageDialog(null, "You pressed p.add(yes);
Cancel Button"); p.add(no);
if(evt.getSource()==exit) p.add(cancel);
System.exit(0); p.add(exit);
}
} add(p);
setSize(400,400);
show();
public static void main(String []args){ }
new Demo();
} public static void main(String []args){
} new Demo();
}
}

18 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q74. MouseMortionEvent });
import javax.swing.*; p.add(yes);
import java.awt.event.*; p.add(no);
class Demo extends JFrame{ p.add(cancel);
JPanel p; p.add(exit);
JButton yes;
JButton no; add(p);
JButton cancel; setSize(400,400);
JButton exit; show();
}
Demo(){
p=new JPanel(); public static void main(String []args){
yes=new JButton("Yes"); new Demo();
no=new JButton("No"); }
cancel=new JButton("Cancel"); }
exit=new JButton("Exit");

yes.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed Yes Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed Yes Button");
}

});

no.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed No Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed No Button");
}

});
cancel.addMouseMotionListener(new
MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed Cancel Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed Cancel Button");
}

});

exit.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
System.exit(0);
}
public void mouseDragged(MouseEvent evt){
System.exit(0);
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 19


Lecture Notes: Developing GUIs (javax.swing)
Q75. Implementing MouseMotionListener Q76. WindowsAdapter class
import javax.swing.*; import javax.swing.*;
import java.awt.event.*; import java.awt.event.*;
class Demo extends JFrame{ class Demo extends JFrame{
JPanel p; JPanel p;
JButton yes; JButton yes;
JButton no; JButton no;
JButton cancel; JButton cancel;
JButton exit; JButton exit;
Demo(){
Demo(){ p=new JPanel();
p=new JPanel(); yes=new JButton("Yes");
yes=new JButton("Yes"); no=new JButton("No");
no=new JButton("No"); cancel=new JButton("Cancel");
cancel=new JButton("Cancel"); exit=new JButton("Exit");
exit=new JButton("Exit");
yes.addMouseMotionListener(new MouseMotionAdapter(){
MyMouseActions mouseMoved=new MyMouseActions(); public void mouseMoved(MouseEvent evt){
yes.addMouseMotionListener(mouseMoved); JOptionPane.showMessageDialog(null, "You pressed
no.addMouseMotionListener(mouseMoved); Yes Button");
cancel.addMouseMotionListener(mouseMoved); }
exit.addMouseMotionListener(mouseMoved); });
no.addMouseMotionListener(new MouseMotionAdapter(){
p.add(yes); public void mouseMoved(MouseEvent evt){
p.add(no); JOptionPane.showMessageDialog(null, "You pressed
p.add(cancel); No Button");
p.add(exit); }
});
add(p); cancel.addMouseMotionListener(new
setSize(400,400); MouseMotionAdapter(){
show(); public void mouseMoved(MouseEvent evt){
} JOptionPane.showMessageDialog(null, "You pressed
cancel Button");
class MyMouseActions implements MouseMotionListener{ }
public void mouseMoved(MouseEvent evt){ });
if(evt.getSource()==yes)
JOptionPane.showMessageDialog(null, exit.addMouseMotionListener(new MouseMotionAdapter(){
"You pressed Yes Button"); public void mouseMoved(MouseEvent evt){
else if(evt.getSource()==no) System.exit(0);
JOptionPane.showMessageDialog(null, }
"You pressed No Button"); });
else if(evt.getSource()==cancel)
p.add(yes);
JOptionPane.showMessageDialog(null, p.add(no);
"You pressed Cancel Button"); p.add(cancel);
else p.add(exit);
System.exit(0);
} add(p);
public void mouseDragged(MouseEvent evt){ addWindowListener(new WindowAdapter(){
// code here for mouse drag event public void windowClosing(WindowEvent evt){
} JOptionPane.showMessageDialog(null, "You are
} closing the window..");
}
public static void main(String []args){ });
new Demo(); setSize(400,400);
} show();
} }

public static void main(String []args){


new Demo();
}
}

20 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q77. Layout Manager Q79. BorderLayout
import java.awt.*; import java.awt.*;
import javax.swing.*; import java.awt.event.*;
class DemoBorder1 extends JFrame{ import javax.swing.*;
JButton btn1; public class Border extends JFrame {
JButton btn2; JButton nButton = new JButton("North");
JButton btn3; JButton sButton = new JButton("South");
JButton btn4; JButton eButton = new JButton("East");
JButton btn5; JButton wButton = new JButton("West");
JButton cButton = new JButton("Center");
DemoBorder1(){
btn1=new JButton("West"); public Border() {
btn2=new JButton("East"); super("Border");
btn3=new JButton("South"); setSize(240, 280);
btn4=new JButton("North"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn5=new JButton("Center"); setLayout(new BorderLayout());
add(nButton, BorderLayout.NORTH);
add(btn1); add(sButton, BorderLayout.SOUTH);
add(btn2); add(eButton, BorderLayout.EAST);
add(btn3); add(wButton, BorderLayout.WEST);
add(btn4); add(cButton, BorderLayout.CENTER);
add(btn5); }
setSize(300,300); public static void main(String[] arguments) {
show(); Border frame = new Border();
} frame.setVisible(true);
public static void main(String []args){ }
DemoBorder1 db=new DemoBorder1(); }
}
}

Q78. BorderLayoyut
import java.awt.*;
import javax.swing.*;
class DemoBorder2 extends JFrame{
JButton btn1;
JButton btn2;
JButton btn3;
JButton btn4;
JButton btn5;

DemoBorder2(){
setLayout(new BorderLayout());
btn1=new JButton("West");
btn2=new JButton("East");
btn3=new JButton("South");
btn4=new JButton("North");
btn5=new JButton("Center");

add(btn1,"West");
add(btn2,"East");
add(btn3,"South");
add(btn4,"North");
add(btn5,"Center");

setSize(300,300);
show();
}
public static void main(String []args){
DemoBorder2 db=new DemoBorder2();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 21


Lecture Notes: Developing GUIs (javax.swing)
Q80. FlowLayout Q81. GridLayout
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
class DemoFlowLayout extends JFrame{ class DemoGrid extends JFrame{
JButton b1; JPanel p1;
JButton b2; JButton b1;
JButton b3; JButton b2;
JButton b4; JButton b3;
JButton b5; JButton b4;
DemoFlowLayout(){ JButton b5;
super("Demonstrates FlowLayout.."); JButton b6;
b1=new JButton("Yes1"); JButton b7;
b2=new JButton("Yes2"); JButton b8;
b3=new JButton("Yes3"); JButton b9;
b4=new JButton("Yes4");
b5=new JButton("Yes5"); DemoGrid(){
p1=new JPanel();
JPanel p=new JPanel(); p1.setLayout(new GridLayout(3,3));
//FlowLayout f=new FlowLayout();
//p.add(f); b1=new JButton("1");
//setLayout(f); b2=new JButton("2");
p.add(b1); b3=new JButton("3");
p.add(b2); b4=new JButton("4");
p.add(b3); b5=new JButton("5");
p.add(b4); b6=new JButton("6");
p.add(b5); b7=new JButton("7");
setSize(35,500); b8=new JButton("8");
setVisible(true); b9=new JButton("9");
add(p);
pack(); p1.add(b1);
JPanel p2=new JPanel(); p1.add(b2);
JTextArea t1=new JTextArea(3,24); p1.add(b3);
p2.add(t1); p1.add(b4);
add(p2); p1.add(b5);
pack(); p1.add(b6);
} p1.add(b7);
public static void main(String args[]){ p1.add(b8);
new DemoFlowLayout(); p1.add(b9);
}
} getContentPane().add(p1);
setSize(400,500);
show();
}

public static void main(String []args){


new DemoGrid();
}
}

22 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q82. GridLayout Q83. Event Handling Part II
import java.awt.*; import java.awt.event.*;
import java.awt.event.*; import javax.swing.*;
import javax.swing.*; class Event1 implements ActionListener{
class Bunch extends JFrame { int i=0;
JButton marcia = new JButton("Marcia"); public void actionPerformed(ActionEvent e){
JButton carol = new JButton("Carol"); i++;
JButton greg = new JButton("Greg"); JOptionPane.showMessageDialog(null, "Now I is : "+i,
JButton jan = new JButton("Jan"); "increment", JOptionPane.ERROR_MESSAGE);
JButton alice = new JButton("Alice"); }
JButton peter = new JButton("Peter"); }
JButton cindy = new JButton("Cindy"); class Demo extends JFrame{
JButton mike = new JButton("Mike"); Event1 evt;
JButton bobby = new JButton("Bobby"); JPanel p;
JButton btn;
public Bunch() {
super("Bunch"); Demo(){
setSize(260, 260); p=new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); evt=new Event1();
JPanel pane = new JPanel(); btn=new JButton("increment");
GridLayout family = new GridLayout(3, 3, 10, 10);
pane.setLayout(family); btn.addActionListener(evt);
pane.add(marcia); p.add(btn);
pane.add(carol); add(p);
pane.add(greg); setSize(100,100);
pane.add(jan); show();
pane.add(alice); }
pane.add(peter);
pane.add(cindy); public static void main(String []args){
pane.add(mike); new Demo();
pane.add(bobby); }
add(pane); }
setVisible(true);
}

public static void main(String[] arguments) {


Bunch frame = new Bunch();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 23


Lecture Notes: Developing GUIs (javax.swing)
Q84. Movable Frame (Arrow Keys) Q85.
import java.awt.*; import java.awt.*;
import java.awt.event.*; import javax.swing.*;
import javax.swing.*; import java.awt.event.*;
class Event4 extends JFrame {
int i=100; class AddTwo extends JFrame implements ActionListener {
int j=100; JTextField one = new JTextField("0", 5);
JLabel plus = new JLabel("+");
Event4(){ JTextField two = new JTextField("0", 5);
super("Moveble Frame"); JButton equals = new JButton("=");
addKeyListener(new KeyListener(){ JTextField result = new JTextField("0", 5);
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_LEFT){ public AddTwo() {
setLeft(); super("Add Two Numbers");
} setSize(400, 60);
if(e.getKeyCode()==KeyEvent.VK_RIGHT){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setRight(); Container pane = getContentPane();
} FlowLayout flow = new FlowLayout();
if(e.getKeyCode()==KeyEvent.VK_UP){ pane.setLayout(flow);
setUp(); equals.addActionListener(this);
} pane.add(one);
if(e.getKeyCode()==KeyEvent.VK_DOWN){ pane.add(plus);
setDown(); pane.add(two);
} pane.add(equals);
pane.add(result);
} setContentPane(pane);
public void keyReleased(KeyEvent e){} setVisible(true);
public void keyTyped(KeyEvent e){} }
});
setBounds(i,j,300,300); public static void main(String[] arguments) {
show(); AddTwo frame = new AddTwo();
} }

public void actionPerformed(ActionEvent evt) {


public static void main(String []args){ try {
Event4 evt=new Event4(); int value1 = Integer.parseInt(one.getText());
int value2 = Integer.parseInt(two.getText());
} result.setText("" + (value1 + value2));
} catch (NumberFormatException exc) {
void setLeft(){ one.setText("0");
i=i-5; two.setText("0");
setBounds(i,j,300,300); result.setText("0");
} }
void setRight(){ }
i=i+5; }
setBounds(i,j,300,300);
}
void setUp(){
j=j-5;
setBounds(i,j,300,300);
}
void setDown(){
j=j+5;
setBounds(i,j,300,300);
}

24 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q86. Q87.
import java.awt.event.*; import java.awt.*;
import javax.swing.*; import java.awt.event.*;
import java.awt.*; import javax.swing.*;
class ChangeTitle extends JFrame implements ActionListener {
JButton b1 = new JButton("Rosencrantz"); class FormatChooser extends JFrame implements ItemListener {
JButton b2 = new JButton("Guildenstern"); String[] formats = { "(choose format)", "Atom", "RSS 0.92",
"RSS 1.0", "RSS 2.0" };
public ChangeTitle() { String[] descriptions = {
super("Title Bar"); "Atom weblog and syndication format",
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); "RSS syndication format 0.92 (Netscape)",
b1.addActionListener(this); "RSS/RDF syndication format 1.0 (RSS/RDF)",
b2.addActionListener(this); "RSS syndication format 2.0 (UserLand)"
FlowLayout flow = new FlowLayout(); };
setLayout(flow); JComboBox formatBox = new JComboBox();
add(b1); JLabel descriptionLabel = new JLabel("");
add(b2);
pack(); public FormatChooser() {
setVisible(true); super("Syndication Format");
} setSize(420, 150);
public void actionPerformed(ActionEvent evt) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object source = evt.getSource(); setLayout(new BorderLayout());
if (source == b1) { for (int i = 0; i < formats.length; i++) {
setTitle("Rosencrantz"); formatBox.addItem(formats[i]);
} else if (source == b2) { }
setTitle("Guildenstern"); formatBox.addItemListener(this);
} add(BorderLayout.NORTH, formatBox);
repaint(); add(BorderLayout.CENTER, descriptionLabel);
} setVisible(true);
}
public static void main(String[] arguments) {
ChangeTitle frame = new ChangeTitle(); public void itemStateChanged(ItemEvent event) {
} int choice = formatBox.getSelectedIndex();
} if (choice > 0) {
descriptionLabel.setText(descriptions[choice-1]);
}
}

public Insets getInsets() {


return new Insets(50, 10, 10, 10);
}

public static void main(String[] arguments) {


FormatChooser fc = new FormatChooser();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 25


Lecture Notes: Developing GUIs (javax.swing)
Q88. Q89.
import java.awt.*; import java.awt.*;
import java.awt.event.*; import javax.swing.*;
import javax.swing.*; import java.awt.event.*;

class KeyDisplay extends JFrame { class Positive extends JFrame implements FocusListener {
JLabel keyLabel = new JLabel("Hit any key"); JTextField[] value = new JTextField[3];

public KeyDisplay() { public Positive() {


super("Hit a Key"); super("Enter Numbers");
setSize(300, 200); setSize(300, 60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.CENTER)); Container pane = getContentPane();
KeyMonitor monitor = new KeyMonitor(this); FlowLayout flow = new FlowLayout();
setFocusable(true); pane.setLayout(flow);
addKeyListener(monitor); for (int i = 0; i < 3; i++) {
add(keyLabel); value[i] = new JTextField("0", 5);
setVisible(true); value[i].addFocusListener(this);
} pane.add(value[i]);
}
public static void main(String[] arguments) { setContentPane(pane);
new KeyDisplay(); setVisible(true);
} }
} public void focusGained(FocusEvent evt) {
checkValue(evt.getSource());
class KeyMonitor extends KeyAdapter { }
KeyDisplay display; public void focusLost(FocusEvent evt) {
checkValue(evt.getSource());
KeyMonitor(KeyDisplay display) { }
this.display = display; private void checkValue(Object source) {
} JTextField field = (JTextField)source;
try {
public void keyTyped(KeyEvent event) { int value = Integer.parseInt(field.getText());
display.keyLabel.setText("" + event.getKeyChar()); if (value < 0) {
display.repaint(); value = value * -1;
} field.setText("" + value);
} }
} catch (NumberFormatException exc) {
field.setText("0");
}
}

public static void main(String[] arguments) {


JFrame frame = new Positive();
}
}

26 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q90. FocusListener Q91.
import java.awt.event.*; import javax.swing.*;
import javax.swing.*;
import java.awt.*; class Authenticator extends javax.swing.JFrame {
JTextField username = new JTextField(15);
class SumNumbers extends JFrame implements JPasswordField password = new
FocusListener {
JTextField value1 = new JTextField("0", 5);
JPasswordField(15);
JLabel plus = new JLabel("+"); JTextArea comments = new JTextArea(4, 15);
JTextField value2 = new JTextField("0", 5); JButton ok = new JButton("OK");
JLabel equals = new JLabel("="); JButton cancel = new JButton("Cancel");
JTextField sum = new JTextField("0", 5);
public Authenticator() {
public SumNumbers() { super("Account Information");
super("Add Two Numbers"); setSize(300, 220);
setSize(350, 90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new
FlowLayout(FlowLayout.CENTER);
setLayout(flow);
JPanel pane = new JPanel();
// add listeners JLabel usernameLabel = new JLabel("Username:
value1.addFocusListener(this); ");
value2.addFocusListener(this); JLabel passwordLabel = new JLabel("Password:
// set up sum field ");
sum.setEditable(false); JLabel commentsLabel = new JLabel("Comments:
// add components ");
add(value1); comments.setLineWrap(true);
add(plus); comments.setWrapStyleWord(true);
add(value2); pane.add(usernameLabel);
add(equals);
pane.add(username);
add(sum);
setVisible(true);
pane.add(passwordLabel);
} pane.add(password);
pane.add(commentsLabel);
public void focusGained(FocusEvent event) { pane.add(comments);
try { pane.add(ok);
float total = Float.parseFloat(value1.getText()) + pane.add(cancel);
Float.parseFloat(value2.getText()); add(pane);
sum.setText("" + total); setVisible(true);
} catch (NumberFormatException nfe) { }
value1.setText("0");
value2.setText("0");
public static void main(String[] arguments) {
sum.setText("0");
}
Authenticator auth = new Authenticator();
} }
}
public void focusLost(FocusEvent event) {
focusGained(event);
}

public static void main(String[] arguments) {


SumNumbers frame = new SumNumbers();
}
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 27


Lecture Notes: Developing GUIs (javax.swing)
Q92. Q93.
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
class Authenticator2 extends javax.swing.JFrame { import javax.swing.*;
JTextField username = new JTextField(15);
JPasswordField password = new class FeedBar extends JFrame {
JPasswordField(15);
JTextArea comments = new JTextArea(4, 15); public FeedBar() {
JButton ok = new JButton("OK"); super("FeedBar");
JButton cancel = new JButton("Cancel");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public Authenticator2() { // create icons
super("Account Information"); ImageIcon loadIcon = new ImageIcon("load.gif");
setSize(300, 220); ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon subscribeIcon = new
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon("subscribe.gif");
ImageIcon unsubscribeIcon = new
JPanel pane = new JPanel(); ImageIcon("unsubscribe.gif");
JLabel usernameLabel = new JLabel("Username: // create buttons
"); JButton load = new JButton("Load", loadIcon);
JLabel passwordLabel = new JLabel("Password: JButton save = new JButton("Save", saveIcon);
"); JButton subscribe = new JButton("Subscribe",
JLabel commentsLabel = new JLabel("Comments: subscribeIcon);
"); JButton unsubscribe = new
comments.setLineWrap(true); JButton("Unsubscribe", unsubscribeIcon);
comments.setWrapStyleWord(true); // add buttons to toolbar
pane.add(usernameLabel); JToolBar bar = new JToolBar();
pane.add(username); bar.add(load);
pane.add(passwordLabel); bar.add(save);
pane.add(password); bar.add(subscribe);
pane.add(commentsLabel); bar.add(unsubscribe);
JScrollPane scroll = new JScrollPane(comments, // prepare user interface
JTextArea edit = new JTextArea(8, 40);
ScrollPaneConstants.VERTICAL_SCROLLBAR_AL JScrollPane scroll = new JScrollPane(edit);
WAYS, BorderLayout bord = new BorderLayout();
setLayout(bord);
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ add("North", bar);
NEVER); add("Center", scroll);
pane.add(scroll); pack();
pane.add(ok); setVisible(true);
pane.add(cancel); }
add(pane);
setVisible(true); public static void main(String[] arguments) {
} FeedBar frame = new FeedBar();
}
public static void main(String[] arguments) { }
Authenticator2 auth = new Authenticator2();
}
}

28 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)


Lecture Notes: Developing GUIs (javax.swing)
Q94. Q95.
import java.awt.*; import javax.swing.*;
import java.awt.event.*; class Birth{
import javax.swing.*;
public static void main(String args[]){
class FeedBar2 extends JFrame {
String s=JOptionPane.showInputDialog("Enter ID
public FeedBar2() { number:","9000000000v");
super("FeedBar 2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create icons
String s1=s.substring(0,2);
ImageIcon loadIcon = new ImageIcon("load.gif"); String s2=s.substring(2,5);
ImageIcon saveIcon = new ImageIcon("save.gif"); int y=Integer.parseInt(s1);
ImageIcon subscribeIcon = new ImageIcon("subscribe.gif"); int d=Integer.parseInt(s2);
ImageIcon unsubscribeIcon = new char sex;
ImageIcon("unsubscribe.gif");
// create buttons
int month=0;
JButton load = new JButton("Load", loadIcon); int date=0;
JButton save = new JButton("Save", saveIcon);
JButton subscribe = new JButton("Subscribe", subscribeIcon); if(d>500){
JButton unsubscribe = new JButton("Unsubscribe", sex='F';
unsubscribeIcon);
// add buttons to toolbar
d=d-500;
JToolBar bar = new JToolBar(); }
bar.add(load); else{
bar.add(save); sex='M';
bar.add(subscribe); d=d;
bar.add(unsubscribe);
// create menu
}
JMenuItem j1 = new JMenuItem("Load");
JMenuItem j2 = new JMenuItem("Save"); int ar[]={31,29,31,30,31,30,31,31,30,31,30,31};
JMenuItem j3 = new JMenuItem("Subscribe"); for(int i=0;i<12;i++){
JMenuItem j4 = new JMenuItem("Unsubscribe"); date=d-ar[i];
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Feeds");
d=date;
menu.add(j1); month=i;
menu.add(j2); if(date<ar[i+1])break;
menu.addSeparator(); }
menu.add(j3);
menu.add(j4);
menubar.add(menu);
if(month>0)
// prepare user interface month+=1;
JTextArea edit = new JTextArea(8, 40);
JScrollPane scroll = new JScrollPane(edit); String
BorderLayout bord = new BorderLayout(); mo[]={"JAN","FEB","MARCH","APRI","MAY","JU
setLayout(bord);
add("North", bar);
N","JUL","AUG","SEPT","OCT","NOV","DEC"};
add("Center", scroll); JOptionPane.showMessageDialog(null,"Date of
setJMenuBar(menubar); birth:"+y+","+mo[month]+","+date+"\n"+"Sex:"+sex,"
pack(); Data of ID",JOptionPane.PLAIN_MESSAGE);
setVisible(true); JOptionPane.showMessageDialog(null,"Do You
}
Want To Exit Program");
public static void main(String[] arguments) { System.exit(10);
FeedBar2 frame = new FeedBar2(); }
} }
}

©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT) 29


Lecture Notes: Developing GUIs (javax.swing)
Q96. } else {
import java.awt.*; response[i] = new JRadioButton(resp[i], false);
import java.awt.event.*; }
import javax.swing.*; group.add(response[i]);
sub2.add(response[i]);
class SurveyWizard extends JPanel implements ActionListener { }
int currentCard = 0; JPanel sub3 = new JPanel();
CardLayout cards = new CardLayout(); nextButton.setEnabled(true);
SurveyPanel[] ask = new SurveyPanel[3]; sub3.add(nextButton);
finalButton.setEnabled(false);
public SurveyWizard() { sub3.add(finalButton);
super(); GridLayout grid = new GridLayout(3, 1);
setSize(240, 140); setLayout(grid);
setLayout(cards); add(sub1);
// set up survey add(sub2);
String question1 = "What is your gender?"; add(sub3);
String[] responses1 = { "female", "male", "not telling" }; }
ask[0] = new SurveyPanel(question1, responses1, 2);
String question2 = "What is your age?"; void setFinalQuestion(boolean finalQuestion) {
String[] responses2 = { "Under 25", "25-34", "35-54", if (finalQuestion) {
"Over 54" }; nextButton.setEnabled(false);
ask[1] = new SurveyPanel(question2, responses2, 1); finalButton.setEnabled(true);
String question3 = "How often do you exercise each week?"; }
String[] responses3 = { "Never", "1-3 times", "More than 3" }
}; }
ask[2] = new SurveyPanel(question3, responses3, 1);
ask[2].setFinalQuestion(true); class SurveyFrame extends JFrame {
for (int i = 0; i < ask.length; i++) { public SurveyFrame() {
ask[i].nextButton.addActionListener(this); super("Survey");
ask[i].finalButton.addActionListener(this); setSize(290, 140);
add(ask[i], "Card " + i); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} SurveyWizard wiz = new SurveyWizard();
} add(wiz);
setVisible(true);
public void actionPerformed(ActionEvent evt) { }
currentCard++;
if (currentCard >= ask.length) { public static void main(String[] arguments) {
System.exit(0); SurveyFrame surv = new SurveyFrame();
} }
cards.show(this, "Card " + currentCard); }
}
}

class SurveyPanel extends JPanel {


JLabel question;
JRadioButton[] response;
JButton nextButton = new JButton("Next");
JButton finalButton = new JButton("Finish");

SurveyPanel(String ques, String[] resp, int def) {


super();
setSize(160, 110);
question = new JLabel(ques);
response = new JRadioButton[resp.length];
JPanel sub1 = new JPanel();
ButtonGroup group = new ButtonGroup();
JLabel quesLabel = new JLabel(ques);
sub1.add(quesLabel);
JPanel sub2 = new JPanel();
for (int i = 0; i < resp.length; i++) {
if (def == i) {
response[i] = new JRadioButton(resp[i], true);

30 ©Niroth L. Samarawickrama ( M.Sc in IT, B.Sc, SCJP, PGDip in IT)

You might also like