0

I want to update my database with the cell values of a J Table when I click on the check boxes of the respective row. Here is my code. I am just a beginner for J2EE, so please elaborate the solution

https://i.stack.imgur.com/XDhgp.jpg

    package Inhospmgmt;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import java.awt.Toolkit;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import javax.swing.RowSorter;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JCheckBox;

public class investigation extends JFrame implements ActionListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 2930370058483683162L;
    static JFrame fm1=new JFrame();
    int cnt = 0, r=0,c=0;
    String columns[]={"SNO", "INVESTIGATION", "PRICE(Rs.)", "SELECTION"};
    String data[][]=new String[cnt][3];
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    static JTable table=new JTable();
    static JScrollPane jsp1; 
    DefaultTableModel tabModel ;

    investigation()
    {
        fm1.getContentPane().setBackground(Color.WHITE);
        fm1.setTitle("Social Care Hospital/Investigation");
        fm1.setIconImage(Toolkit.getDefaultToolkit().getImage(investigation.class.getResource("/Inhospmgmt/img/icpic.jpg")));
        fm1.getContentPane().setLayout(null);


        JLabel lblInvestigation = new JLabel("Investigations");
        lblInvestigation.setForeground(SystemColor.textHighlight);
        lblInvestigation.setFont(new Font("Times New Roman", Font.BOLD, 30));
        lblInvestigation.setHorizontalAlignment(SwingConstants.CENTER);
        lblInvestigation.setBounds(557, 23, 229, 46);
        fm1.getContentPane().add(lblInvestigation);

        JLabel label = new JLabel("PID No.");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setForeground(SystemColor.textHighlight);
        label.setFont(new Font("Tahoma", Font.BOLD, 20));
        label.setBounds(10, 83, 79, 28);
        fm1.getContentPane().add(label);

        textField = new JTextField();
        textField.setColumns(10);
        textField.setBackground(Color.WHITE);
        textField.setBounds(114, 78, 114, 33);
        fm1.getContentPane().add(textField);

        JButton button = new JButton("");
        button.setIcon(new ImageIcon(investigation.class.getResource("/Inhospmgmt/img/unnamed.png")));
        button.setFocusPainted(false);
        button.setContentAreaFilled(false);
        button.setBackground(Color.WHITE);
        button.setBounds(227, 78, 33, 33);
        fm1.getContentPane().add(button);

        JLabel label_1 = new JLabel("Patient Name :");
        label_1.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        label_1.setBounds(10, 144, 99, 22);
        fm1.getContentPane().add(label_1);

        textField_1 = new JTextField();
        textField_1.setEnabled(false);
        textField_1.setDisabledTextColor(Color.BLUE);
        textField_1.setColumns(10);
        textField_1.setBounds(137, 145, 226, 22);
        fm1.getContentPane().add(textField_1);

        JLabel label_2 = new JLabel("Patient Age :");
        label_2.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        label_2.setBounds(411, 144, 90, 22);
        fm1.getContentPane().add(label_2);

        textField_2 = new JTextField();
        textField_2.setEnabled(false);
        textField_2.setDisabledTextColor(Color.BLUE);
        textField_2.setColumns(10);
        textField_2.setBounds(499, 145, 79, 22);
        fm1.getContentPane().add(textField_2);

        JLabel lblDepartment = new JLabel("Department :");
        lblDepartment.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        lblDepartment.setBounds(633, 144, 99, 22);
        fm1.getContentPane().add(lblDepartment);

        textField_3 = new JTextField();
        textField_3.setEnabled(false);
        textField_3.setDisabledTextColor(Color.BLUE);
        textField_3.setColumns(10);
        textField_3.setBounds(725, 145, 249, 22);
        fm1.getContentPane().add(textField_3);

        JLabel lblWard = new JLabel("Ward :");
        lblWard.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        lblWard.setBounds(1018, 144, 90, 22);
        fm1.getContentPane().add(lblWard);

        textField_4 = new JTextField();
        textField_4.setEnabled(false);
        textField_4.setDisabledTextColor(Color.BLUE);
        textField_4.setColumns(10);
        textField_4.setBounds(1082, 145, 165, 22);
        fm1.getContentPane().add(textField_4);

        JLabel lblBedNo = new JLabel("Bed No :");
        lblBedNo.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        lblBedNo.setBounds(411, 192, 69, 22);
        fm1.getContentPane().add(lblBedNo);

        textField_5 = new JTextField();
        textField_5.setEnabled(false);
        textField_5.setDisabledTextColor(Color.BLUE);
        textField_5.setColumns(10);
        textField_5.setBounds(499, 193, 79, 22);
        fm1.getContentPane().add(textField_5);

        JLabel lblConsultingDoctor = new JLabel("Consulting Doctor :");
        lblConsultingDoctor.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        lblConsultingDoctor.setBounds(10, 192, 143, 22);
        fm1.getContentPane().add(lblConsultingDoctor);

        textField_6 = new JTextField();
        textField_6.setEnabled(false);
        textField_6.setDisabledTextColor(Color.BLUE);
        textField_6.setColumns(10);
        textField_6.setBounds(138, 193, 226, 22);
        fm1.getContentPane().add(textField_6);


        JSeparator separator = new JSeparator();
        separator.setBounds(0, 243, 1362, 2);
        fm1.getContentPane().add(separator);    

        table = new JTable();
        Connection con = MyConnection.connect();
        try
        {
            String s ="SELECT SNo,INVESTIGATION,PRICE FROM investigation";
            PreparedStatement p =con.prepareStatement(s);
            ResultSet rs = p.executeQuery();
            rs.last();
            cnt = rs.getRow();
            rs.beforeFirst();
            String data[][]=new String[cnt][3];

            while(rs.next())
            {
                data[r][c] = String.valueOf(rs.getInt("SNo")); ++c;
                data[r][c] = rs.getString("INVESTIGATION");++c;
                data[r][c] = String.valueOf(rs.getInt("PRICE")); ++c;


                r=r+1;
                c=0;
            }


             table.setModel(new DefaultTableModel(data,columns) 
             { 
                 public boolean isCellEditable(int row, int column){
                        if(column==0 || column==1 || column==2 )
                        {
                            return false;
                        }
                        return true;
                    }

                 Class[ ] type=new  Class[]{java.lang.String.class , java.lang.String.class ,java.lang.String.class, java.lang.Boolean.class} ;

            public  Class getColumnClass (int columnIndex) 
            { 
                return type [columnIndex] ; 
                    } 

             } ) ;


            setDefaultLookAndFeelDecorated(true);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.setBackground(Color.white);
            table.setGridColor(Color.magenta);
            table.getColumnModel().getColumn(0).setPreferredWidth(80);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            table.getColumnModel().getColumn(2).setPreferredWidth(100);
            table.getTableHeader().setBackground(Color.pink);
            table.setRowSelectionAllowed(true);
            table.getTableHeader().setFont(new Font("SansSerif",Font.BOLD, 15));

            jsp1 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            jsp1.setBackground(Color.white);
            jsp1.setViewportBorder(new MatteBorder(3, 3, 3, 3, (Color) new Color(0, 0, 0)));
            jsp1.getViewport().setBackground(table.getBackground());
            jsp1.setBounds(526, 404, 485, 162);
            fm1.getContentPane().add(jsp1);

            fm1.setVisible(true);
            fm1.setExtendedState(JFrame.MAXIMIZED_BOTH);

        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try {
                con.close();
                } 
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }



    public static void main(String[] args) 
    {
        new investigation() ;       

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288

1 Answers1

1
  1. Add a TableModelListener to the TableModel of the JTable.

  2. When an event is generated for the column containing the check box you make sure the value is Boolean.TRUE. Then you generate the SQL necessary to copy the data to your database.

You can check out: TableModelListener and multiple column validation for a working example that uses a TableModelListener.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288