Results 1 to 2 of 2
  1. #1
    lakshayghai is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default CheckBoxes and JTables

    My third column contains checkboxes. how can i get which checkbox is selected and which is not selected.
    I have to populate my database with the rows that are checked.
    plz help me. the code is given below

    import java.awt.event.*;
    import java.awt.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.TableColumn;
    import javax.swing.DefaultCellEditor;

    public class TakeAttendance extends JFrame
    {

    Vector columnNames = new Vector();
    Vector data = new Vector();
    Container con = getContentPane();
    JButton ok= new JButton("SUBMIT");
    public TakeAttendance(Object o){
    try
    {
    con.setLayout(new BorderLayout());
    this.setTitle("Take Attendance");
    this.setLocation(new Point(0, 0));
    this.setSize(new Dimension(500, 500));
    String sem=o.toString();
    // Connect to the Database

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineattendance","root","lucky");
    Statement st=conn.createStatement();


    // Read data from a table
    String sql = "Select userID ID, name from users,student where semester='"+sem+"'and users.userId=student.studentID";
    //String sqlq="Select * from student";
    ResultSet rs = st.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();

    // Get column names

    for (int i = 1; i <= columns; i++)
    {
    columnNames.addElement( md.getColumnName(i) );
    }

    // Get row data

    while (rs.next())
    {
    Vector row = new Vector(columns);

    for (int i = 1; i <= columns; i++)
    {
    row.addElement( rs.getObject(i) );

    }
    row.addElement(new Boolean(false));
    data.addElement( row );
    }

    rs.close();
    st.close();
    }
    catch(Exception e)
    {
    System.out.println( e );
    }
    columnNames.addElement("Attendance");



    // Create table with database data

    JTable table = new JTable(data, columnNames){public Class getColumnClass(int column) {
    if(column==2)
    return Boolean.class;
    else
    return super.getColumnClass(column);
    }};
    JScrollPane jsp = new JScrollPane(table);

    TableColumn includeColumn = table.getColumnModel().getColumn(2);
    final JCheckBox check = new JCheckBox();
    includeColumn.setCellEditor(new DefaultCellEditor(check));





    getContentPane().add(jsp,BorderLayout.CENTER);
    getContentPane().add(ok,BorderLayout.SOUTH);
    this.setSize(new Dimension(600, 600));
    ok.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {

    }
    });
    this.setVisible(true);

    }

    }



    plzz help..

    thanks...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Read the API for JTable and look for a method that returns the value at a row and column.

    db

Similar Threads

  1. ComboBox with CheckBoxes
    By heba.farouk in forum AWT / Swing
    Replies: 14
    Last Post: 06-09-2010, 11:03 AM
  2. How to use Mnemonic for CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:45 PM
  3. How to use Swing CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:44 PM
  4. Scrolling with JTables
    By hiranya in forum AWT / Swing
    Replies: 5
    Last Post: 10-30-2007, 08:48 AM
  5. Problem with checkboxes
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •