Wednesday, 6 November 2019

Swing view

Hi All,
Use this code.
----------------------------------

package placement;


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;
import java.sql.*;

class Skillview extends JFrame

                {

private JPanel topPanel ;

private JTable table;

private JScrollPane scrollPane;

private String[] columnNames= new String[3];

private String[][] dataValues=new String[10][9] ;
Connection con;
Statement stmt;

public Skillview()

    {

setTitle("View Reg");

setSize(1000,300);

topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?",
             "Exit Confirmation", JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           setVisible(false);
           new Homepage();
        }
    }
};
addWindowListener(exitListener);
/*
columnNames=new String[] {"Column 1" , "Column 2" , "Column 3"};

dataValues = new String[][]     {

                                                {"1","2","3"},

                                                {"4","5","6"},

                                                {"7","8","9"}

                                            };
                                            */
   //////
    columnNames = new String[] {
               "RId", "Date", "Description"

        };
          //create table with data
        try{
    Class.forName("com.mysql.jdbc.Driver");
     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
    //here sonoo is database name, root is username and password
     stmt=con.createStatement();
    //stmt.executeUpdate("insert into parol_request values('"+prison_id+"','123','1973-10-12','hfdjfsdjflejre','out')");
    ResultSet rs=stmt.executeQuery("select * from reminder");
    int i=0;
   while(rs.next()){
  dataValues[i][0]=rs.getString(1);
    dataValues[i][1]=rs.getString(2);
    dataValues[i][2]=rs.getString(3);
   
   //System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

    i=i+1;
    }
    //con.close();
    }catch(Exception e1){ System.out.println(e1);}

    ///////////////

 TableModel model=new myTableModel();

 table =new JTable( );

 table.setRowHeight(20);

 table.setModel(model);

 scrollPane=new JScrollPane(table);

  topPanel.add(scrollPane,BorderLayout.CENTER);

  table.addMouseListener(new java.awt.event.MouseAdapter()

            {

public void mouseClicked(java.awt.event.MouseEvent e)

{

int row=table.rowAtPoint(e.getPoint());

int col= table.columnAtPoint(e.getPoint());

JOptionPane.showMessageDialog(null," Value in the cell deleted :"+ " " +table.getValueAt(row,col).toString());
//String sql="delete from skills where skid='"+table.getValueAt(row,0).toString()+"'";
Skill hup=new Skill(table.getValueAt(row,0).toString(),table.getValueAt(row,1).toString(),table.getValueAt(row,2).toString());
hup.setVisible(true);
//System.out.println(""+sql);
try{
//con.open();
//stmt.executeUpdate(sql);
//con.close();
}
catch(Exception e1)
{

}
//System.out.println(" Value in the cell Updated :"+ " " +sql+" "+table.getValueAt(row,col).toString());

}

}

);
setVisible(true);
}

            public class myTableModel extends DefaultTableModel

                                                                                    {

                                                    myTableModel( )

                                                 {

                                                   super(dataValues,columnNames);

                                                   System.out.println("Inside myTableModel");

                                                 }

                                    public boolean isCellEditable(int row,int cols)

                                     {

                                   return false;

                                    }

                                    }

            public static void main(String args[])

            {

            Skillview mainFrame=new Skillview();

            mainFrame.setVisible(true);

            }

                        }

No comments:

Post a Comment

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...