Table Cell editing code in Swing giving wrong output

60 views Asked by At

I have written this code but its not displaying the dialogue box on click. I think the row is getting selected, but the button is not getting clicked.

public class AfterLogin extends javax.swing.JFrame  {

    public AfterLogin() {
        initComponents();
        this.getContentPane().setBackground(Color.white);

        FetchTable();
    }


    private void FetchTable()
    {
        try
            {
                ArrayList det=new ArrayList();
                ArrayList rid=new ArrayList();
                ArrayList sno=new ArrayList();
                ArrayList name=new ArrayList();
                ArrayList dat=new ArrayList();
                ArrayList sta=new ArrayList();
                ArrayList lati=new ArrayList();
                ArrayList longi=new ArrayList();
                ArrayList loca=new ArrayList();
                ArrayList hrate=new ArrayList();
                ArrayList speed=new ArrayList();


                String tablereq="userequesttable";
                String tableinfo="userinfotable";
                Connection con=xod.connection.ConnectionFactory.emergencyConnection();
                PreparedStatement streq=con.prepareStatement("select * from "+tablereq);

                ResultSet rsreq=streq.executeQuery();

                String id;
                ResultSet rsinfo;
                PreparedStatement stinfo;

                while(rsreq.next())
                {
                stinfo=con.prepareStatement("select Name from "+tableinfo+" where DeviceID=?");

                id=rsreq.getString("DeviceID");
                stinfo.setString(1,id);
                rsinfo=stinfo.executeQuery();
                while(rsinfo.next())
                {
                name.add(rsinfo.getString("Name"));
                }
                }           

                rsreq.beforeFirst();
                while(rsreq.next())
                {
                    det.add("View Details");
                    rid.add(rsreq.getString("RequestID"));
                    sno.add(rsreq.getString("SerialNO"));
                    dat.add(rsreq.getString("DateAndTime"));
                    sta.add(rsreq.getString("Status"));
                    lati.add(rsreq.getString("Latitude"));
                    longi.add(rsreq.getString("Longitude"));
                    loca.add(rsreq.getString("Location"));
                    hrate.add(rsreq.getString("HeartRate"));
                    speed.add(rsreq.getString("SpeedAndDirection"));

                }   


                DefaultTableModel dtm=(DefaultTableModel)jTable1.getModel();
                dtm.setRowCount(0);

                for(int i=0;i<rid.size();i++)
                {
                    Object records[]={det.get(i),sno.get(i),rid.get(i),name.get(i),dat.get(i),sta.get(i),lati.get(i),longi.get(i),loca.get(i),"",hrate.get(i),speed.get(i)};
                    dtm.addRow(records);
                }

                jTable1.getColumn("Details").setCellRenderer(new ButtonRenderer());
                jTable1.getColumn("Details").setCellEditor(new ButtonEditor(new JCheckBox()));
                jTable1.getColumnModel().getColumn(0).setPreferredWidth(70);

            }catch( SQLException ex)
            {
                ex.printStackTrace();
            }
}

public static void main(String args[]) {


        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AfterLogin().setVisible(true);
            }
        });
    }



    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;

}


class ButtonRenderer extends JButton implements TableCellRenderer {

    public ButtonRenderer() {
        setOpaque(true);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(UIManager.getColor("Button.background"));
        }
        setText((value == null) ? "" : value.toString());
        return this;
    }
}

class ButtonEditor extends DefaultCellEditor {

    protected JButton button;
    private String label;
    private boolean isPushed;

    public ButtonEditor(JCheckBox checkBox) {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        if (isSelected) {
            button.setForeground(table.getSelectionForeground());
            button.setBackground(table.getSelectionBackground());
        } else {
            button.setForeground(table.getForeground());
            button.setBackground(table.getBackground());
        }
        label = (value == null) ? "" : value.toString();
        button.setText(label);
        isPushed = true;
        return button;
    }

    @Override
    public Object getCellEditorValue() {
        if (isPushed) {
            JOptionPane.showMessageDialog(button, label + ": Ouch!");
        }
        isPushed = false;
        return label;
    }

    @Override
    public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
    }

    @Override
    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}
1

There are 1 answers

0
camickr On

I think the row is getting selected, but the button is not getting clicked.

Did you add any debug code to verify this? Add println(...) statements to check the value of "isPushed" this will tell you if the method is being executed or not.

You can use the Table Button Column class. It provide the renderer and editor for you. All you need to do is provide the Action you want to invoke when the button is clicked.

By providing an Action it makes the class more reusable as you don't hardcode the logic into the editor.

In the future when asking a question post a proper SSCCE demonstrating the problem. We can't execute your code because we don't have access to the database. The database is irrelevant to the question, so all you need to do is provide a JTable with hardcoded data to demonstrate the problem.