Sunday , 24 November 2024

CS506 Assignment 2 Solution Fall 2012

This the 100 % correct solution of CS506 assignment 2 Fall 2012 Submit your Assignment do not copy the source code just try this code yourself and take this only for help. Best of luck for your CS501 assignment 2 Solution fall 2012

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import javax.swing.table.*;

import java.sql.*;

class Form1 extends JFrame{

//create elements

private ArrayList<JCheckBox> grade = new ArrayList<JCheckBox>();

private JPanel TopPanel;

private JTable table;

private JScrollPane spane;

private String total;

DefaultTableModel model;

//end elements

//constructor

public Form1(){

setTitle(“Assignment 2 (CS506)”);

setSize(400,400);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

TopPanel = new JPanel();

TopPanel.setLayout(new BorderLayout());

TopPanel.add(getAllEle(),BorderLayout.CENTER);

getContentPane().add(TopPanel);

setVisible(true);

setResizable(false);

pack();

}//end constructor

//Handler Class

class Handler implements ItemListener{

//Handlre function

public void itemStateChanged(ItemEvent event){

total = “”;

for(JCheckBox box: grade){

if(box.isSelected()){

total += “‘” + box.getText() + “‘” + “,”;

}

}

if (total.equals(“”)){

total = “‘z'”;

}

model.setRowCount(0);

getData(total);

}


}// end Handler class

//function get all elements

public JPanel getAllEle(){

JPanel p = new JPanel();

p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

p.add(getCheckBox());

p.add(getTable());

return p;

}// end of function get all elements

//function get checkbox pagel

public JPanel getCheckBox(){

JPanel p = new JPanel();

p.setPreferredSize(new Dimension(400,100));

p.setBorder(BorderFactory.createTitledBorder(“Grades Selection”));

Box b = Box.createHorizontalBox();

grade.add(new JCheckBox(“A”));

grade.add(new JCheckBox(“B”));

grade.add(new JCheckBox(“C”));

grade.add(new JCheckBox(“D”));

grade.add(new JCheckBox(“F”));

for(JCheckBox jbox: grade){

jbox.addItemListener(new Handler());

b.add(jbox);

}

p.add(b);

return p;

} //end fo get check box panel

//function get table panel

public JPanel getTable(){

JPanel p = new JPanel();

String cols[] = {“ID”,”Name”,”Subject”,”Class”,”Grades”,”Address”};

String data[][] = {

{“232″,”fari”,”Computer Science”,”MCS”,”A”,”Lahore”}

};

model = new DefaultTableModel(data,cols);

table = new JTable(model);

spane = new JScrollPane(table);

spane.setAutoscrolls(true);

spane.setPreferredSize(new Dimension(400,200));

p.add(spane);

return p;

}// end function get table panel

//data base connection function

public void getData(String s){

try{

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

String url = “jdbc:odbc:studentDSN”;

Connection con = DriverManager.getConnection(url);

String sql = “SELECT * FROM Students WHERE Grades IN(“+s+”)ORDER BY

ID DESC “;

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

String id = rs.getString(“ID”);

String name = rs.getString(“Name”);

String subject = rs.getString(“Subject”);

String clas = rs.getString(“Class”);

String grades = rs.getString(“Grades”);

String address = rs.getString(“Address”);

model.insertRow(0, new Object[]{id, name, subject, clas, grades, address});

}

con.close();

} catch(Exception e){

System.out.println(e);

}

}//end of getData function

}

///Driver Class


import java.awt.*;

import javax.swing.*;

class Driver{

public static void main(String[] args){

try{

Form1 form1 = new Form1();

} catch(Exception e){

System.out.println(e);

}

}

}

Check Also

CS506 Unsolved Paper for Final Term Try to solve

Question No: 1 ( Marks: 1 ) – Please choose one Which of the following …

Leave a Reply

Your email address will not be published. Required fields are marked *

*