我已经开发了一个应用程序,我从数据库中获取数据并填充到 jtable 中。但问题在于渲染复选框。我已经为选择所有复选框进行了编码,但它在 UIManager 中给出了错误。尝试了很多但没有用。代码是: -
public jtable1_1() throws Exception {
DBEngine dbengine = new DBEngine();
data = dbengine.getcandidatereport();
//create header for the table
header = new Vector<String>();
header.add("check");
header.add("Name");
header.add("UserID"); //Empid
header.add("EName"); // employee position
header.add("LeadName");
initComponents();
TableColumn tc = jTable1.getColumnModel().getColumn(0);
tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
tc.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
tc.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
int row = jTable1.getSelectedRow();
jTextField1.setText(jTable1.getModel().getValueAt(row,1).toString());
jTextField2.setText(jTable1.getModel().getValueAt(row,2).toString());
jTextField3.setText(jTable1.getModel().getValueAt(row,3).toString());
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new jtable1_1().setVisible(true);
}catch(Exception e){e.printStackTrace();}
}
});
}
public class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if (source instanceof AbstractButton == false) {
return;
}
boolean checked = e.getStateChange() == ItemEvent.SELECTED;
for (int x = 0, y = jTable1.getRowCount(); x < y; x++) {
jTable1.setValueAt(new Boolean(checked), x, 0);
}
}
}
public class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
protected CheckBoxHeader rendererComponent;
protected int column;
protected boolean mousePressed = false;
public CheckBoxHeader(ItemListener itemListener) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
}
public Component getTableCellRendererComponent(JTable jTable1,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
if (jTable1 != null) {
JTableHeader header = jTable1.getTableHeader();
if (header != null) {
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
rendererComponent.setText("Check All");
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return rendererComponent;
}
protected void setColumn(int column) {
this.column = column;
}
public int getColumn() {
return column;
}
protected void handleClickEvent(MouseEvent e) {
if (mousePressed) {
mousePressed = false;
JTableHeader header = (JTableHeader) (e.getSource());
JTable tableView = header.getTable();
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = tableView.convertColumnIndexToModel(viewColumn);
if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
doClick();
}
}
}
public void mouseClicked(MouseEvent e) {
handleClickEvent(e);
((JTableHeader) e.getSource()).repaint();
}
public void mousePressed(MouseEvent e) {
mousePressed = true;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}