0

I have table which populated from database. How can i add a single button in last column?

Here is the code for jtable,

private void getData(){
        try{
            con = DatabaseConnection.ConnectDB();
            java.sql.Statement st = con.createStatement();
            String sql  = "SELECT * FROM mainTable WHERE status = 0";
            rs = st.executeQuery(sql);
            jTable1.setModel(DbUtils.resultSetToTableModel(rs));
        } catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    }
ARLEQUINA
  • 129
  • 1
  • 1
  • 11
  • learn this http://stackoverflow.com/questions/10620448/most-simple-code-to-populate-jtable-from-resultset – Youcef LAIDANI Mar 30 '17 at 20:18
  • [This one](http://stackoverflow.com/questions/13833688/adding-jbutton-to-jtable) might be helpful as well. – Pavlo Viazovskyy Mar 30 '17 at 20:20
  • Concentrate on one question at a time: http://stackoverflow.com/questions/43122112/get-all-data-from-database-in-textfield-when-jlist-item-selected. You still haven't asked a reasonable question in your last posting. Either update the question or delete it so people don't waste time trying to understand what you are asking. And post a proper [mcve] when you ask a question. – camickr Mar 30 '17 at 23:12

1 Answers1

1

I suggest you to start with simple example that would add the button into JTable cell. Later you can setModel() with resultset from database.

An example would demonstrate how to add JButton into JTable Cell.

public class JButtonTableExample {

public JButtonTableExample() {
    JFrame frame = new JFrame("JButtonTable Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][] {
        {
            "button 1",
            "foo"
        }, {
            "button 2",
            "bar"
        }
    }, new Object[] {
        "Button",
        "String"
    });

    JTable table = new JTable(dm);
    table.getColumn("Button").setCellRenderer(new ButtonRenderer());
    table.getColumn("Button").setCellEditor(new ButtonEditor(new JCheckBox()));


    JScrollPane scroll = new JScrollPane(table);

    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.getColumnModel().getColumn(0).setPreferredWidth(100);
    frame.add(scroll);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new JButtonTableExample();
        }
    });

 }
}


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();
 }
}
fabfas
  • 2,200
  • 1
  • 21
  • 21