我正在使用jframes jtable和pagging等在java swing中开发一个应用程序, 我在我的数据库中有15000条记录现在我想在jtable的最后一列中的每一行添加打印按钮,这样我就可以用按钮列设置该行的动作。 请帮助我如何制作15000个jbuttons数组并将其添加到swing jawa 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.
}
}
但我无法针对每一行设置按钮操作
答案 0 :(得分:0)
但现在我想添加打印按钮来打印该行的记录......
那么BOND给出的建议有什么问题?
但如果你真的想在表格的每一行都有一个单独的按钮,你可以使用Table Button Column中的渲染器/编辑器。它允许您在列中呈现按钮并将Action与该按钮相关联。