Java Swing
Open Dialog box
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class OpenMenu extends JFrame implements ActionListener{ JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; OpenMenu(){ open=new JMenuItem(“Open File”); open.addActionListener(this); file=new JMenu(“File”); file.add(open); mb=new JMenuBar(); mb.setBounds(0,0,800,20); mb.add(file); ta=new JTextArea(800,800); ta.setBounds(0,20,800,800); add(mb); add(ta); } public void actionPerformed(ActionEvent e) { if(e.getSource()==open){ openFile();…
Edit menu for Notepad
import javax.swing.*; import java.awt.event.*; public class Notepad implements ActionListener{ JFrame f; JMenuBar mb; JMenu file,edit,help; JMenuItem cut,copy,paste,selectAll; JTextArea ta; Notepad(){ f=new JFrame(); cut=new JMenuItem(“cut”); copy=new JMenuItem(“copy”); paste=new JMenuItem(“paste”); selectAll=new JMenuItem(“selectAll”); cut.addActionListener(this); copy.addActionListener(this); paste.addActionListener(this); selectAll.addActionListener(this); mb=new JMenuBar(); mb.setBounds(5,5,400,40); file=new JMenu(“File”); edit=new JMenu(“Edit”); help=new JMenu(“Help”); edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll); mb.add(file);mb.add(edit);mb.add(help);…
Displaying image
For displaying image, we can use the method drawImage() of Graphics class. Syntax of drawImage() method: public abstract boolean drawImage(Image img, int x, int y,…
Graphics in swing:
java.awt.Graphics class provides many methods for graphics programming. Commonly used methods of Graphics class: public abstract void drawString(String str, int x, int y): is used…
Digital Watch
import javax.swing.*; import java.awt.*; import java.text.*; import java.util.*; public class DigitalWatch implements Runnable{ JFrame f; Thread t=null; int hours=0, minutes=0, seconds=0; String timeString = “”; JButton b; DigitalWatch(){ f=new JFrame(); t = new Thread(this); t.start(); b=new JButton(); b.setBounds(100,100,100,50); f.add(b); f.setSize(300,400); f.setLayout(null); f.setVisible(true); } public void run() { try { while (true) { Calendar cal = Calendar.getInstance();…
J Slider class
The JSlider is used to create the slider. By using JSlider a user can select a value from a specific range. Commonly used Constructors of…
J Progress Bar class
The JProgressBar class is used to display the progress of the task. Commonly used Constructors of JProgressBar class: JProgressBar(): is used to create a horizontal…
JColor Chooser class
The JColorChooser class is used to create a color chooser dialog box so that user can select any color. Commonly used Constructors of JColorChooser class:…
JTable class
The JTable class is used to display the data on two dimensional tables of cells. Commonly used Constructors of JTable class: JTable(): creates a table…
JComboBox class
The JComboBox class is used to create the combobox (drop-down list). At a time only one item can be selected from the item list. Commonly…