Tuesday, December 31, 2013

Distribute java application to users

Create executable jar file for your application first. If you have multiple jars to distribute, you may consider to use One-JAR to wrap them into one, though for many installers this is not necessary. After you have done this, there are many options to distribute your application. Here list only a few of them.

You can use Java Web Start to distribute and launch your application, which automatically updates your application on the user's system whenever you make an edition. You can also use other application launchers such as listed below to produce an executable file for your users.

1. use Launch4j to distribute and launch your application.

2. use Inno Setup to distribute your application

3. use IzPack to distribute your application

You can now let your users download the executable through Internet or obtain a CD.

A. Do the following if you decide to let your users to download from Internet.

     1. Compress the executable and all other required files into a zip file.

     2. Create a link for download on your website:
          <a href="path/file.zip">Download</a>

B. If you would like to make a CD that automatically launches, do the following.

     1. Create a text file using the notepad and name it Autorun.inf. This is the file that Windows automatically looks for when a CD-Rom is placed into your system. This file should have the following content.
       
         [autorun]
         open=file.exe
         icon=file2.ico

     2. Burn the autorun CD and add the Autorun.inf file to the main directory of your CD. Make sure to check the Automatic option for disc autorun capable or bootable if there is one while burning.

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.


Friday, December 27, 2013

Use reflection to dynamically invoke methods

This sample code uses class reflection associated with an ActionListener to dynamically invoke the specified method at runtime. The method name and parameters can be set differently under different conditions and for different source objects.

In the code, class MethodRelection has three JTextField fields and an objModified method. When any of the JTextField fields is modified, the objModified method will be called to handle the modification accordingly.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MethodReflection extends JFrame{
    public MethodReflection() {
        JTextField[] data = {new JTextField("Peach", 20),
                                  new JTextField("Papaya", 20),
                                  new JTextField("Mango", 20)};
        JPanel panel = new JPanel(new GridLayout(3, 1));
     
        for (int i=0; i<data.length; i++){
            try {
                data[i].addActionListener(new ObjectActionListener(this,
                                                   "objModified",
                                                   new Object[]{data[i], "Field "+(i+1)+" Modified"}
                ));
                panel.add(data[i]);
            } catch (NoSuchMethodException e){
                System.out.println(e.getMessage());
            }
        }
     
        this.add(panel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700, 300);
        this.pack();
    }
 
    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                (new MethodReflection()).setVisible(true);
            }
        });
     
     
    }
 
    public class ObjectActionListener implements ActionListener {
        private Object methodSource = null;
        private Object[] parameters = null;
        private String methodName = null;
        private Method method = null;

        public ObjectActionListener(Object methodSource, String methodName, Object[] parameters)
                throws NoSuchMethodException, SecurityException {
            this.methodSource = methodSource;
            this.methodName = methodName;
            if (parameters == null) {
                parameters = new Object[0];
            }
            this.parameters = parameters;
            method = getMethod();
        }

        private Method getMethod() throws NoSuchMethodException {
            Class theClass = methodSource.getClass();

            Class[] paramTypes = new Class[parameters.length];
            for (int i = 0; i < parameters.length; i++) {
                paramTypes[i] = parameters[i].getClass();
            }
            return theClass.getMethod(methodName, paramTypes);
        }

        public void actionPerformed(ActionEvent event) {
            try {
                method.invoke(methodSource, parameters);
            } catch (IllegalAccessException e) {
                System.out.println(e.getMessage());
            } catch (InvocationTargetException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public void objModified(JTextField obj, String message){
            String text = obj.getText();
            System.out.println(message+": "+text);
    }
}

The code using MethodHandle to dynamically invoke method can be found here

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

TableCellEditor that implements ItemListener

This sample code uses the JComboBox as the component.

public class MyComboBoxEditor extends DefaultCellEditor implements PopupMenuListener, ItemListener {
        Component comp = null;
        int theRow = 0;
        int theCol = 0;
        public MyComboBoxEditor(JComboBox jcb) {
            super(jcb);
        }

        @Override
        public Component getTableCellEditorComponent(
                JTable table, Object value, boolean isSelected, int row, int column) {

            //critical to set the row and col before calling
            //super.getTableCellEditorComponent which fires the ItemListener
            //for the ItemListener to work properly not using previous row and col.
            theRow = row;
            theCol = column;
            comp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
         
            ((JComboBox)comp).removeItemListener(this);
            ((JComboBox)comp).addItemListener(this);
            ((JComboBox)comp).removePopupMenuListener(this);
            ((JComboBox)comp).addPopupMenuListener(this);
         
            return comp;
        }
     
        @Override
        public void popupMenuCanceled(PopupMenuEvent e){
            table.stopCellEditing();
        }
   
        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            table.stopCellEditing();
        }
     
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        }
     
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e == null) {
                return;
            }
            JComboBox source = (JComboBox) e.getSource();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                  Object obj = e.gtItem();
                  //table.editCellAt(theRow, theCol); need this because of the calling table.stopCellEditing() above
                  //code for modifying the JTable related to theRow and theCol
           }
           e = null
      }
}

