按下按钮后,Java将颜色设置为jtable

按下按钮后,Java将颜色设置为jtable,java,Java,我有一个jtable,它有一个delete按钮,当我选择一行并单击delete按钮时,我希望将该行设置为绿色 但是,当我单击“离开”时,行颜色没有保留在那里,我选择的每一行都变成绿色,而不是默认的蓝色 public class table extends javax.swing.JFrame { DefaultTableModel model; private boolean btnClicked = false; public table() { i

我有一个jtable,它有一个delete按钮,当我选择一行并单击delete按钮时,我希望将该行设置为绿色

但是,当我单击“离开”时,行颜色没有保留在那里,我选择的每一行都变成绿色,而不是默认的蓝色

public class table extends javax.swing.JFrame {

    DefaultTableModel model;
    private boolean btnClicked = false;

    public table() {
        initComponents();
        model = (DefaultTableModel) tbltable.getModel();

    }

    public class CustomTableCellRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);


            if (isSelected) {

                cell.setBackground(getBackground());                
                String type = (String) String.valueOf(model.getValueAt(tbltable.getSelectedRow(), 3));
                if (("deleted").equals(type)) {
                    cell.setBackground(Color.GREEN);
                }

            }
            return cell;

        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtCol1 = new javax.swing.JTextField();
        txtCol2 = new javax.swing.JTextField();
        txtCol3 = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbltable = new javax.swing.JTable();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtCol1.setText("jTextField1");

        txtCol2.setText("jTextField2");

        txtCol3.setText("jTextField3");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        tbltable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Col1", "Col2", "Col3", "Col4"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbltable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbltableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbltable);

        btnEdit.setText("edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(txtCol1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtCol2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtCol3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAdd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEdit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDelete)))))
                .addContainerGap(283, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCol1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCol2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCol3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(95, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       

        String chkcol = "notdeleted";
        model.insertRow(model.getRowCount(), new Object[]{txtCol1.getText(), txtCol2.getText(), txtCol3.getText(), chkcol});
    }                                      

    private void tbltableMouseClicked(java.awt.event.MouseEvent evt) {                                      
        txtCol1.setText(String.valueOf(model.getValueAt(tbltable.getSelectedRow(), 0)));
        txtCol2.setText(String.valueOf(model.getValueAt(tbltable.getSelectedRow(), 1)));
        txtCol3.setText(String.valueOf(model.getValueAt(tbltable.getSelectedRow(), 2)));
        int rowIndex = tbltable.getSelectedRow();
        System.out.println("rowIndex " + rowIndex);
    }                                     

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        model.setValueAt(txtCol1.getText(), tbltable.getSelectedRow(), 0);
        model.setValueAt(txtCol2.getText(), tbltable.getSelectedRow(), 1);
        model.setValueAt(txtCol3.getText(), tbltable.getSelectedRow(), 2);
    }                                       
    TableColumn tcol;
    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        model.setValueAt("deleted", tbltable.getSelectedRow(), 3);

        if (String.valueOf(model.getValueAt(tbltable.getSelectedRow(), 3)).equals("deleted")) {
            tcol = tbltable.getColumnModel().getColumn(0);
            tcol.setCellRenderer(new CustomTableCellRenderer());

            tcol = tbltable.getColumnModel().getColumn(1);
            tcol.setCellRenderer(new CustomTableCellRenderer());

            tcol = tbltable.getColumnModel().getColumn(2);
            tcol.setCellRenderer(new CustomTableCellRenderer());

            tcol = tbltable.getColumnModel().getColumn(3);
            tcol.setCellRenderer(new CustomTableCellRenderer());
        }


    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new table().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbltable;
    private javax.swing.JTextField txtCol1;
    private javax.swing.JTextField txtCol2;
    private javax.swing.JTextField txtCol3;
    // End of variables declaration                   
}
公共类表扩展了javax.swing.JFrame{
默认表格模型;
私有布尔值btnClicked=false;
公共表(){
初始化组件();
model=(DefaultTableModel)tbltable.getModel();
}
公共类CustomTableCellRenderer扩展了DefaultTableCellRenderer{
公共组件GetTableCellRenderComponent(JTable表、对象obj、布尔isSelected、布尔hasFocus、int行、int列){
组件单元=super.getTableCellRenderComponent(表、对象、isSelected、hasFocus、行、列);
如果(当选){
cell.setBackground(getBackground());
字符串类型=(String)String.valueOf(model.getValueAt(tbltable.getSelectedRow(),3));
如果((“删除”)。等于(类型)){
单元格.背景(颜色.绿色);
}
}
返回单元;
}
}
@抑制警告(“未选中”)
//                           
私有组件(){
txtCol1=newjavax.swing.JTextField();
txtCol2=newjavax.swing.JTextField();
txtCol3=newjavax.swing.JTextField();
btnAdd=newjavax.swing.JButton();
jScrollPane1=newjavax.swing.JScrollPane();
tbltable=newjavax.swing.JTable();
btnEdit=newjavax.swing.JButton();
btnDelete=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setText(“jTextField1”);
setText(“jTextField2”);
setText(“jTextField3”);
btnAdd.setText(“添加”);
addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BTNADDACION(evt);
}
});
setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
},
新字符串[]{
“Col1”、“Col2”、“Col3”、“Col4”
}
) {
类[]类型=新类[]{
java.lang.Object.class、java.lang.Object.class、java.lang.Object.class、java.lang.String.class
};
公共类getColumnClass(int columnIndex){
返回类型[列索引];
}
});
tbltable.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
tbltableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tbltable);
btnEdit.setText(“编辑”);
addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BTNEDITACTION(evt);
}
});
btnDelete.setText(“删除”);
btnDelete.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BTN删除操作(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(92,92,92)
.addComponent(txtCol1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(18,18,18)
.addComponent(txtCol2,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtCol3,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(87,87,87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED\u SIZE,452,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnDelete()()))
.addContainerGap(283,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32,32,32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtCol1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addComponent(txtCol2,javax.swing.GroupLayout.PREFERRED_
table.removeColumn( table.getColumn(...) );
table.getModel().getValueAt(...);