gpt4 book ai didi

java - 如何在 swing java jframe 中制作 15000 个 jbutton 的数组,并使用按钮对每一行设置操作?

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:14 24 4
gpt4 key购买 nike

我正在使用 jframes jtable 和分页等在 java swing 中开发一个应用程序,我的数据库中有 15000 条记录,现在我想在 jtable 最后一列的每一行添加打印按钮,这样我就可以使用按钮列设置该行的操作。请帮助我如何制作 15000 个 jbutton 数组并将其添加到 swingajawa jframe 中的 jtable 列,以便我可以通过单击该按钮添加每行的操作?

我使用按钮渲染器和按钮编辑器类尝试了此代码

class ButtonRenderer extends JButton implements TableCellRenderer
{

public ButtonRenderer() {
// set button properties
setOpaque(true);
}


@Override
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean selected, boolean focused, int row, int col) {

// setText((obj==null) ? "":obj.toString());
if(selected)
{
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else
{
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((obj==null) ? "" :obj.toString());

return this;
}

}

class ButtonEditor extends DefaultCellEditor
{
protected JButton button;
private String label;
private boolean isPushed;

public ButtonEditor(JTextField txt) {
super(txt);
button = new JButton();
button.setOpaque(true);

button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
}
// override a couple of methods

@Override
public Component getTableCellEditorComponent(JTable table, Object obj,
boolean selected, int row, int col) {
// set text to button, set clicked to true, then return the btn object
if (selected)
{
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
}
else
{
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label=(obj==null) ? "" :obj.toString();
button.setText(label);
isPushed = true;
return button;
}
// if button cell value changes, if clicked that is

@Override
public Object getCellEditorValue() {
DisplayRecords dis = new DisplayRecords();
if(isPushed)

{
String query = "Select * from Voters";
int row = table.getSelectedRow();
String table_click = (table.getModel().getValueAt(row, 0).toString());
String query = "Select * from Voters where ID='"+table_click+"' ";
Connection connection = getConnection();
Statement st;

try
{
st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
String r1 = rs.getString(1);
dis.id.setText(r1);
dis.id.setEditable(false);
String r2 = rs.getString(2);
dis.t2.setText(r2);
dis.t2.setEditable(false);
String r3 = rs.getString(3);
dis.t3.setText(r3);
dis.t3.setEditable(false);
String r4 = rs.getString(4);
dis.t4.setText(r4);
dis.t4.setEditable(false);
String r5 = rs.getString(5);
dis.t5.setText(r5);
dis.t5.setEditable(false);
String r6 = rs.getString(6);
dis.t6.setText(r6);
dis.t6.setEditable(false);
String r7 = rs.getString(7);
dis.t7.setText(r7);
dis.t7.setEditable(false);
String r8 = rs.getString(8);
dis.t8.setText(r8);
dis.t8.setEditable(false);
String r9 = rs.getString(9);
dis.t9.setText(r9);
dis.t9.setEditable(false);
String r10 = rs.getString(10);
dis.t10.setText(r10);
dis.t10.setEditable(false);
String r11 = rs.getString(11);
dis.t11.setText(r11);
dis.t11.setEditable(false);
String r12 = rs.getString(12);
dis.t12.setText(r12);
dis.t12.setEditable(false);
String r13 = rs.getString(13);
dis.t13.setText(r13);
dis.t13.setEditable(false);
String r14 = rs.getString(14);
dis.t14.setText(r14);
dis.t14.setEditable(false);
String r15 = rs.getString(15);
dis.t15.setText(r15);
dis.t15.setEditable(false);
String r16 = rs.getString(16);
dis.t16.setText(r16);
dis.t16.setEditable(false);
String r17 = rs.getString(17);
dis.t17.setText(r17);
dis.t17.setEditable(false);
String r18 = rs.getString(18);
dis.t18.setText(r18);
dis.t18.setEditable(false);
String r19 = rs.getString(19);
dis.t19.setText(r19);
dis.t19.setEditable(false);
String r20 = rs.getString(20);
dis.t20.setText(r20);
dis.t20.setEditable(false);
}
}
catch (SQLException ex)
{
System.out.println("Print Button Click exception");
}
dis.setVisible(true);
dis.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// JOptionPane.showMessageDialog(button, label+"Clicked");
}
// set it to false now that its clicked
isPushed = false;
return new String(label); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean stopCellEditing() {
//set clicked to false
isPushed = false;
return super.stopCellEditing(); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected void fireEditingStopped() {

super.fireEditingStopped(); //To change body of generated methods, choose Tools | Templates.
}
}

但我无法针对每一行设置按钮操作

最佳答案

but now i want to add print button to print records of that row...

那么BOND给出的建议有什么问题呢?

但是,如果您确实想要在表格的每一行上都有一个单独的按钮,您可以使用 Table Button Column 中的渲染器/编辑器。它允许您在列中呈现按钮并将操作与该按钮关联。

关于java - 如何在 swing java jframe 中制作 15000 个 jbutton 的数组,并使用按钮对每一行设置操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260524/

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