----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Monday, December 16, 2013

JTable Freezing/Fixing column headers or rows while scrolling


import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestClass extends javax.swing.JFrame {
    private DefaultTableModel tableModel;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTable jTable;
 
    public TestClass() {
        jScrollPane = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jScrollPane.setViewportView(jTable);
        getContentPane().add(jScrollPane);
     
        setPreferredSize(new Dimension(300,120));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
        Object[][] data = {{"Item1", "Papaya", "Letus", "Cashew", "Pine"},
                             {"Item2", "Orange", "Carrot", "Pine nut", "Oak"},
                             {"Item3", "Apple", "Pepper", "Pistacho", "Cypress"},
                             {"Item4", "Mango", "Tomato", "Almond", "Cedar"},
                             {"Item5", "Pear", "Romman", "Brazil", "Palm"}};
        Object[] headers = {"Item", "Fruit", "Vegetable", "Nuts", "Tree"};
        tableModel = new DefaultTableModel(data, headers);
        jTable.setModel(tableModel);
     
        jTable.setAutoCreateColumnsFromModel( false );
        for (int i=0; i<jTable.getColumnCount(); i++){
            jTable.getColumnModel().getColumn(i).setMinWidth(100);
        }
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        jScrollPane.setColumnHeaderView(jTable.getTableHeader());
     
        pack();
    }
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestClass().setVisible(true);
            }
        });
    }
}

See also
JTable freezing/fixing columns when scrolling

-------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

JTable freezing/fixing columns when scrolling

Following is the sample code for freezing one or more table columns while scrolling through the other columns.

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TestClass extends javax.swing.JFrame {
    private DefaultTableModel tableModel;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTable jTable;
    private javax.swing.JTable freezeTable;
    private int fixedColumns = 1;//number of colums to be freezed
 
    public TestClass() {
        jScrollPane = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jScrollPane.setViewportView(jTable);
        getContentPane().add(jScrollPane);
     
        setPreferredSize(new Dimension(300,200));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
        Object[][] data = {{"Item1", "Papaya", "Letus", "Cashew", "Pine"},
                             {"Item2", "Orange", "Carrot", "Pine nut", "Oak"},
                             {"Item3", "Apple", "Pepper", "Pistacho", "Cypress"}};
        Object[] headers = {"Item", "Fruit", "Vegetable", "Nuts", "Tree"};
        tableModel = new DefaultTableModel(data, headers);
        jTable.setModel(tableModel);
     
        jTable.setAutoCreateColumnsFromModel( false );
        for (int i=0; i<jTable.getColumnCount(); i++){
            jTable.getColumnModel().getColumn(i).setMinWidth(100);
        }
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                //  Keep freezeTable in sync with the jTable
                if ("selectionModel".equals(e.getPropertyName())) {
                    freezeTable.setSelectionModel(jTable.getSelectionModel());
                }

                if ("dataModel".equals(e.getPropertyName())) {
                    freezeTable.setModel(jTable.getModel());
                }
            }
        });
     
        freezeTable = new javax.swing.JTable();
        freezeTable.setAutoCreateColumnsFromModel(false);
        freezeTable.setModel(tableModel);
        freezeTable.setSelectionModel(jTable.getSelectionModel());
        freezeTable.setFocusable(false);
     
        for (int i = 0; i < fixedColumns; i++) {
            TableColumnModel colModel = jTable.getColumnModel();
            TableColumn column = colModel.getColumn(0);
            colModel.removeColumn(column);
            freezeTable.getColumnModel().addColumn(column);
        }

        //Synchronize sorting of freezeTable with jTable
        jTable.setAutoCreateRowSorter(true);
        freezeTable.setRowSorter(jTable.getRowSorter());
        jTable.setUpdateSelectionOnSort(true);
        freezeTable.setUpdateSelectionOnSort(false);

        //  Add the fixed table to the scroll pane
        freezeTable.setPreferredScrollableViewportSize(freezeTable.getPreferredSize());
        jScrollPane.setRowHeaderView(freezeTable);
        jScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, freezeTable.getTableHeader());

        // Synchronize scrolling of the row header with the jTable
        jScrollPane.getRowHeader().addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                //  Sync the scroll pane scrollbar with the row header
                JViewport viewport = (JViewport) e.getSource();
                jScrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
            }
        });
        pack();
    }
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestClass().setVisible(true);
            }
        });
    }
}

See also
JTable Freezing/Fixing column headers while scrolling

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, December 13, 2013

JTable sort by clicking column header

Sample code for sorting a column by clicking the column header.

import java.awt.Dimension;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class TestClass extends javax.swing.JFrame {
    private DefaultTableModel tableModel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
 
    public TestClass() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1.setViewportView(jTable1);
        getContentPane().add(jScrollPane1);
     
        setPreferredSize(new Dimension(500,300));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
     
        Object[] headers = {"Item", "Fruit"};
        tableModel = new DefaultTableModel();
        tableModel.setColumnIdentifiers(headers);
        Vector row1 = new Vector();
        row1.add("Item1");
        row1.add("Papaya");
        Vector row2 = new Vector();
        row2.add("Item2");
        row2.add("Orange");
        Vector row3 = new Vector();
        row3.add("Item3");
        row3.add("Apple");
        tableModel.addRow(row1);
        tableModel.addRow(row2);
        tableModel.addRow(row3);
     
        jTable1.setModel(tableModel);
     
        jTable1.setAutoCreateRowSorter(true);
        TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(tableModel);
        sorter.setComparator(1, new SorterComparator<String>());
        jTable1.setRowSorter(sorter);
    }
 
    private class SorterComparator<E> implements Comparator {
        public int compare(Object s1, Object s2){
            return ((String)s1).compareTo((String)s2);
        }
    }
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestClass().setVisible(true);
            }
        });
    }
}

Further Readings:
1. JTable sort the content of a column

-------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

JTable sort the content of a column

Here is the sample code

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

public class NewJFrame extends javax.swing.JFrame {
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private MyTableModel tableModel;
 
    public NewJFrame() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1.setViewportView(jTable1);
        getContentPane().add(jScrollPane1);
     
        JButton button = new JButton("Sort Fruit");
        JPanel panel = new JPanel();
        panel.add(button, null);
        this.add(panel, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(500,300));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
     
        Object[] headers = {"Item", "Fruit"};
        tableModel = new MyTableModel();
        tableModel.setColumnIdentifiers(headers);
        SortableVector row1 = new SortableVector();
        row1.add("Item1");
        row1.add("Papaya");
        SortableVector row2 = new SortableVector();
        row2.add("Item2");
        row2.add("Orange");
        SortableVector row3 = new SortableVector();
        row3.add("Item3");
        row3.add("Apple");
        tableModel.addRow(row1);
        tableModel.addRow(row2);
        tableModel.addRow(row3);
     
        jTable1.setModel(tableModel);
     
        button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               tableModel.sort();
               tableModel.fireTableDataChanged();
           }
        });
    }
 
    public class MyTableModel extends DefaultTableModel {
        public void sort() {
            Vector<SortableVector> data = getDataVector();
            Collections.sort(data);
        }
    }
 
    public class SortableVector<E> extends Vector implements Comparable {

        public SortableVector() {
        }

        @Override
        public int compareTo(Object o) { //sort on column 1
            Vector row2 = (SortableVector) o;
            String value1 = (String)get(1);
            String value2 = (String)row2.get(1);
         
            return value1.compareTo(value2);
        }
    }

Further Readings:
1. JTable sort by clicking column header

-------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

JTable automatically scroll cell to view when tabbing out of view

/**
     * When tabbing out of the view, scroll the selected cell to the closest edge of the view.
     * If a tab or mouse click is within the view, no scroll will happen
     * @param rowIndex the row of the selected cell
     * @param vColIndex the column of the selected cell
     */
    public void scrollCellToView(int rowIndex, int vColIndex) {
        if (!(this.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport) this.getParent();
        Rectangle rect = this.getCellRect(rowIndex, vColIndex, true);
        Rectangle viewRect = viewport.getViewRect();
     
        int x = viewRect.x;
        int y = viewRect.y;
     
        if (rect.x >= viewRect.x && rect.x <= (viewRect.x + viewRect.width - rect.width)){
         
        } else if (rect.x < viewRect.x){
            x = rect.x;
        } else if (rect.x > (viewRect.x + viewRect.width - rect.width)) {
            x = rect.x - viewRect.width + rect.width;
        }
     
        if (rect.y >= viewRect.y && rect.y <= (viewRect.y + viewRect.height - rect.height)){
         
        } else if (rect.y < viewRect.y){
            y = rect.y;
        } else if (rect.y > (viewRect.y + viewRect.height - rect.height)){
            y = rect.y - viewRect.height + rect.height;
        }
     
        viewport.setViewPosition(new Point(x,y));
    }

-----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Using ButtonGroup for JRadioButtons in JTable

In the case of using JCheckBox in JTable, it is nice to just put the Boolean value in the table model and let the DefaultTableCellRenderer and the DefaultCellEditor to automatically do the job for you. And you don't have to do anything more than just putting the Boolean values in the table model, modifying the getColumnClass method to return Boolean.class for the column, and modifying the isCellEditable method to return true for this column in your table model.

However, when it comes to the case of using JRadioButtons in JTable, the story changes due to the need of exclusive selection. The DefaultTableCellRenderer and the DefaultCellEditor use the same JRadioButton to render and edit all the cells. You have to loop through the table to manually set the Boolean value to false each time a new selection is made. To avoid this, the JRadioButtons can be directly put into the table model, and make the cell renderer and editor to use this same JRadioButton to render and edit the cell. So, these radiobuttons can be added to one or more ButtonGroups for exclusive selection. The trick here is that you need a MouseListener added to your JTable to set selection for the ButtonGroup.

Following is the sample code.

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class NewJFrame extends javax.swing.JFrame {            
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;  
    private ButtonGroup radioGroup = new ButtonGroup();
 
    public NewJFrame() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1.setViewportView(jTable1);
        getContentPane().add(jScrollPane1);

        setPreferredSize(new Dimension(500,300));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
     
        JRadioButton bt1 = new JRadioButton();
        JRadioButton bt2 = new JRadioButton();
        JRadioButton bt3 = new JRadioButton();
        radioGroup.add(bt1);
        radioGroup.add(bt2);
        radioGroup.add(bt3);
     
        Object[][] data = {{"Item1", bt1}, {"Item2", bt2}, {"Item3", bt3}};
        Object[] headers = {"Name", "Select"};
        MyTableModel tableModel = new MyTableModel();
        tableModel.setDataVector(data, headers);
        jTable1.setModel(tableModel);
     
        RadioButtonRendererEditor re = new RadioButtonRendererEditor();
        jTable1.getColumn("Select").setCellRenderer(re);
        jTable1.getColumn("Select").setCellEditor(re);
        jTable1.addMouseListener(new TableListener());
        tableModel.fireTableDataChanged();
    }
 
    public class MyTableModel extends DefaultTableModel {

        @Override
        public Class getColumnClass(int colIndex) {
            Class type = Object.class;
            if (colIndex == 1) {
                type = JRadioButton.class;
            }
            return type;
        }
     
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex){
            if (columnIndex == 1){
                return true;
            } else {
                return false;
            }
        }
    }
 
    public class TableListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e){
            int row = jTable1.rowAtPoint(e.getPoint());
            int col = jTable1.columnAtPoint(e.getPoint());
         
            if (col == 1){// the radiobutton column
                JRadioButton bt = (JRadioButton)jTable1.getValueAt(row, 1);
               // radioGroup.setSelected(radioGroup.getSelection(), false);

                ButtonModel bm = radioGroup.getSelection();
                ButtonModel btM = bt.getModel();
                if (bm != null && !bm.equals(btM)){
                        bm.setSelected(false);
                        bm.setArmed(false);
                        bm.setPressed(false);
                }

                radioGroup.setSelected(btM, true);
             
               ((MyTableModel)jTable1.getModel()).fireTableDataChanged();
            }
        }
    }
 
    public class RadioButtonRendererEditor extends DefaultCellEditor
            implements TableCellRenderer, TableCellEditor, ItemListener {

        private JRadioButton field;

        public RadioButtonRendererEditor() {
            super(new JCheckBox());
        }

        public RadioButtonRendererEditor(JCheckBox checkBox) {
            super(checkBox);
        }

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected,
                boolean hasFocus,
                int row, int col) {

            if (value instanceof JRadioButton) {
                field = (JRadioButton) value;
                field.setHorizontalAlignment(SwingConstants.CENTER);
                field.setBackground(Color.WHITE);
            }
            return field;
        }

        @Override
        public Component getTableCellEditorComponent(
                JTable table, Object value, boolean isSelected, int row, int column) {

            field = (JRadioButton) value;
            field.addItemListener(this);
            return field;
        }

        @Override
        public void itemStateChanged(ItemEvent e) {
            super.fireEditingStopped();
        }

        @Override
        public Object getCellEditorValue() {
            field.removeItemListener(this);
            return field;
        }

        public JRadioButton getValue() {
            return field;
        }

        public void setValue(JRadioButton panel) {
            field = panel;
        }
    }
             
}

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.