[Go to site: main page, start]

0% found this document useful (0 votes)
3 views10 pages

Java Program

The document contains multiple Java code snippets demonstrating the creation of GUI components using Swing and AWT, including tables, checkboxes, JDBC connections, and dialog boxes. Each code segment illustrates different functionalities such as adding components to frames, setting fonts and colors, and handling user interactions. Overall, it serves as a practical guide for building basic Java applications with graphical user interfaces.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java Program

The document contains multiple Java code snippets demonstrating the creation of GUI components using Swing and AWT, including tables, checkboxes, JDBC connections, and dialog boxes. Each code segment illustrates different functionalities such as adding components to frames, setting fonts and colors, and handling user interactions. Overall, it serves as a practical guide for building basic Java applications with graphical user interfaces.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Creating table in swing

import [Link];
import [Link];
import [Link];
public class table
{
JFrame f;
JTable t;

table()
{
f=new JFrame();
[Link]("tbale");
String [][] data={

{"abc","1","cs"},{"pqr","2","bcom"},{"hgfh","3","bcz"},{"asdas","2","asd"}
};
String[] colname={"name","pin","dept"};
t=new JTable(data,colname);
JScrollPane sp=new JScrollPane(t);
[Link](sp);
[Link](true);
[Link](400,400);

}
public static void main(String a[])
{
new table();
}

2. creating check box


import [Link].*;
import [Link].*;
public class chbox extends Frame
{
public chbox()
{
CheckboxGroup ch1,chh2;
Checkbox cd1,cd2,cd3,cd4,ch2,ch3,ch4,ch22,ch33,ch44;
cd1=new Checkbox("vb",true);
cd2=new Checkbox("asp",false);
cd3=new Checkbox("php",false);
cd4=new Checkbox("java",false);

ch1=new CheckboxGroup();
ch2=new Checkbox("asp",false,ch1);
ch3=new Checkbox("php",false,ch1);
ch4=new Checkbox("java",false,ch1);

chh2=new CheckboxGroup();
ch22=new Checkbox("execute",false,chh2);
ch33=new Checkbox("read",false,chh2);
ch44=new Checkbox("write",false,chh2);

Panel p1=new Panel();


[Link](cd1);
[Link](cd2);
[Link](cd3);
[Link](cd4);

[Link](ch2);
[Link](ch3);
[Link](ch4);

[Link](ch22);
[Link](ch33);
[Link](ch44);

add(p1);
setBackground([Link]);
setForeground([Link]);
setSize(400,400);
setVisible(true);
setLayout(new GridLayout(2,4));
}

public static void main(String a[])


{
chbox ch=new chbox();
}
}

[Link] jdbc connection


import [Link].*;
class dbconnect
{
public static void main(String a[])
{
try
{
[Link]("[Link]");
Connection conn=[Link]("jdbc:odbc:myjavadbname");
[Link]("connected");
Statement st=[Link]();
boolean b=[Link]("create table book(bno number,name varchar(20))");
if(b)
[Link]("table not created");
else
[Link]("table created");

//Resultset rs=[Link]("select * from emp");

}
catch(Exception s)
{
[Link]("error"+s);
}
}

[Link] values into tables and displaying


import [Link].*;
class dbconnect2
{
public static void main(String a[])
{
try
{
[Link]("[Link]");
Connection conn=[Link]("jdbc:odbc:db2");
[Link]("connected");
Statement st=[Link]();
boolean b=[Link]("create table emp2(bno number,name varchar(20))");
if(b)
[Link]("table not created");
else
[Link]("table created");
[Link]("insert into emp2 values(1,'sd')");
[Link]("value insetred");

ResultSet rs=[Link]("select * from emp2");


while([Link]())
{
[Link]([Link](1)+" ");
[Link]([Link](2)+" ");

}
catch(Exception s)
{
[Link]("error"+s);
}
}

5 adding component in frames

import [Link].*;
class f1
{
public static void main(String a[])
{
Frame fr1=new Frame();
Button btn=new Button("ok");
//[Link](btn);
Button btn2=new Button("ok2");
//[Link](btn2);
Panel p1=new Panel();
String s1="h1";
Label l1=new Label("hello world");
Label l2=new Label(s1);
[Link](l1);
[Link](l2);
[Link](btn);
[Link](btn2);

[Link](p1);
[Link](true);
[Link](400,400);
[Link]("first frame demo");

6 setting font and color to frames

import [Link].*;
class f2 extends Frame
{

public void paint(Graphics g)


{
Font f=new Font("Helvetica",[Link],14);
[Link](f);
[Link]([Link]);
[Link]("this is my first demo",75,100);
}
public static void main(String a[])
{
f2 ff2=new f2();
[Link]([Link]);
[Link]([Link]);
[Link](400,400);
[Link](true);
}
}

7 creating dialog box using swing

import [Link].*;
import [Link].*;
public class jdialog
{
public static void main(String a[])
{

JDialog stest=new JDialog();


[Link]("dialog box");
[Link](true);
[Link](300,300);

}
}
8 creating jdialog using swing with action

import [Link].*;
import [Link].*;
public class jdialogaction extends JFrame implements ActionListener
{
static JFrame frame;
public static void main(String a[])
{
frame=new JFrame("JFRAm");
jdialogaction ac=new jdialogaction();
JPanel p=new JPanel();
JButton btn=new JButton("hello");
[Link](ac);
[Link](btn);
[Link](p);
[Link](300,400);
[Link]();

}
public void actionPerformed(ActionEvent a)
{
String s=[Link]();
if([Link]("hello"))
{
JDialog stest=new JDialog(frame,"daihsvdh");
JLabel l1=new JLabel("this is dialog box after click heello");
[Link](l1);
//[Link]("dialog box");
[Link](true);
[Link](300,300);

}
}
}
9 creating dialog with different option

import [Link].*;
import [Link].*;
public class jdialogbox
{
public static void main(String a[])
{
[Link](null,"heello world");
String name=[Link]("enter user name");
[Link](null,name);
int input=[Link](null,"are u sure ?");
//0 =yes 1=no 3=cancel
[Link](input);

}
}

10 creating tab panes using swing

import [Link].*;
import [Link].*;
public class jframedemo extends JFrame
{
public jframedemo()
{
JPanel fruit,veg;
JLabel l1,l2,l3,l4,l5,l6;

fruit=new JPanel();
l1=new JLabel("apple");
l2=new JLabel("banana");
l3=new JLabel("watermelon");

[Link](l1);
[Link](l2);
[Link](l3);
add(fruit);

veg=new JPanel(new GridLayout(3,1));


l4=new JLabel("tomato");
l5=new JLabel("brinjal");
l6=new JLabel("potato");

[Link](l4);
[Link](l5);
[Link](l6);
add(veg);

JTabbedPane tb =new JTabbedPane();


[Link]("abc",fruit);
[Link]("xyz",veg);

add(tb);
setVisible(true);
setSize(200,200);
}
public static void main(String a[])
{
jframedemo jfd=new jframedemo();

}
}

You might also like