如何取消选中我用作 header 列之一的 JCheckBox?
How do I uncheck a JCheckBox that I am using as one of my header columns?
我的 JCheckBox "check all" header 列的工作原理是,如果我选中该框,则此列中的所有复选框都会被选中。我想这样做,如果您选中复选框 header 以便列中的所有框都被选中,取消选中列中的一个复选框后将取消选中复选框 header。
public class TablePanel extends javax.swing.JPanel {
public TablePanel() {
initComponents();
populateStringArrays(); //populate string arrays that will populate my table
table.setModel(tableModel);
table.getTableHeader().setReorderingAllowed(false);
setWidthOfTable();
loadTable(); //loads table with my array
table.getColumnModel().getColumn(5).setHeaderRenderer(new BoxRenderer(new MyItemListener()));
}
public class BoxRenderer extends JCheckBox implements TableCellRenderer, MouseListener{
String columnTitle = "Rings";
protected BoxRenderer rendererComponent;
int column;
boolean mousePressed = false;
public BoxRenderer(ItemListener listener){
rendererComponent = this;
rendererComponent.addItemListener(listener);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(table != null){
JTableHeader header = table.getTableHeader();
if(header != null){
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
Border border = header.getBorder();
header.setBorder(BorderFactory.createRaisedBevelBorder());
rendererComponent.setHorizontalTextPosition(SwingConstants.LEADING);
rendererComponent.setHorizontalAlignment(CENTER);
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
rendererComponent.setText(columnTitle);
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) {
((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) {
}
}
class MyItemListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if (source instanceof AbstractButton == false){
return;}
boolean checked = e.getStateChange() == ItemEvent.SELECTED;
System.out.println("Boolean checked = " + checked);
if(checked == true){
checkAll();
}else{
uncheckAll();
}
}
}
private void checkAll(){
for(int i=0; i<tableRecordArrayList.size(); i++){
table.getModel().setValueAt(true, i, 5);
}
}
private void uncheckAll(){
for(int i=0; i<tableRecordArrayList.size(); i++){
table.getModel().setValueAt(false, i, 5);
}
}
class TableModel extends javax.swing.table.AbstractTableModel{
/**Constructor**/
@Override
public int getRowCount() {
return tableRecordArrayList.size();
}
@Override
public int getColumnCount() {
return 6;
}
/**refresh for changes**/
public void refreshTable(){
fireTableDataChanged();
}
@Override
public Object getValueAt(int row, int column) {
TableRecord record = tableRecordArrayList.get(row);
switch(column){
case 0:
return record.firstName;
case 1:
return record.middleName;
case 2:
return record.lastName;
case 3:
return record.age;
case 4:
return record.height;
case 5:
return record.ring;
}
return "N/A";
}
@Override
public String getColumnName(int column){
/**set the title of the columns of the table**/
switch(column){
case 0:
return "First Name";
case 1:
return "Middle Name";
case 2:
return "Last Name";
case 3:
return "Age";
case 4:
return "Height(cm)";
case 5:
return "Has Championship Ring";
}
return "N/A";
}
/**turn column into checkboxes**/
@Override
public Class<?> getColumnClass(int column){
if(column == 5){
return Boolean.class;
}
return String.class;
}
@Override
public boolean isCellEditable(int row, int column){
return column == 5;
}
@Override
public void setValueAt(Object avalue, int row, int column){
TableRecord record;
Boolean v;
switch(column){
case 5:
record = tableRecordArrayList.get(row);
v = (Boolean) avalue;
record.ring = v;
fireTableCellUpdated(row,1);
printArrayList();
fireTableDataChanged();
break;
}
}
}
}
这个问题已经解决了。请访问此 link 以找到解决方案。
https://coderanch.com/t/700630/java/uncheck-JCheckBox-header-columns#3288215
I want to make it so that if you check the checkbox header so that all the boxes in the column become checked, uncheck one of the checkboxes in the column after will uncheck the checkbox header.
将 TableModelListener
添加到 TableModel
。
生成 TableModelEvent
时,您可以检查包含复选框的列中的数据是否已更改,然后在需要时重置 header 复选框。
查看:JTable -> TableModeListener 使用 TableModelListener
的基本示例。
我的 JCheckBox "check all" header 列的工作原理是,如果我选中该框,则此列中的所有复选框都会被选中。我想这样做,如果您选中复选框 header 以便列中的所有框都被选中,取消选中列中的一个复选框后将取消选中复选框 header。
public class TablePanel extends javax.swing.JPanel {
public TablePanel() {
initComponents();
populateStringArrays(); //populate string arrays that will populate my table
table.setModel(tableModel);
table.getTableHeader().setReorderingAllowed(false);
setWidthOfTable();
loadTable(); //loads table with my array
table.getColumnModel().getColumn(5).setHeaderRenderer(new BoxRenderer(new MyItemListener()));
}
public class BoxRenderer extends JCheckBox implements TableCellRenderer, MouseListener{
String columnTitle = "Rings";
protected BoxRenderer rendererComponent;
int column;
boolean mousePressed = false;
public BoxRenderer(ItemListener listener){
rendererComponent = this;
rendererComponent.addItemListener(listener);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(table != null){
JTableHeader header = table.getTableHeader();
if(header != null){
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
Border border = header.getBorder();
header.setBorder(BorderFactory.createRaisedBevelBorder());
rendererComponent.setHorizontalTextPosition(SwingConstants.LEADING);
rendererComponent.setHorizontalAlignment(CENTER);
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
rendererComponent.setText(columnTitle);
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) {
((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) {
}
}
class MyItemListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if (source instanceof AbstractButton == false){
return;}
boolean checked = e.getStateChange() == ItemEvent.SELECTED;
System.out.println("Boolean checked = " + checked);
if(checked == true){
checkAll();
}else{
uncheckAll();
}
}
}
private void checkAll(){
for(int i=0; i<tableRecordArrayList.size(); i++){
table.getModel().setValueAt(true, i, 5);
}
}
private void uncheckAll(){
for(int i=0; i<tableRecordArrayList.size(); i++){
table.getModel().setValueAt(false, i, 5);
}
}
class TableModel extends javax.swing.table.AbstractTableModel{
/**Constructor**/
@Override
public int getRowCount() {
return tableRecordArrayList.size();
}
@Override
public int getColumnCount() {
return 6;
}
/**refresh for changes**/
public void refreshTable(){
fireTableDataChanged();
}
@Override
public Object getValueAt(int row, int column) {
TableRecord record = tableRecordArrayList.get(row);
switch(column){
case 0:
return record.firstName;
case 1:
return record.middleName;
case 2:
return record.lastName;
case 3:
return record.age;
case 4:
return record.height;
case 5:
return record.ring;
}
return "N/A";
}
@Override
public String getColumnName(int column){
/**set the title of the columns of the table**/
switch(column){
case 0:
return "First Name";
case 1:
return "Middle Name";
case 2:
return "Last Name";
case 3:
return "Age";
case 4:
return "Height(cm)";
case 5:
return "Has Championship Ring";
}
return "N/A";
}
/**turn column into checkboxes**/
@Override
public Class<?> getColumnClass(int column){
if(column == 5){
return Boolean.class;
}
return String.class;
}
@Override
public boolean isCellEditable(int row, int column){
return column == 5;
}
@Override
public void setValueAt(Object avalue, int row, int column){
TableRecord record;
Boolean v;
switch(column){
case 5:
record = tableRecordArrayList.get(row);
v = (Boolean) avalue;
record.ring = v;
fireTableCellUpdated(row,1);
printArrayList();
fireTableDataChanged();
break;
}
}
}
}
这个问题已经解决了。请访问此 link 以找到解决方案。
https://coderanch.com/t/700630/java/uncheck-JCheckBox-header-columns#3288215
I want to make it so that if you check the checkbox header so that all the boxes in the column become checked, uncheck one of the checkboxes in the column after will uncheck the checkbox header.
将 TableModelListener
添加到 TableModel
。
生成 TableModelEvent
时,您可以检查包含复选框的列中的数据是否已更改,然后在需要时重置 header 复选框。
查看:JTable -> TableModeListener 使用 TableModelListener
的基本示例。