-1

Is there anyone know how I can add a checkbox in this code:

String data[][]={
       {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}
   };
   String column[]={"Fruits","Fruits","Fruits"};
   table=new JTable(new DefaultTableModel(data, column)){
            private Border outside = new MatteBorder(1, 0, 1, 0, Color.RED);
            private Border inside = new EmptyBorder(0, 1, 0, 1);
            private Border highlight = new CompoundBorder(outside, inside);

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
            {
                Component c = super.prepareRenderer(renderer, row, column);
                JComponent jc = (JComponent)c;

                // Add a border to the selected row

                if (isRowSelected(row))
                    jc.setBorder( highlight );

                                return c;
            }
        };


jScrollPane1.setViewportView(table);

I just want to add checkboxes so that if I check a checkbox it will highlight and all checked checkboxes will be highlighted. Thank You in advance for helping me!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2322019
  • 1
  • 1
  • 5
  • I don't understand the question so I will just suggest you read the Swing tutorial on [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for a working example that contains check boxes. – camickr May 30 '13 at 00:13

1 Answers1

0

Here is pseudo code, I found somewhere in my repository. Use it according to your use.

import java.awt.Color;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.*;

public class TableCheckBoxHighLight extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableCheckBoxHighLight() {
        Object[] columnNames = { "Col1", "Col2", "Select" };
        Object[][] data = { 
                            { "Item1", "123", false },
                            { "Item2", "345", false }, 
                            { "Item3", "678", false },
                            { "Item4", "901", false }
                          };
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @SuppressWarnings("unchecked")
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                case 0:
                    return String.class;
                case 1:
                    return String.class;

                default:
                    return Boolean.class;
                }
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer,
                    int row, int col) {
                Component c = super.prepareRenderer(renderer, row, col);

                int[] selCols = table.getSelectedColumns();
                table.setSelectionBackground(Color.GREEN);
                for (int i : selCols)
                    c.setBackground(Color.RED);

                return c;
            }
        };

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableCheckBoxHighLight frame = new TableCheckBoxHighLight();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }
        });
    }
}
Smit
  • 4,685
  • 1
  • 24
  • 28
  • Thanks for the help mr @smit. Could you edit the code so that only the rows with checked checkboxes are highlighted. tnx. – user2322019 Jun 02 '13 at 03:44