1

I don't know how to go about i have this frame i wish to populate a JTable and add a checkbox.

public static void update_table() {

        try {

        String sql="SELECT * FROM equipments";
        PreparedStatement update = con.prepareStatement(sql);
        ResultSet result = update.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(result));
        table.setVisible(true);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mcjohn Key
  • 15
  • 8

1 Answers1

2

I assume you mean to want to add an additional column not found in the database containing a check box so you can select rows?

If so then you can use a wrapper TableModel.

Here is an example that might help:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class CheckBoxWrapperTableModel extends AbstractTableModel
{
    private ArrayList<Boolean> checkBoxes = new ArrayList<>();

    private DefaultTableModel model;
    private String columnName;

    public CheckBoxWrapperTableModel(DefaultTableModel model, String columnName)
    {
        this.model = model;
        this.columnName = columnName;

        for (int i = 0; i < model.getRowCount(); i++)
            checkBoxes.add( Boolean.FALSE );
    }

    @Override
    public String getColumnName(int column)
    {
        return (column > 0) ? model.getColumnName(column - 1) : columnName;
    }

    @Override
    public int getRowCount()
    {
        return model.getRowCount();
    }

    @Override
    public int getColumnCount()
    {
        return model.getColumnCount() + 1;
    }

    @Override
    public Object getValueAt(int row, int column)
    {
        if (column > 0)
            return model.getValueAt(row, column - 1);
        else
        {
            Object value = checkBoxes.get(row);
            return (value == null) ? Boolean.FALSE : value;
        }
    }

    @Override
    public boolean isCellEditable(int row, int column)
    {
        if (column > 0)
            return model.isCellEditable(row, column - 1);
        else
            return true;
    }

    @Override
    public void setValueAt(Object value, int row, int column)
    {
        if (column > 0)
            model.setValueAt(value, row, column - 1);
        else
        {
            checkBoxes.set(row, (Boolean)value);
        }

        fireTableCellUpdated(row, column);
    }

    @Override
    public Class getColumnClass(int column)
    {
        return (column > 0) ? model.getColumnClass(column - 1) : Boolean.class;
    }

    public void removeRow(int row)
    {
        checkBoxes.remove(row);
        fireTableRowsDeleted(row, row);
        model.removeRow(row);
    }

    private static void createAndShowGUI()
    {
        //  Create the table with check marks in the first column

        DefaultTableModel model = new DefaultTableModel(5, 1);

        for (int i = 0; i < model.getRowCount(); i++)
        {
            model.setValueAt("" + i, i, 0);
        }

        CheckBoxWrapperTableModel wrapperModel = new CheckBoxWrapperTableModel(model, "Select");
        JTable table = new JTable(wrapperModel);

        //  Add button to delete selected rows

        JButton button = new JButton( "Delete Selected Rows" );
        button.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                for (int i = table.getRowCount() - 1; i >= 0; i--)
                {
                    Boolean selected = (Boolean)table.getValueAt(i, 0);
                    System.out.println(selected + " : " + i);

                    if (selected)
                    {
                        wrapperModel.removeRow(i);
                    }

                }
            }
        });

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane( table ) );
        frame.add( button, BorderLayout.PAGE_END );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

If the model works then you would us the model with code like:

//table.setModel(DbUtils.resultSetToTableModel(result));
TableModel utilsModel = DbUtils.resultSetToTableModel(result);
TableModel wrapperModel = new CheckBoxWrapperTableModel(utilsModel, "Select");
table.setModel( wrapperModel );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • It perfectly works. i don't know if i can't ask some more questions from the project. I have 7 collumns were two are of class blob. I want that any time there is a class of type blob an image appears on the table cell. – Mcjohn Key Sep 09 '15 at 19:53
  • how will i add an acction listener to that checknox please – Mcjohn Key Sep 09 '15 at 19:59
  • Only a single question should be asked otherwise the thread gets too complicated to follow. `I want that any time there is a class of type blob an image appears on the table cell` - then you need to use a custom renderer for the table. Read the JTable API and you will find a link titled `How to Use Tables` that will take you to the Swing tutorial for more information on renderers. – camickr Sep 09 '15 at 20:31
  • @MacjohnKey, `how will i add an acction listener to that checknox` Why? Usually you use the checkbox to check many rows. Then you would have a button on the form to process all the selected rows. So the ActionListener would be added to the button. In any case you don't add an ActionListener to the check box because it is not a real component. You could add a TableModelListener to the table to listen for change of state in the check box. – camickr Sep 09 '15 at 20:32