如何在JTable中修复JButton?因为按下按钮后会暂停在表格的最后位置

时间:2012-04-03 10:59:30

标签: java swing jtable jbutton tablecelleditor

我遇到JTableJButton的问题。 按下按钮后,将其悬挂在桌子的最后位置。 尝试运行下面的代码。

第一次打开“打开4”行并按“关闭4”按钮关闭。 之后,如果我打开“打开3”行,按钮“4”仍然存在......

如何解决这个问题?

    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.EventObject;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.*;
    import javax.swing.table.*;

    class MyRenderer implements TableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasValue, int row, int column) {
            DefaultTableModel model =((DefaultTableModel)table.getModel());
            if (model.getValueAt(row,0)!=null && column==0) { 
                String DATA= "Close "+(model.getValueAt(row,0)).toString();
                JButton b = new JButton(DATA);
                return b;
                }
            else return null;
            }
        }

    class MyEditor implements TableCellEditor {
        public Component getTableCellEditorComponent(final JTable table, Object value,
                                             boolean isSelected, final int row, int column){
            final DefaultTableModel model =((DefaultTableModel)table.getModel());    
            if (model.getValueAt(row,0)!=null && column==0 ){
                String text =model.getValueAt(row,0).toString();
                final JButton b = new JButton(text);
                ActionListener AKCJA = new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        if(model.getValueAt(row+1,0)==null)
                            while(model.getValueAt(row+1,0)==null)
                                ((DefaultTableModel)table.getModel()).removeRow(row+1);                        
                        }
                    };
                    b.addActionListener(AKCJA);
                    return b;
                    } 
            else  return null;            
            } 
        public void removeCellEditorListener(CellEditorListener cl) { }
        public void addCellEditorListener(CellEditorListener cl) { }
        public boolean shouldSelectCell(EventObject ev) { return true; }
        public boolean isCellEditable(EventObject ev) {return true;}
        public Object getCellEditorValue() { return null;}
        public boolean stopCellEditing() { return true;}
        public void cancelCellEditing() { }
        }

    @SuppressWarnings("serial")
    class MyJpanel extends JPanel{
        public MyJpanel(){
            super(new GridLayout(1,0));
            final DefaultTableModel model = new DefaultTableModel();          
            model.addColumn("Col1");
            model.addColumn("Col2");          

            for(int kk=0;kk<8;kk++)
                model.addRow(new Object[]{kk, "Open "+kk});

            final JTable table = new JTable(model);   
            TableColumn td = table.getColumnModel().getColumn(0);
            MyRenderer mojRenderer = new MyRenderer();
            td.setCellRenderer(mojRenderer);
            MyEditor mojEditor = new MyEditor();
            td.setCellEditor(mojEditor);

            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int viewRow = table.getSelectedRow();
                            if(event.getValueIsAdjusting())
                                 if((model.getValueAt(viewRow,0))!=null)
                                     for(int i=0;i<2;i++)
                                         model.insertRow(viewRow+i+1,new Object[]{null, "b"});               
                            }
                        }         
                    );
            add(table );
            }
        }

    public class Test{
        public static void main(String[] args)
        {
            final JFrame ramka=new JFrame("xxx");
            ramka.getContentPane().add(new MyJpanel());
            ramka.setSize(500,200);
            ramka.setVisible(true);
            ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        }

1 个答案:

答案 0 :(得分:0)

解决方案是Jtable容器中的移除按钮,例如在按钮table.removeAll();之前使用ActionListener()

找到问题非常有用: Component[] Komponenty = table.getComponents(); 您可以打印Array Komponenty并找到您忘记删除或隐藏的组件;