Advance Java Practical List

January 29, 2018 | Author: Viral Parmar | Category: Java Servlet, Databases, System Software, Computer Programming, Software Engineering
Share Embed Donate


Short Description

Advance Java Practical...

Description

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

PRACTICAL – 1 (AWT) 1) Write an AWT program to create check boxes for different courses belongs to a university such that the course selected would be displayed. import java.applet.Applet; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.*; public class pract1 extends Applet implements ItemListener,ActionListener { Button b1; Checkbox IT = null; Checkbox CE = null; Checkbox mech = null; Checkbox EE = null; Checkbox EC = null; Checkbox civil = null; public void init() { IT = new Checkbox("IT"); CE = new Checkbox("CE"); mech = new Checkbox("mech"); EE = new Checkbox("EE"); EC = new Checkbox("EC"); civil = new Checkbox("Civil"); b1 = new Button("Output"); add(IT);add(CE);add(mech);add(EE);add(EC);add(civil); add(b1); b1.addActionListener(this); IT.addItemListener(this); CE.addItemListener(this); mech.addItemListener(this); EE.addItemListener(this); EC.addItemListener(this); civil.addItemListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) { repaint(); } } public void paint(Graphics g) { int i=10; int k=60; INFORMATION TECHNOLOGY DEPARMENT

1

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

if(IT.getState()==true) { g.drawString("IT",10,k+=20); } if(CE.getState()==true) { g.drawString("CE",10,k+=20); } if(mech.getState()==true) { g.drawString("mech",10,k+=20); } if(EE.getState()==true) { g.drawString("EE",10,k+=20); } if(EC.getState()==true) { g.drawString("EC",10,k+=20); } if(civil.getState()==true) { g.drawString("civil",10,k+=20); } } public void itemStateChanged(ItemEvent ie) { //repaint(); } }

Output:

INFORMATION TECHNOLOGY DEPARMENT

2

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

2) Create a list of vegetables if you click on one of the items of the list items would be displayed in text box. import java.applet.Applet; import java.awt.Graphics; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /* */ public class pract2 extends Applet implements ItemListener { List list = null; TextField a; public void init() { a = new TextField( 70); //create a multi select list list = new List(5, true); //add items to a list list.add("potatoes,"); list.add("Brinjal,"); list.add("Cabbage,"); list.add("Carrot,"); list.add("Onion,"); list.add("Mint,"); list.add("potato,"); //add list add(list); add(a); //add listener list.addItemListener(this); } public void paint(Graphics g) { String[] items = list.getSelectedItems(); String msg = ""; for(int i=0; i < items.length; i++) { msg = items[i] + " " + msg; } a.setText("vegetables:"+ msg); } public void itemStateChanged(ItemEvent ie) { repaint(); } } INFORMATION TECHNOLOGY DEPARMENT

3

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

Output:

INFORMATION TECHNOLOGY DEPARMENT

4

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

PRACTICAL – 2 (SWING) 3) Create a login form which contains user ID, password field and two buttons submit and reset. If the user ID and password is left blank on click of submit button, show a message to the user to fill in the fields, on click of reset button clear the fields. import java.awt.*; import java.awt.event.*; import javax.swing.*; publicclass login extendsJFrame { privateJLabel jLabel1; privateJLabel jLabel2; privateJTextField jTextField1; privateJPasswordField jPasswordField1; privateJButton jButton1; privateJPanel contentPane; public login() { super(); create(); this.setVisible(true); } privatevoid create() { jLabel1 = newJLabel(); jLabel2 = newJLabel(); jTextField1 = newJTextField(); jPasswordField1 = newJPasswordField(); jButton1 = newJButton(); contentPane = (JPanel)this.getContentPane(); jLabel1.setHorizontalAlignment(SwingConstants.LEFT); jLabel1.setForeground(newColor(0, 0, 255)); jLabel1.setText("username:"); jLabel2.setHorizontalAlignment(SwingConstants.LEFT); jLabel2.setForeground(newColor(0, 0, 255)); jLabel2.setText("password:"); jTextField1.setForeground(newColor(0, 0, 255)); jTextField1.setSelectedTextColor(newColor(0, 0, 255)); jTextField1.setToolTipText("Enter your username"); jTextField1.addActionListener(newActionListener(){ publicvoid actionPerformed(ActionEvent e) { jTextField1_actionPerformed(e); } } jPasswordField1.setForeground(newColor(0, 0, 255)); jPasswordField1.setToolTipText("Enter your password"); INFORMATION TECHNOLOGY DEPARMENT

5

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

jPasswordField1.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { jPasswordField1_actionPerformed(e); } } jButton1.setBackground(newColor(204, 204, 204)); jButton1.setForeground(newColor(0, 0, 255)); jButton1.setText("Login"); jButton1.addActionListener(newActionListener(){ publicvoid actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } } contentPane.setLayout(null); contentPane.setBorder(BorderFactory.createEtchedBorder()); contentPane.setBackground(newColor(204, 204, 204)); addComponent(contentPane, jLabel1, 5,10,106,18); addComponent(contentPane, jLabel2, 5,47,97,18); addComponent(contentPane, jTextField1, 110,10,183,22); addComponent(contentPane, jPasswordField1, 110,45,183,22); addComponent(contentPane, jButton1, 150,75,83,28); this.setTitle("Login To Members Area"); this.setLocation(newPoint(76, 182)); this.setSize(newDimension(335, 141)); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setResizable(false); } privatevoid addComponent(Container container,Component c,int x,int y,int width,int height) { c.setBounds(x,y,width,height); container.add(c); } privatevoid jTextField1_actionPerformed(ActionEvent e){} privatevoid jPasswordField1_actionPerformed(ActionEvent e){} privatevoid jButton1_actionPerformed(ActionEvent e) { System.out.println("\njButton1_actionPerformed(ActionEvent e) called."); String username = newString(jTextField1.getText()); String password = newString(jPasswordField1.getText()); if(username.equals("") || password.equals("")) { jButton1.setEnabled(false);

INFORMATION TECHNOLOGY DEPARMENT

6

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

JLabel errorFields = newJLabel("You must entera username and password to login."); JOptionPane.showMessageDialog(null,errorFields); jTextField1.setText(""); jPasswordField1.setText(""); jButton1.setEnabled(true); this.setVisible(true); } else{ JLabel optionLabel = newJLabel("You entered"+username+"as your username. Is this correct?"); int confirm =JOptionPane.showConfirmDialog(null,optionLabel); switch(confirm) { caseJOptionPane.YES_OPTION: jButton1.setEnabled(false); break; caseJOptionPane.NO_OPTION: jButton1.setEnabled(false); jTextField1.setText(""); jPasswordField1.setText(""); jButton1.setEnabled(true); break; caseJOptionPane.CANCEL_OPTION: jButton1.setEnabled(false); jTextField1.setText(""); jPasswordField1.setText(""); jButton1.setEnabled(true); break; } } } publicstaticvoid main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.Wi ndowsLookAndFeel"); } catch(Exception ex){ System.out.println("Failed loading L&F: "); System.out.println(ex); INFORMATION TECHNOLOGY DEPARMENT

7

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

} new login(); } }

