Swing中的表单元格编辑代码给出了错误的输出

时间:2015-06-17 13:59:44

标签: java swing tablecellrenderer tablecelleditor

我已经编写了这段代码但是没有在点击时显示对话框。我认为该行已被选中,但按钮未被点击。

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 个答案:

答案 0 :(得分:1)

  

我认为该行已被选中,但该按钮未被点击。

您是否添加了任何调试代码来验证这一点?添加println(...)语句来检查&#34; isPushed&#34;的值。这将告诉您该方法是否正在执行。

您可以使用Table Button Column课程。它为您提供渲染器和编辑器。您需要做的就是在单击按钮时提供要调用的Action

通过提供Action,它可以让课程更加可重复使用,因为您不会将逻辑硬编码到编辑器中。

将来在问题发布时,SSCCE显示问题。我们无法执行您的代码,因为我们无法访问数据库。数据库与问题无关,因此您只需提供一个带有硬编码数据的JTable来演示问题。