Results 1 to 3 of 3
  1. #1
    Alfaromeo is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Java AWT CheckBox List

    The below code shows the checkbox list , however the need is to find the list of checkboxes and it's values which are selected. Now only the ones which are highlighted are displayed

    Java Code:
    package com.Download;
    
    package com.Books;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.DefaultListSelectionModel;
    import javax.swing.table.DefaultTableModel;
    
    public class CheckBoxList extends JPanel {
    
    private static final int CHECK_COL = 1;
    private static final Object[][] DATA = {
        {"One", Boolean.TRUE}, {"Two", Boolean.FALSE},
        {"Three", Boolean.TRUE}, {"Four", Boolean.FALSE},
        {"Five", Boolean.TRUE}, {"Six", Boolean.FALSE},
        {"Seven", Boolean.TRUE}, {"Eight", Boolean.FALSE},
        {"Nine", Boolean.TRUE}, {"Ten", Boolean.FALSE}};
    private static final String[] COLUMNS = {"Number", "CheckBox"};
    private DataModel dataModel = new DataModel(DATA, COLUMNS);
    private JTable table = new JTable(dataModel);
    private DefaultListSelectionModel selectionModel;
    
    public CheckBoxList() {
        super(new BorderLayout());
        this.add(new JScrollPane(table));
        this.add(new ControlPanel(), BorderLayout.SOUTH);
        table.setPreferredScrollableViewportSize(new Dimension(250, 175));
        selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
    }
    
    private class DataModel extends DefaultTableModel {
    
        public DataModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
        }
    
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == CHECK_COL) {
                return getValueAt(0, CHECK_COL).getClass();
            }
            return super.getColumnClass(columnIndex);
        }
    
        @Override
        public boolean isCellEditable(int row, int column) {
            return column == CHECK_COL;
        }
    }
    
    private class ControlPanel extends JPanel {
    
        public ControlPanel() {
            this.add(new JLabel("Selection:"));
            this.add(new JButton(new SelectionAction("Clear", false)));
            this.add(new JButton(new SelectionAction("Check", true)));
    
    
    
        }
    }
    
    private class SelectionAction extends AbstractAction {
    
        boolean value;
    
        public SelectionAction(String name, boolean value) {
            super(name);
            this.value = value;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < dataModel.getRowCount(); i++) {
                if (selectionModel.isSelectedIndex(i)) {
                    dataModel.setValueAt(value, i, CHECK_COL);
                    //System.out.println(dataModel.getValueAt(row, column));
                }
            }
        }
    }
    
    private static void createAndShowUI() {
        JFrame frame = new JFrame("CheckABunch");
        frame.add(new CheckBoxList());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
    
            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
     }

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Java AWT CheckBox List

    The checkboxes are the visual representation of the Booleans under the CheckBox column in the table, and so the list of checkboxes is just the values of the cells under this column. You can get the values by going through the table's data model (dataModel).

    Here's something to help you get started:

    Java Code:
    for (int i = 0; i < dataModel.getRowCount(); i++) {
      Boolean checkBoxValue = (Boolean) dataModel.getValueAt(i, COLUMNS.length - 1);
      if (checkBoxValue == Boolean.TRUE) {
        System.out.println("Found check mark at row " + i);
      }
    }

  3. #3
    Alfaromeo is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Java Thread : Checkbox bug in JTABLE .
    By dinu in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2013, 09:31 AM
  2. Replies: 3
    Last Post: 07-27-2010, 10:52 AM
  3. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 05:30 PM
  4. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 09:10 PM
  5. [SOLVED] Checkbox new to Java
    By s34nn4 in forum Java Applets
    Replies: 11
    Last Post: 04-12-2009, 06:21 AM

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
  •