Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By clydedoris

Thread: jcheckbox and jtable

  1. #1
    seredi is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default jcheckbox and jtable

    JCheckBox is checked but it still shows its value as false when i use system.out.print .If the focus is lost but with JCheckBox still checked then it returns true. i.e when i check 2 checkbox the result showed is for first checkbox. the status of second checkbox is not shown. complete prog is shown below: pls run the prog and do rectify my mistake. any help is welcome.

    public class check extends JFrame {
    public check(){
    setBounds(00,40,400,400);
    Color c= new Color(160,200,100);
    getContentPane().setBackground(c);
    Color c3= new Color(0,50,50,2);
    setTitle( "MARKING OF TARGET HABITATION");
    setUndecorated(true);
    getRootPane().setWindowDecorationStyle(JRootPane.P LAIN_DIALOG);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String[] columnNames =
    {"Country", "Capital", "Population in Millions",
    "Democracy"};
    Object[][] data = {
    {"USA", "Washington DC", 280, new Boolean(false)},
    {"Canada", "Ottawa", 32,new Boolean(false)},
    {"United Kingdom", "London", 60, new Boolean(false)},
    {"Germany", "Berlin", 83, new Boolean(false)},
    {"France", "Paris", 60, new Boolean(false)},
    {"Norway", "Oslo", 4.5, new Boolean(false)},
    {"India", "New Deli", 1046,new Boolean(false)}
    };

    final JTable table = new JTable(data, columnNames){
    public boolean isCellEditable(int rowIndex, int colIndex) {
    return (colIndex==3);//Disallow the editing of any cell
    } };
    table.getColumnModel().getColumn(3).setCellEditor( new CheckBoxCellEditor());
    table.getColumnModel().getColumn(3).setCellRendere r(new
    CWCheckBoxRenderer());
    JScrollPane scrollPane = new JScrollPane(table);
    JButton button= new JButton("check");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
    boolean[] rsel= new boolean[table.getRowCount()];
    for(int i=0;i<table.getRowCount();i++){
    rsel[i]=(boolean) table.getValueAt(i, 3);
    if(rsel[i]){
    System.out.println("checkbox selected at row" +" "+i+"is"+"
    "+rsel[i]);
    for(int col=0;col<=3;col++){
    table.getValueAt(i, col);
    System.out.println("data at row" +" "+i+"is"+"
    "+table.getValueAt(i, col));
    }
    }
    }
    }
    });
    JPanel buttonpanel= new JPanel();
    buttonpanel.add(button);
    add(scrollPane,BorderLayout.CENTER);
    add(buttonpanel,BorderLayout.SOUTH);
    Color c1= new Color(160,200,100);
    table.setBackground(c1);
    buttonpanel.setBackground(c1);
    setBackground(c1);
    setVisible(true);
    }

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

    }
    class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
    private static final long serialVersionUID = 1L;
    protected JCheckBox checkBox;
    public CheckBoxCellEditor() {
    checkBox = new JCheckBox();
    checkBox.setHorizontalAlignment(SwingConstants.CEN TER);
    // checkBox.setBackground( Color.white);
    }

    public Component getTableCellEditorComponent(
    JTable table,
    Object value,
    boolean isSelected,
    int row,
    int column) {
    checkBox.setSelected(((Boolean) value).booleanValue());
    return checkBox;

    }
    public Object getCellEditorValue() {
    return Boolean.valueOf(checkBox.isSelected());
    }

    }

    class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
    private static final long serialVersionUID = 1L;
    Border border = new EmptyBorder(1,2,1,2);
    public CWCheckBoxRenderer() {
    super();
    setOpaque(true);
    setHorizontalAlignment(SwingConstants.CENTER);
    }
    public Component getTableCellRendererComponent(
    JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column) {
    if (value instanceof Boolean) {
    setSelected(((Boolean)value).booleanValue());
    //setEnabled(table.isCellEditable(row, column));
    setForeground(table.getForeground());
    setBackground(table.getBackground());

    }return this;
    }
    }
    }

  2. #2
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default Re: jcheckbox and jtable

    at least format your code because it is really hard to read.
    quad64bit likes this.
    [why are you annoyed with my sig?]

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Re: jcheckbox and jtable

    I have checked your code.And its working. you did some casting mistakes.


    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    
    public class check extends JFrame {
    public check(){
    setBounds(00,40,400,400);
    Color c= new Color(160,200,100);
    getContentPane().setBackground(c);
    Color c3= new Color(0,50,50,2);
    setTitle( "MARKING OF TARGET HABITATION");
    setUndecorated(true);
    getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    String[] columnNames =
    {"Country", "Capital", "Population in Millions",
    "Democracy"};
    Object[][] data = {
    {"USA", "Washington DC", 280, new Boolean(false)},
    {"Canada", "Ottawa", 32,new Boolean(false)},
    {"United Kingdom", "London", 60, new Boolean(false)},
    {"Germany", "Berlin", 83, new Boolean(false)},
    {"France", "Paris", 60, new Boolean(false)},
    {"Norway", "Oslo", 4.5, new Boolean(false)},
    {"India", "New Deli", 1046,new Boolean(false)}
    };
    
    final JTable table = new JTable(data, columnNames){
    public boolean isCellEditable(int rowIndex, int colIndex) {
    return (colIndex==3);//Disallow the editing of any cell
    } };
    table.getColumnModel().getColumn(3).setCellEditor( (TableCellEditor) new CheckBoxCellEditor());
    table.getColumnModel().getColumn(3).setCellRenderer((TableCellRenderer) new CWCheckBoxRenderer());
    JScrollPane scrollPane = new JScrollPane(table);
    JButton button= new JButton("check");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
    boolean[] rsel= new boolean[table.getRowCount()];
    for(int i=0;i<table.getRowCount();i++){
          rsel[i]=(Boolean) table.getValueAt(i, 3);
    if(rsel[i]){
    System.out.println("checkbox selected at row" +" "+i+"is"+" "+rsel[i]);
    for(int col=0;col<=3;col++){
    table.getValueAt(i, col);
    System.out.println("data at row" +" "+i+"is"+" "+table.getValueAt(i, col));
    }
    }
    }
    }
    });
    JPanel buttonpanel= new JPanel();
    buttonpanel.add(button);
    add(scrollPane,BorderLayout.CENTER);
    add(buttonpanel,BorderLayout.SOUTH);
    Color c1= new Color(160,200,100);
    table.setBackground(c1);
    buttonpanel.setBackground(c1);
    setBackground(c1);
    setVisible(true);
    }
    
    public static void main(String args[]){
    new check();
    
    }
    
        private void r(CWCheckBoxRenderer cWCheckBoxRenderer) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
    private static final long serialVersionUID = 1L;
    protected JCheckBox checkBox;
    public CheckBoxCellEditor() {
    checkBox = new JCheckBox();
    checkBox.setHorizontalAlignment(SwingConstants.CENTER);
    // checkBox.setBackground( Color.white);
    }
    
    public Component getTableCellEditorComponent(
    JTable table,
    Object value,
    boolean isSelected,
    int row,
    int column) {
    checkBox.setSelected(((Boolean) value).booleanValue());
    return checkBox;
    
    }
    public Object getCellEditorValue() {
    return Boolean.valueOf(checkBox.isSelected());
    }
    
    }
    
    class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
    private static final long serialVersionUID = 1L;
    Border border = new EmptyBorder(1,2,1,2);
    public CWCheckBoxRenderer() {
    super();
    setOpaque(true);
    setHorizontalAlignment(SwingConstants.CENTER);
    }
    public Component getTableCellRendererComponent(
    JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column) {
    if (value instanceof Boolean) {
    setSelected(((Boolean)value).booleanValue());
    //setEnabled(table.isCellEditable(row, column));
    setForeground(table.getForeground());
    setBackground(table.getBackground());
    
    }return this;
    }
    }
    }
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

Similar Threads

  1. Adding JCheckBox to JTable
    By loveboat.adam@gmail.com in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 05:39 AM
  2. JCheckbox in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 09-07-2010, 06:29 AM
  3. JCheckbox in a Jtable Column
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 01:45 PM
  4. How to add a jcheckbox in jtable
    By lakshayghai in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2010, 12:32 AM
  5. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 08:40 PM

Tags for this Thread

Posting Permissions

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