import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ProgressBarr extends JFrame implements ActionListener
{
JButton jb;
JProgressBar jpb1;
int value =0;
ProgressBarr()
{
Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
jb = new JButton("Click");
contentPane.add(jb);
jb.addActionListener(this);
jpb1 = new JProgressBar(0,5000);
contentPane.add(jpb1);
jpb1.setStringPainted(true);
}
public static void main(String args[])
{
ProgressBarr pb1 = new ProgressBarr();
pb1.setSize(300,80);
pb1.setTitle("Progress Bar");
pb1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pb1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
while(value <= 1000)
{
value = value+10;
jpb1.setValue(value);
}
}
}
Output:-
Showing posts with label Advance Java. Show all posts
Showing posts with label Advance Java. Show all posts
Saturday, February 4, 2012
Progress Bar
Table
import javax.swing.*;
import java.awt.*;
public class Table extends JFrame
{
Object[][] cells={{"ABC",999999},{"XYZ",999900},{"ABC",999999},{"XYZ",999900},{"ABC",999999}};
String[] columnName={"Name","Employee ID"};
JTable table=new JTable(cells,columnName);
JScrollPane pane=new JScrollPane(table);
Container con;
Table()
{
super("LOG IN");con=getContentPane();
setSize(200,200);
setLayout(null);
pane.setBounds(20,20,150,100);
con.add(pane);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String s[])
{
Table obj=new Table();
}
}
Output:-
Tree
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Tree1 extends JFrame
{
public static void main(String args[])
{
Tree1 t1 = new Tree1("A Tree");
t1.setSize(200,200);
t1.setVisible(true);
t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Tree1(String title)
{
setTitle(title);
DefaultMutableTreeNode root=new DefaultMutableTreeNode ("Engineering");
DefaultMutableTreeNode style=new DefaultMutableTreeNode ("Information Technology");
root.add(style);
style=new DefaultMutableTreeNode("Electronics");
root.add(style);
style=new DefaultMutableTreeNode ("Computer Science");
root.add(style);
style=new DefaultMutableTreeNode ("Mechanical");
root.add(style);
style=new DefaultMutableTreeNode ("Electrical");
root.add(style);
style=new DefaultMutableTreeNode ("Sound");
root.add(style);
JTree jt=new JTree(root);
Container contentPane=getContentPane();
contentPane.add(new JScrollPane(jt));
}
}
Output:-
Package
import pack.*;
import javax.swing.*;
import java.awt.*;
class Main extends JFrame
{
public static void main(String[] s)
{
JFrame f1=new JFrame();
f1.setSize(200,200);
f1.setBackground(Color.blue);
f1.setLayout(null);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pan d1=new Pan();
f1.add(d1);
}
}
-------------------------
package pack;
import javax.swing.*;
import java.awt.*;
public class Pan extends JPanel
{
JLabel l1;
public Pan()
{
setSize(100,100);
setLayout(null);
setBackground(Color.red);
setVisible(true);
setBounds(50,50,80,40);
l1=new JLabel("PANEL");
l1.setBounds(15,15,50,10);
add(l1);
}
}
Output:-
Friday, February 3, 2012
Timer
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JavaSimpleTimer extends JPanel implements ActionListener
{
int miliseconds=0;
int seconds=0;
int minutes=0;
Timer myTimer;
Font timerFont=new Font("Verdana",Font.BOLD,24);
public JavaSimpleTimer()
{
myTimer=new Timer(10,this);
JFrame myFrame=new JFrame("Simple Timer");
myFrame.add(this);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(200,100);
myFrame.setVisible(true);
myTimer.setInitialDelay(0);
myTimer.start();
}
public void paint(Graphics g)
{
super.paint(g);
String mili=Integer.toString(miliseconds);
String sec=Integer.toString(seconds);
String min=Integer.toString(minutes);
if(mili.length()==1)
{
mili="0"+mili;
}
if(sec.length()==1)
{
sec="0"+sec;
}
if(min.length()==1)
{
min="0"+min;
}
g.setFont(timerFont);
g.drawString(min+" :",20,20);
g.drawString(sec+" :",80,20);
g.drawString(mili,140,20);
}
public void actionPerformed(ActionEvent event)
{
miliseconds=miliseconds+1;
if(miliseconds==100)
{
miliseconds=0;
seconds=seconds+1;
}
if(seconds==60)
{
seconds=0;
minutes=minutes+1;
}
repaint();
}
public static void main(String[]args)
{
JavaSimpleTimer myTest=new JavaSimpleTimer();
}
}
Output:-
Sunday, June 5, 2011
First PDF
import java.io.FileOutputStream;
import java.util.Date;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class FirstPdf {
private static String FILE = "c:/temp/FirstPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// iText allows to add metadata to the PDF which can be viewed in your Adobe
// Reader
// under File -> Properties
private static void addMetaData(Document document) {
document.addTitle("My first PDF");
document.addSubject("Using iText");
document.addKeywords("Java, PDF, iText");
document.addAuthor("Lars Vogel");
document.addCreator("Lars Vogel");
}
private static void addTitlePage(Document document)
throws DocumentException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph("Title of the document", catFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph(
"Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph(
"This document describes something which is very important ",
smallBold));
addEmptyLine(preface, 8);
preface.add(new Paragraph(
"This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.de ;-).",
redFont));
document.add(preface);
// Start a new page
document.newPage();
}
private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("First Chapter", catFont);
anchor.setName("First Chapter");
// Second parameter is the number of the chapter
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph subPara = new Paragraph("Subcategory 1", subFont);
Section subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Hello"));
subPara = new Paragraph("Subcategory 2", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Paragraph 1"));
subCatPart.add(new Paragraph("Paragraph 2"));
subCatPart.add(new Paragraph("Paragraph 3"));
// Add a list
createList(subCatPart);
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 5);
subCatPart.add(paragraph);
// Add a table
createTable(subCatPart);
// Now add all this to the document
document.add(catPart);
// Next section
anchor = new Anchor("Second Chapter", catFont);
anchor.setName("Second Chapter");
// Second parameter is the number of the chapter
catPart = new Chapter(new Paragraph(anchor), 1);
subPara = new Paragraph("Subcategory", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("This is a very important message"));
// Now add all this to the document
document.add(catPart);
}
private static void createTable(Section subCatPart)
throws BadElementException {
PdfPTable table = new PdfPTable(3);
// t.setBorderColor(BaseColor.GRAY);
// t.setPadding(4);
// t.setSpacing(4);
// t.setBorderWidth(1);
PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
subCatPart.add(table);
}
private static void createList(Section subCatPart) {
List list = new List(true, false, 10);
list.add(new ListItem("First point"));
list.add(new ListItem("Second point"));
list.add(new ListItem("Third point"));
subCatPart.add(list);
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
}
Thursday, February 24, 2011
CheckBox ItemListener
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class CheckBox extends JFrame implements ItemListener
{
JCheckBox ch1,ch2;
JPanel p1,p2;
Container conn;
CheckBox()
{
conn=getContentPane();
conn.setBackground(Color.WHITE);
p1=new JPanel();p1.setBackground(Color.yellow);
p2=new JPanel();p2.setBackground(Color.red);
ch1=new JCheckBox("Blue",false);ch1.setOpaque(false);
ch2=new JCheckBox("Red",false);ch2.setOpaque(false);
conn.add(ch1);conn.add(ch2);
ch1.addItemListener(this);ch2.addItemListener(this);
}
public void itemStateChanged(ItemEvent e )
{
if (ch1.isSelected()==true)
{
conn.add(p1);p1.setBounds(30,40,50,30);
this.repaint();
}
if(ch1.isSelected()==false)
{
conn.remove(p1);p1.setBounds(30,40,50,30);
this.repaint();
}
if (ch2.isSelected()==true)
{
conn.add(p2);p2.setBounds(100,40,50,30);
this.repaint();
}
if (ch2.isSelected()==false)
{
conn.remove(p2);p2.setBounds(100,40,50,30);
this.repaint();
}
}
public static void main(String[] args)
{
JFrame win = new CheckBox();
win.setTitle("CheckBox");
win.setSize(200,120);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
Output
RadioButton ItemListener
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Radiobutton extends JFrame implements ItemListener
{
JRadioButton first,second,third,fourth;
JPanel p1,p2,p3,p4;
Container conn;
Radiobutton()
{
conn=getContentPane();
conn.setBackground(Color.ORANGE);
p1=new JPanel();
p1.setBackground(Color.blue);
p2=new JPanel();
p2.setBackground(Color.red);
p3=new JPanel();
p3.setBackground(Color.black);
p4=new JPanel();
p4.setBackground(Color.yellow);
first = new JRadioButton("Blue");
second = new JRadioButton("red");
third = new JRadioButton("Black");
fourth = new JRadioButton("Yellow");
//Add on Frame
conn.add(first);conn.add(second);conn.add(third);conn.add(fourth);
ButtonGroup bg = new ButtonGroup();
bg.add(first);bg.add(second);bg.add(third);bg.add(fourth);
first.addItemListener(this);
second.addItemListener(this);
third.addItemListener(this);
fourth.addItemListener(this);
}
public void itemStateChanged(ItemEvent e )
{
if (first.isSelected())
{
conn.add(p1);
conn.remove(p2);conn.remove(p3);conn.remove(p4);
p1.setBounds(40,60,30,30);
this.repaint();
}
if (second.isSelected())
{
conn.add(p2);
conn.remove(p1);conn.remove(p3);conn.remove(p4);
p2.setBounds(100,60,30,30);
this.repaint();
}
if (third.isSelected())
{
conn.add(p3);
conn.remove(p2);conn.remove(p1);conn.remove(p4);
p3.setBounds(160,60,30,30);
this.repaint();
}
if (fourth.isSelected())
{
conn.add(p4);
conn.remove(p2);conn.remove(p3);conn.remove(p1);
p4.setBounds(220,60,30,30);
this.repaint();
}
}
public static void main(String[] args)
{
JFrame win = new Radiobutton();
win.setTitle("Radiobutton");
win.setSize(300,200);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}Output
Tuesday, January 4, 2011
RadioButton
import java.awt.*;
import javax.swing.*;
class Radiobutton extends JFrame
{
JRadioButton first,second,third,fourth,fifth;
Radiobutton()
{
first = new JRadioButton("First");
second = new JRadioButton("Second");
third = new JRadioButton("Third");
fourth = new JRadioButton("Fourth");
fifth = new JRadioButton("Fifth");
//Add on Frame
add(first);
add(second);
add(third);
add(fourth);
add(fifth);
ButtonGroup bg = new ButtonGroup();
bg.add(first);
bg.add(second);
bg.add(third);
bg.add(fourth);
bg.add(fifth);
}
public static void main(String[] args)
{
JFrame win = new Radiobutton();
win.setTitle("Radiobutton");
win.setSize(400,100);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
Output
CheckBox
import javax.swing.*;
class CheckBox extends JFrame
{
JCheckBox ch1,ch2,ch3,ch4;
CheckBox()
{
ch1=new JCheckBox("Cricket",false);
ch2=new JCheckBox("Football",false);
ch3=new JCheckBox("Baseball",false);
ch4=new JCheckBox("Swimming",false);
add(ch1);add(ch3);add(ch4);add(ch2);
}
public static void main(String[] args)
{
JFrame win = new CheckBox();
win.setTitle("CheckBox");
win.setSize(400,100);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
Output
Saturday, January 1, 2011
Add Label
import javax.swing.*;
class Label extends JFrame
{
JLabel l;
Label()
{
add(l=new JLabel("Have a Nice Day"));
l.setBounds(30,60,100,20);
}
public static void main(String[] args)
{
JFrame win = new Label();
win.setTitle("Label");
win.setSize(200,200);
win.setLayout(null);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
Output
Create Button on Frame
import javax.swing.*;
class Button extends JFrame
{
JButton b1;
Button()
{
add(b1=new JButton("ok"));
b1.setBounds(60,60,50,50);
}
public static void main(String[] args)
{
JFrame win = new Button();
win.setTitle("Button");
win.setSize(200,200);
win.setLayout(null);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
OutputTuesday, December 28, 2010
Subscribe to:
Posts (Atom)