i working on JTable in that i am increasing rows dynamically in that all column are textbox but i want to add button in any one column for edit and delete purpose how can i add button in taht
my code is bellow
row[0] = res.getString(1);
row[1] = res.getString(2);
row[2] = res.getString(3);
i above code i can pass data to three different column that data getting from data base its dynamically dont know count row .as like that i want to add button to fourth column dynamically how can i do that .
Addition from OP:
public class View_Company {
public static JFrame frame;
public static final String[] COLUMN_NAMES = { "Company Name", "Email Id",
"Contact Number", "Address", "No of Department",
"No of Employees " };
private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
private JTable table = new JTable(model);
public static JComboBox b, b1, b2;
private JPanel mainPanel = new JPanel(new BorderLayout());
private Random random = new Random();
public View_Company() {
;
JPanel buttonPanel = new JPanel();
JButton addDataButton = new JButton("Show Data");
buttonPanel.add(addDataButton);
addDataButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
addDataActionPerformed();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
table = new JTable(model) {
private static final long serialVersionUID = 1L;
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.red));
}
return c;
}
};
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.PAGE_START);
}
DBConnection connect = new DBConnection();
private void addDataActionPerformed() throws SQLException {
Object[] row = new Object[COLUMN_NAMES.length];
connect.getConnection();
PreparedStatement stmt = connect.conn.prepareStatement("select c.company_name,c.email_id,c.contact_no,c.address,count(distinct d.dept_id),count(distinct e.emp_id) from payroll.company_details c left join payroll.department d on c.company_id=d.company_id left join payroll.employee_master e on d.dept_id=e.dept_id group by (c.company_name)");
ResultSet res=stmt.executeQuery();
while(res.next())
{
for (int j = 0; j < row.length; j++) {
System.out.println(res.getString(1));
System.out.println(res.getString(2));
System.out.println(res.getString(3));
System.out.println(res.getString(4));
System.out.println(res.getString(5));
row[0] = res.getString(1);
row[1] = res.getString(2);
row[2] = res.getString(3);
row[3] = res.getString(4);
row[4] = res.getString(5);
row[5]=res.getString(6);
table.setRowHeight(30);
}
model.addRow(row);
}
}
public JComponent getComponent() {
return mainPanel;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame = new JFrame("DefaultTableModelDemo");
frame.getContentPane().add(new View_Company().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 650);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}