Java
Slip27
Q.1) Write a java program to accept the details employee (Eno, Ename ,Salary) and add it into the JTable by clicking on the Button.(Max : 5 Records) [Marks30]
Solution
Emp.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Emp extends JFrame implements ActionListener
{
JButton b1 = new JButton("Add");
JButton b2 = new JButton("Close");
JLabel l1 = new JLabel("Eno :");
JLabel l2 = new JLabel("Ename :");
JLabel l3 = new JLabel("Salary :");
Jtable obj = new Jtable();
int count = 0;
JTextField t1 = new JTextField(20);
JTextField t2 = new JTextField(20);
JTextField t3 = new JTextField(20);
Emp() throws Exception
{
setLayout(new GridLayout(4,2));
b1.addActionListener(this);
b2.addActionListener(this);
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(b1); add(b2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b1)
{
if(count == 5)
{
JOptionPane.showMessageDialog(null,"Maximum Records are only 5","Failed",JOptionPane.INFORMATION_MESSAGE);
return;
}
count++;
obj.add(t1.getText(),t2.getText(), t3.getText());
}
else
System.exit(0);
}
public static void main(String ar[]) throws Exception
{
Emp e = new Emp();
e.show();
}
}
Slip27_2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class MyThread extends Thread
{
JTextField tf;
MyThread(JTextField tf)
{
this.tf = tf;
}
public void run()
{
while(true)
{
Date d = new Date();
int h = d.getHours();
int m = d.getMinutes();
int s = d.getSeconds();
tf.setText(h+":"+m+":"+s);
try
{
Thread.sleep(300);
}
catch(Exception e){}
}
}
}
class Slip27_2 extends JFrame implements ActionListener
{
JTextField txtTime;
JButton btnStart,btnStop;
MyThread t;
Slip27_2()
{
txtTime = new JTextField(20);
btnStart = new JButton("Start");
btnStop = new JButton("Stop");
setTitle("Stop Watch");
setLayout(new FlowLayout());
setSize(300,100);
add(txtTime);
add(btnStart);
add(btnStop);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnStart.addActionListener(this);
btnStop.addActionListener(this);
t = new MyThread(txtTime);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnStart)
{
if(!t.isAlive()) t.start();
else t.resume();
}
if(ae.getSource()==btnStop)
{
t.suspend();
}
}
public static void main(String a[])
{
new Slip27_2();
}
}
Jtable.java
import java.awt.*;
import javax.swing.*;
import java.sql.*;
/*
JTable
*/
class Jtable extends JFrame
{
private JTable table;
private JScrollPane scrollPane;
Object[][] data= new Object[5][3];
String colNames[] = {"Eno","Ename","Salary"};
int i;
Jtable()throws Exception
{
setTitle( "JTable Application" );
setSize( 300, 200 );
setBackground( Color.gray );
table=new JTable(data,colNames);
scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
setVisible(true);
}
void add(String s1,String s2, String s3)
{
data[i][0] = s1;
data[i][1] = s2;
data[i][2] = s3;
i++;
//table=new JTable(data,colNames);
//scrollPane = new JScrollPane( table );
//getContentPane().add( scrollPane );
}
}
Tags:
java