Java Programs Recent
Java Programs Recent
class appendDemo
String s;
int a=76;
s=sb.append("a=").append(a).append("!").toString();
System.out.println(s);
2. This program will create a combo box and allow the user to add or remove items from the
combo with ease
import javax.swing.*;
import java.awt.event.*;
JComboBox combo;
JTextField txtBox;
public AddRemoveItemFromCombo(){
1
JFrame frame = new JFrame("Add-Remove Item of a Combo Box");
button1.addActionListener(new ActionListener(){
if (!txtBox.getText().equals("")){
int a = 0;
if(combo.getItemAt(i).equals(txtBox.getText())){
a = 1;
break;
if (a == 1)
else
combo.addItem(txtBox.getText());
else{
});
2
JButton button2 = new JButton("Remove");
button2.addActionListener(new ActionListener(){
if (combo.getItemCount() > 0)
combo.removeItemAt(0);
else
});
panel.add(txtBox);
panel.add(combo);
panel.add(button1);
panel.add(button2);
frame.add(panel);
// frame.add(panel1);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output
3
3. Program to create a combo box
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JComboBox combo;
JTextField txt;
public ComboBox(){
4
JPanel panel = new JPanel();
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
txt.setText(str);
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
5
Output
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
panel.add(list);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
6
}
Output
import javax.swing.*;
import java.awt.*;
public CreateRadioButton(){
JRadioButton Male,Female;
buttonGroup.add(Male);
7
panel.add(Male);
buttonGroup.add(Female);
panel.add(Female);
Male.setSelected(true);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
Output
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
8
public class CreateSlider{
JSlider slider;
JLabel label;
public CreateSlider(){
slider.setValue(70);
slider.addChangeListener(new MyChangeAction());
panel.add(slider);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9
int value = slider.getValue();
label.setText(str);
Output
import javax.swing.*;
public SwingMenu(){
JFrame frame = new JFrame("Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10
JMenuBar menubar = new JMenuBar();
filemenu.add(new JSeparator());
editmenu.add(new JSeparator());
fileItem3.add(new JSeparator());
editItem2.add(new JSeparator());
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
11
frame.setJMenuBar(menubar);
frame.setSize(400,400);
frame.setVisible(true);
Output
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JTextField txtField;
JLabel lbl;
12
public SwingDragDrop(){
lbl.setTransferHandler(new TransferHandler("text"));
JComponent jc = (JComponent)e.getSource();
TransferHandler th = jc.getTransferHandler();
th.exportAsDrag(jc, e, TransferHandler.COPY);
};
lbl.addMouseListener(ml);
panel.add(txtField);
frame.add(lbl, BorderLayout.CENTER);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
13
Output
9. Tooltip program
import java.awt.event.*;
JList list;
JTextField txtItem;
DefaultListModel model;
public TooltipTextOfList(){
14
for(int i = 0; i < str_list.length; i++)
model.addElement(str_list[i]);
return item;
} else {
return null;
};
button.addActionListener(new MyAction());
panel.add(txtItem);
panel.add(button);
panel.add(list);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15
public class MyAction extends MouseAdapter implements ActionListener{
if (data.equals(""))
else{
model.addElement(data);
txtItem.setText("");
Output
16
10. To find multiples of numbers provided by the user (the user provides the starting number and
the last number
import java.util.Scanner;
value = scan.nextInt();
limit = scan.nextInt();
System.out.println ();
System.out.println ("The multiples of " +value + "between" + value + "and" + limit + "inclusive are:");
17
System.out.print (mult + "\t");
count++;
if(count % PER_LINE == 0)
System.out.println();
import javax.swing.*;
import java.awt.*;
JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste");
toolbar.add(cutbutton);
18
toolbar.add(copybutton);
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
12. Same button program but the name of the button changes when clicked on
import javax.swing.*;
import java.awt.event.*;
JButton button;
19
}
public ChangeButtonLabel(){
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (text.equals("Click here")){
button.setText("another label");
else{
button.setText("Click here");
20
13. List items for selection but only one can be selected
import javax.swing.*;
import java.awt.event.*;
public DimensionItemList(){
String name[] = {"PEACE", "PATRICIA DEBORAH ", "LABAN", "IRENE", "PAULINE", "JAMES",
"MATHEW"};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21
model.addElement(name[i]);
return item;
} else {
return null;
};
list.setPrototypeCellValue("NDEJJE UNIVERSITY");
panel.add(list);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
22
14. Program to display message dialog box when a button is clicked
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton button;
public ShowMessageDialog(){
button.addActionListener(new MyAction());
panel.add(button);
23
button = new JButton("Show \"Ok/Cancel\" message dialog box");
button.addActionListener(new MyAction());
panel.add(button);
button.addActionListener(new MyAction());
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
24
JOptionPane.showConfirmDialog(null, "This is the \"Yes/No/Cancel\" message dialog box.");
import java.awt.*;
import java.awt.event.*;
frm.add(lbl);
frm.setSize(350,200);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
System.exit(0);
});
25
TextField lFirstName = new TextField(20);
p.setLayout(new GridLayout(3,1));
p.add(jFirstName);
p.add(lFirstName);
p.add(jLastName);
p.add(lLastName);
p.add(Submit);
p1.add(p);
frm.add(p1,BorderLayout.NORTH);
Output
import java.awt.GridLayout;
import javax.swing.*;
class JOptionPaneTest {
26
public static void main(String[] args) {
panel.add(combo);
panel.add(field1);
panel.add(field2);
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
} else {
System.out.println("Cancelled");
27
Output
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton button;
public ShowMessageDialog(){
button.addActionListener(new MyAction());
panel.add(button);
28
button = new JButton("Show \"Ok/Cancel\" message dialog box");
button.addActionListener(new MyAction());
panel.add(button);
button.addActionListener(new MyAction());
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
29
JOptionPane.showConfirmDialog(null, "This is the \"Yes/No/Cancel\" message dialog box.");
Output
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import javax.swing.*;
30
// Declare Variables
public basicFrame() {
super();
// Main window
setTitle("ReportEditor");
setBackground(Color.LIGHT_GRAY);
// components
try {
txtfeld.read(in, datei);
in.close();
} catch (Exception e) {
System.out.println("Error !");
// setLayout(new GridLayout(2,2));
// Scroll Shizzle
31
bildlauf.getViewport().add(txtfeld, null);
getContentPane().add(bildlauf, BorderLayout.CENTER);
//txtfeld.setSize(500,680);
//add(txtfeld);
//this.getContentPane().add(txtfeld);
// close
addWindowListener(new WindowLauscher());
// event handlers...
System.exit(0);
hf.setSize(500, 700);
hf.setLocation(100, 100);
hf.setVisible(true);
32
}
Output
33