我们如何在jtable列中添加jbutton [英] how we can add jbutton in jtable column
本文介绍了我们如何在jtable列中添加jbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究JTable,因为我正在动态增加行,因为所有列都是文本框但我想在任何一列中添加按钮以进行编辑和删除目的如何在taht中添加按钮
我的代码如下:
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以上代码我可以将数据传递到三个不同的列,数据来自数据库,它动态地不知道计数行。就像我想要动态地向第四列添加按钮我怎么能这样做。
来自OP:
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 " };
// 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);
}
});
}
}
推荐答案
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html [ ^ ]
请阅读教程。
使用组合框作为编辑器部分也适用于添加JButton。
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.
这篇关于我们如何在jtable列中添加jbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文