/*
 * JideTableTest.java
 *
 * Created on 19/09/2011, 09:56:21
 */
package test;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author andremerlo
 */
public class JideTableTest extends javax.swing.JPanel {

    /** Creates new form JideTableTest */
    public JideTableTest() {
        initComponents();
        jButton1.setAction(new DeleteAction());
        jideTable1.setModel(new DefaultTableModel(new Object[][]{
                    new Object[]{
                        "a", "b"
                    },
                    new Object[]{
                        "c", "d"
                    },}, new Object[]{
                    "col1",
                    "col2"
                }));
        jideTable1.setAutoCreateRowSorter(true);
    }

    public class DeleteAction extends AbstractAction {

        public DeleteAction() {
            super("Delete");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int selected = jideTable1.getSelectionModel().getLeadSelectionIndex();
            int selectInModel = jideTable1.convertRowIndexToModel(selected);
            ((DefaultTableModel) jideTable1.getModel()).removeRow(selectInModel);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new JideTableTest());
        frame.pack();
        frame.setVisible(true);
    }

    public class SampleObject {

        public SampleObject(String prop1, String prop2) {
            this.prop1 = prop1;
            this.prop2 = prop2;
        }
        protected String prop1;

        /**
         * Get the value of prop1
         *
         * @return the value of prop1
         */
        public String getProp1() {
            return prop1;
        }

        /**
         * Set the value of prop1
         *
         * @param prop1 new value of prop1
         */
        public void setProp1(String prop1) {
            this.prop1 = prop1;
        }
        protected String prop2;

        /**
         * Get the value of prop2
         *
         * @return the value of prop2
         */
        public String getProp2() {
            return prop2;
        }

        /**
         * Set the value of prop2
         *
         * @param prop2 new value of prop2
         */
        public void setProp2(String prop2) {
            this.prop2 = prop2;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jideTable1 = new com.jidesoft.grid.JideTable();
        jButton1 = new javax.swing.JButton();

        jScrollPane1.setViewportView(jideTable1);

        jButton1.setText("Delete");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(73, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private com.jidesoft.grid.JideTable jideTable1;
    // End of variables declaration//GEN-END:variables
}
