gpt4 book ai didi

java - 复选框渲染器

转载 作者:行者123 更新时间:2023-12-02 07:21:17 24 4
gpt4 key购买 nike

我开发了一个应用程序,其中我从数据库中获取数据并填充到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) {
}
}

最佳答案

public class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {

关于java - 复选框渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14181459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com