Output:

Fig: Showing Login Form One with Empty Username And Password

INFORMATION TECHNOLOGY DEPARMENT

8

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

4) Create a split pane which divides the frame into two parts .process a list and on selecting an item in a list the items should be displayed in the other portion. import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; public class pract4 { @SuppressWarnings("deprecation") public static void main(String[] args) { JFrame frame = new SplitPaneFrame(); frame.show(); } } class SplitPaneFrame extends JFrame implements ListSelectionListener { public SplitPaneFrame() { setSize(400, 300); list = new JList(texts); list.addListSelectionListener(this); description = new JTextArea(); JSplitPane innerPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list, description); getContentPane().add(innerPane, "Center"); } public void valueChanged(ListSelectionEvent event) { JList source = (JList)event.getSource(); Display value = (Display)source.getSelectedValue(); description.setText(value.getDescription()); } private JList list; private JTextArea description; private Display[] texts = { new Display("Text1", "This is text1."), new Display("Text2", "This is text2."), new Display("Text3", "This is text3."), new Display("Text4", "This is text4.") }; } class Display { public Display(String n, String t)

INFORMATION TECHNOLOGY DEPARMENT

9

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

{ name = n; des = t; } public String toString() { return name; } public String getDescription() { return des; } private String name; private String des; }

Output:

Figure: Output of split pan

INFORMATION TECHNOLOGY DEPARMENT

10

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

PRACTICAL – 3 (JDBC) 5) Write a JDBC program to insert details of college student in the MS Access database. /* database connectivity with MS-Access is done by creating DataSourceName(dsn) in this example*/ /* Steps to use this example: * go to ms-access and make a database called "student_base" and create table named student_base.mdb * 1. Go to Control Panel 2. Click on Administrative Tools(windows 2000/xp), Click onODBC(win98) OR if u have (windows 7) than go to C:\Windows\SysWOW64\odbcad32.exe 3. click on ODBC 4. Then , you will see a ODBC dialog box. Click on UserDSn 5. Click on Add Button 6. Select Microsoft Access Driver(*.mdb) driver and click on finish 7. Give a Data Source Name : student_base 8. Then Click on Select 9. Browse on the database addItemDB.mdb file on your disk by downloading it link provided.. will be stored 10. Click on OK. Once the DSN is created, you can do this example*/ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; import java.awt.event.*; import java.sql.*; public class addItemToDatabase extends JFrame { //Initializing Components private JTextField inputs[]; private JButton add, reset; private JLabel labels[]; private String fldLabel[] = {"First Name: ","Last Name: ","Branch","Enroll-no "}; private JPanel p1; Connection con; Statement st; ResultSet rs; String db;

INFORMATION TECHNOLOGY DEPARMENT

11

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR

AJT- 170703

//Setting up GUI public addItemToDatabase() { //Setting up the Title of the Window super("Adding Data to the Database"); //Set Size of the Window (WIDTH, HEIGHT) setSize(300,180); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing Components inputs = new JTextField[4]; labels = new JLabel[4]; add = new JButton("Add"); reset = new JButton("Reset"); p1 = new JPanel(); //Setting Layout on JPanel 1 with 5 rows and 2 column p1.setLayout(new GridLayout(5,2)); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Setting up the container layout GridLayout grid = new GridLayout(1,1,0,0); pane.setLayout(grid); //Creating a connection to MS Access and fetching errors using "try-catch" to check if it is successfully connected or not. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //db = "jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=nilesh1.accdb;"; con = DriverManager.getConnection("jdbc:odbc:nilesh"); st = con.createStatement(); JOptionPane.showMessageDialog(null,"Successfully Connected to Database","Confirmation", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.ERROR_MESSAGE); System.exit(0); } //Constructing JLabel and JTextField using "for loop" in their desired order for(int count=0; count0) { for(int i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF