gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:24:55 24 4
gpt4 key购买 nike

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

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();
}
}

最佳答案

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

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

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

通过提供Action,它使类更具可重用性,因为您无需将逻辑硬编码到编辑器中。

以后提问时适当贴出SSCCE演示问题。我们无法执行您的代码,因为我们无权访问数据库。数据库与问题无关,因此您所需要做的就是提供一个包含硬编码数据的 JTable 来演示问题。

关于java - Swing 中的表格单元格编辑代码给出错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com