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

import javax.swing.

*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class JComboBoxDemo {


JFrame frame;
JComboBox ayears;
JButton addbtn,editbtn,removebtn,remove_all_btn;
public JComboBoxDemo() {
frame = new JFrame();
frame.setTitle("JComboBox Demo");
frame.setSize(400,400);
addbtn = new JButton("ADD"); editbtn = new JButton("Editable");
removebtn = new JButton("Remove");
remove_all_btn= new JButton("Remove All");
frame.setLocationRelativeTo(null);
String academic_years[] = {"FYCM","SYCM","TYCM"};
ayears = new JComboBox(academic_years);
frame.add(ayears);
frame.add(addbtn);
frame.add(removebtn);
frame.add(remove_all_btn);
frame.add(editbtn);
addbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String item = JOptionPane.showInputDialog(null,"Enter new Item ");
ayears.addItem(item);
}
});
removebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int index = ayears.getSelectedIndex();
System.out.println(index);
ayears.removeItemAt(index);
}
});
remove_all_btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{

ayears.removeAllItems();
}
});
editbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{

ayears.setEditable(true);
}
});
ayears.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{

String selected = ayears.getSelectedItem().toString();


System.out.println(selected);
}
});
ayears.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
String selected = ayears.getSelectedItem().toString();
// System.out.println(selected);
}

});
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String arg[])
{
JComboBoxDemo test = new JComboBoxDemo();
}

}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class JcheckBoxDemo {


JFrame frame;
JCheckBox ch1,ch2,ch3,ch4;
public JcheckBoxDemo() {

frame = new JFrame();


frame.setTitle("Check Box Demo App");
frame.setSize(400,400);
ch1 = new JCheckBox("C++",true); ch2 = new JCheckBox();
ch3 = new JCheckBox(); ch4 = new JCheckBox();
ch1.setText("C++");ch2.setText("JAVA");
ch3.setText("VB.NET");ch4.setText("PYTHON");
frame.add(ch1); frame.add(ch2); frame.add(ch3); frame.add(ch4);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

ItemListener il = new ItemListener(){


@Override
public void itemStateChanged(ItemEvent g)
{
String text=((JCheckBox)g.getSource()).getText().toString();
switch(text)
{
case "C++":
if(((JCheckBox)g.getSource()).isSelected()==true)
{
System.out.println("C++ is selected");
}else
{
System.out.println("C++ is deselected");
}
break;
case "JAVA":
if(((JCheckBox)g.getSource()).isSelected()==true)
{
System.out.println("JAVA is selected");
}else
{
System.out.println("JAVA is deselected");
}
break;
case "VB.NET":
if(((JCheckBox)g.getSource()).isSelected()==true)
{
System.out.println("VB.NET is selected");
}else
{
System.out.println("VB.NET is deselected");
}
break;
case "PYTHON":
if(((JCheckBox)g.getSource()).isSelected()==true)
{
System.out.println("PYTHON is selected");
}else
{
System.out.println("PYTHON is deselected");
}
break;
default:
break;
}
}
};
ch1.addItemListener(il);
ch2.addItemListener(il);
ch3.addItemListener(il);
ch4.addItemListener(il);
}
public static void main(String args[])
{
JcheckBoxDemo demo = new JcheckBoxDemo();
}

}
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;

public class TextAreaDemo {

JFrame frame;
JTextArea notepad;
JButton display;
JLabel word_count_status;
JLabel char_count_status;
JMenu editmenu;
JMenuBar mbar;
JMenuItem append,insert,font;
public TextAreaDemo() {
frame = new JFrame("");
frame.setSize(400,480);
frame.setLayout(null);
notepad = new JTextArea();
notepad.setBounds(30,30,320,320);
display = new JButton("Display");
display.setBounds(30,360,100,40);

word_count_status = new JLabel("Words count:");


word_count_status.setBounds(150,360,100,40);

char_count_status = new JLabel("Char count:");


char_count_status.setBounds(260,360,100,40);
mbar = new JMenuBar();
editmenu = new JMenu("Edit");
append = new JMenuItem("Append");
insert = new JMenuItem("Insert");
font = new JMenuItem("Font");
editmenu.add(append);editmenu.add(insert);editmenu.add(font);
mbar.add(editmenu);
frame.setJMenuBar(mbar);
append.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
String temp = JOptionPane.showInputDialog(frame,"Enter Text to Append");
notepad.append(temp);
}

});
insert.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
String temp = JOptionPane.showInputDialog(frame,"Enter
Text to Insert");
String position = JOptionPane.showInputDialog(frame,"Enter position to insert
at location");
notepad.insert(temp,Integer.parseInt(position));
}

});
font.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
Font myfont = new Font("Constantia",Font.BOLD,15);
notepad.setFont(myfont);
}
});
frame.add(char_count_status);
frame.add(word_count_status);
display.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
String text = notepad.getText();
String words[] = text.split("\\s");
int word_count = words.length;
int char_count = text.length();
word_count_status.setText("Word count:"+" "+Integer.toString(word_count));
char_count_status.setText("char count:"+" "+Integer.toString(char_count));
}

});
frame.add(display);
frame.add(notepad);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[])
{
TextAreaDemo demo = new TextAreaDemo();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;

public class LabelDemo extends JFrame{

JButton btn ;
JLabel label;
public LabelDemo() {
setTitle("Swing Button Test");
label = new JLabel("Hello From Advance Java..");
label.setBounds(180,50,200,40);
label.setBackground(Color.BLUE);
label.setForeground(Color.white);
label.setOpaque(true);
label.setHorizontalAlignment(JLabel.LEFT);
System.out.println("Alignemnt : "+label.getHorizontalAlignment());
add(label);

btn = new JButton();


btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JLabel mylabel = new JLabel("Dynamic created Label",new
ImageIcon("d:\\myicon.jpg"),JLabel.RIGHT);
mylabel.setBounds(50,200,260,100);
add(mylabel);
repaint();
}
});
btn.setLocation(50,50);
btn.setSize(100,50);
add(btn);

setSize(400,400);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String arg[])


{
LabelDemo demo = new LabelDemo();
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class LabelExample


{

public static void main(String arg[])


{
JFrame frame = new JFrame();

}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ButtonTest {


JFrame frame;
JButton btn1,btn2;

public ButtonTest() {
frame = new JFrame();
frame.setTitle("Button Demo");
btn1 = new JButton("Button 1");btn2 = new JButton("Button 2");
btn1.setMnemonic(KeyEvent.VK_C);
btn2.setMnemonic(KeyEvent.VK_V);
btn1.setBounds(50,50,100,50);
btn2.setBounds(250,50,100,50);
btn1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e)
{
btn2.setText("Cancel");
btn2.setIcon(new ImageIcon("d:\\myicon.jpg"));
}

});
btn2.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e)
{
btn1.setText("OK");
btn1.setIcon(btn2.getIcon());

});
frame.add(btn1); frame.add(btn2);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
public static void main(String arg[])
{
ButtonTest test = new ButtonTest();
}

}
import javax.swing.*;
import java.awt.*;

public class swingtest extends JFrame{


JButton btn ;
Button awtbtn;
public swingtest() {
setTitle("Swing Button Test");

btn = new JButton();


btn.setLocation(50,50);
btn.setSize(100,50);
add(btn);

awtbtn = new Button("OK AWT");


awtbtn.setLocation(200,50);
awtbtn.setSize(100,50);
add(awtbtn);

setSize(400,400);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
swingtest test = new swingtest();
}
}

You might also like