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

    Default How to add a jcheckbox in jtable

    I have created a table which is being populated at runtime from a database.
    The third column contains checkboxes, but the the checkbox are not visible.
    they are only visible when we click the cell. value is shown true/false. I want the checkbox to be displayed with a check n uncheck.

    This is my code:-

    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();
    public TakeAttendance(Object o){
    try
    {
    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) );
    }

    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);
    JScrollPane jsp = new JScrollPane(table);

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



    getContentPane().add( jsp);
    this.setSize(new Dimension(600, 600));
    this.setVisible(true);
    /* JPanel buttonPanel = new JPanel();
    getContentPane().add( buttonPanel, BorderLayout.SOUTH ); */
    }
    }


    plz help...

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

    Default

    You set an editor but not a renderer, which is why you are getting this behavior. But you don't need to do either, just override getColumnClass to return Boolean.class for the column(s) where you want a check box to be used as renderer/editor.

    db

  3. #3
    ankita is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default storing values of checkbox in database

    hey..hii...
    i have a form which contains a list of checkboxes...on the next page i am displaying the list of the checkboxes or their names which the user checked by storing it in an array...
    the code for it as follows:-
    <%!Integer i;
    String[] symptom; %>
    You have selected:
    <%
    symptom = request.getParameterValues("symptom");
    if (symptom != null)
    {
    for (int i = 0; i < symptom.length; i++)
    {
    out.println ("<b>"+symptom[i]+"<b>");
    }
    }
    else out.println ("<b>none<b>");
    %>


    now i want to store all these checked values in a table at backend ie oracle...
    im not getting how i am suppose to get the values from the array n send them to the database...need help on this....plz if any1 can help..
    thanx...

  4. #4
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Can you post the describe of the table used in the database
    And the columns in the JTable as well as the component per column (if default leave it blank).
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  5. #5
    ankita is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default reply

    the table columns are
    seq_no number primary key
    s1 varchar2 not null
    s2 varchar2 not null
    s3 varchar2 not null
    s4 varchar2 not null

    i want to insert the respective values of checkboxes in their respective columns....if i enable s1 n s3 checkbox then values should get stored in s1 n s3 columns of table...

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    what values are stored in s1 - s4 precisely? (true or false)?

    Let i = {1, 2, 3, 4} // repesents s1 - s4
    if you, you should use Boolean.parseBoolean([s[i].getObject(col_no).toString()]) and Boolean.toString(JCheckBox[i].isSelected)
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  7. #7
    ankita is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default reply

    i did not understand the statement that you sent in your last reply...
    can you explain it if possible.....or just send in one example where this statement is used...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ankita: Why are you hijacking someone else's thread? Why didn't you start your own thread for your own question?

  9. #9
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    Ankita: Why are you hijacking someone else's thread? Why didn't you start your own thread for your own question?
    What do you mean? This thread is a replica?


    Quote Originally Posted by ankita View Post
    i did not understand the statement that you sent in your last reply...
    can you explain it if possible.....or just send in one example where this statement is used...
    Just substitute the s[i] to s1 -> s4. I just converted it into an object array to generalize the code. Anyways, you could have it to a long way.

    What was your question anyway, How to add JCheckBox in a JTable or set the Selection of the JCheckBox?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chyrl View Post
    What do you mean? This thread is a replica?
    The thread was started in March by lakshayghai and is a Swing related thread located in the Swing forum. The current discussion started by ankita has nothing to do with the original post and absolutely nothing to do with Swing, and as such, I've a mind to lock this. The hijacker (ankita) should start his/her own thread.
    Last edited by Fubarable; 04-14-2010 at 11:45 PM.

Similar Threads

  1. How can I add JCheckBox to each row?
    By batya in forum AWT / Swing
    Replies: 1
    Last Post: 11-04-2009, 09:25 PM
  2. JCheckbox in Jtree
    By shajuantony in forum AWT / Swing
    Replies: 10
    Last Post: 09-09-2009, 10:42 AM
  3. How to add JCheckbox as a node in JTree
    By shajuantony in forum New To Java
    Replies: 0
    Last Post: 04-09-2009, 07:19 AM
  4. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 07:40 PM
  5. jcheckbox issues need help. thanks.
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 10:37 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
  •