Click here to Skip to main content
15,883,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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:

Java
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 " };

	// the table model and the table:
	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) {
					// TODO Auto-generated catch block
					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);
	}

	// This code is called when the button is pushed.
	DBConnection connect = new DBConnection();

	private void addDataActionPerformed() throws SQLException {
//		for (int i = 0; i < 1; i++) {
			// creates a row array
			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);
//					row[5] = "me5";
//					System.out.println(res.getString(6));
//					System.out.println(res.getString(7));
					
					table.setRowHeight(30);

					
				}
					model.addRow(row);
					
				}
				

			
			// then adds the row to the DefaultTableModel
			
//		}
	}
	 
	public JComponent getComponent() {
		return mainPanel;
	}

	// start my app in a thread-safe way
	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.pack();

				frame.setLocationRelativeTo(null);

				frame.setVisible(true);

			}
		});
	}

}
Posted
Updated 19-Mar-14 2:01am
v5
Comments
Shubhashish_Mandal 18-Mar-14 13:35pm    
Google is the best place to start. At first do something , if stuck then post your problem.
baliram bhande 19-Mar-14 4:35am    
in that i want to add button in position row[5] =? how can i do that i am searchin on google but they can show example only hard code button in table please tell me is there any method .i am trying many method but they cant work in that situation

1 solution

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html[^]

Please read the tutorial.

the section "Using a Combo Box as an Editor" is also suitable for a JButton to be added.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900