1

I am creating a JTable in swing and I have managed to create a table that is scrollable with some random data added to it. Now I ma trying to add a check box in the last column of every row.

Now this is what I am getting so far:

Screenshot of Table

When you look at he table you can see that instead of getting checkboxes I am getting a column with a string value "false" which is not what I want. I want a selectable box and I want to allow multiple selections. This is the code from the class:

import java.awt.BorderLayout; 
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;


public class BiogramTable extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JCheckBox checkbox;
    private static final long serialVersionUID = 1L; 




    /**
     * Create the frame.
     */
    public BiogramTable() {
        Object[] columns = {"Name", "Age" , "Gender" , "Boolean"};

        Object[][] data = {{"John", "18", "Male", false}, 
                {"Jessica", "19", "Female", false}, 
                {"Dave", "52", "Male", false},
                {"Jake", "30", "Male", false}, 
                {"Jeremy", "14", "Male", false},
                {"Jemma", "34", "Female", false},
                {"Amy", "16", "Female", false},
                {"Patrick", "18", "Male", false}};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        final JCheckBox checkBox = new JCheckBox();


        table = new JTable(model) {

            private static final long serialVersionUID = 1L;


            //@Override
            //public Class getColumnClass(int column) {
                //switch (column) {
                //case 0:
                //  return String.class;
                //case 1:
                    //return String.class;
                //case 2:
                    //return Integer.class;
                //case 3:
                    //return Double.class;
                //default:
                    //return Boolean.class;
                //}
            //}

        };

        //table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                BiogramTable frame = new BiogramTable();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }

        });
    }
}

I have done a lot of research on this but I haven't managed to find a solution using swing. I would be great if you could let me know what is wrong with my method and let me know what the correct method is as this is the first time I am creating a JTable in Java swing using Windows builder. I have seen a similar question to mine in stack overflow but my problem is different as I am getting String rather than checkboxes. So my question is why is this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
David Gardener
  • 93
  • 2
  • 13

1 Answers1

3

You should Override getColumnClass method according to your data requirement. Refer to below changes.

table image

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;

class BiogramTable extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JCheckBox checkbox;
    private static final long serialVersionUID = 1L;

    /**
     * Create the frame.
     */
    public BiogramTable() {
        Object[] columns = {"Name", "Age", "Gender", "Boolean"};

        Object[][] data = {{"John", "18", "Male", false},
        {"Jessica", "19", "Female", false},
        {"Dave", "52", "Male", false},
        {"Jake", "30", "Male", false},
        {"Jeremy", "14", "Male", false},
        {"Jemma", "34", "Female", false},
        {"Amy", "16", "Female", false},
        {"Patrick", "18", "Male", false}};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        final JCheckBox checkBox = new JCheckBox();

        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return Integer.class;
                    case 2:
                        return String.class;
                    case 3:
                        return Boolean.class;
                    default:
                        return String.class;
                }
            }
        };

        //table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                BiogramTable frame = new BiogramTable();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }

        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Akila
  • 1,258
  • 2
  • 16
  • 25
  • I don't have a getColumn class yet which is why I commented it out. – David Gardener Oct 27 '17 at 09:36
  • But I don't that, that is the problem as to why I am getting strings rather than checkboxes – David Gardener Oct 27 '17 at 09:37
  • Then tell me what is you really need ? do u want to get check boxes values (Ex .true of false) or something else ? – Akila Oct 27 '17 at 09:38
  • I basically want checkboxes at the end of every row so in the last column and I want to be able to select the rows of interest by selecting the check box and then I'm going to add a button that will copy the selected rows into a text file – David Gardener Oct 27 '17 at 09:41
  • 1
    then `table.getValueAt(table.getSelectedRow(), 3)` will simply return check box values (will return `true` or `false` ) – Akila Oct 27 '17 at 09:47