결성! 카르텔

Frame.java

MyJButton selected; 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here: 
        panel2.removeAll(); 
        panel2.updateUI(); 
        MyJButton btn = new MyJButton(new Visitor(tf1.getText(),tf2.getText())); 
        btn.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent e) { 
                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
                selected = (MyJButton) e.getSource(); 
                tf1.setText(selected.getVs().getName()); 
                tf2.setText(selected.getVs().getTel()); 
            } 
    }); 
        clearTf(); 
        panel1.add(btn); 
        panel1.updateUI(); 
}                                         
 
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here: 
        panel2.removeAll(); 
        panel2.updateUI(); 
        if(selected == null) { 
            return; 
        } 
        panel1.remove(selected); 
        selected = null; 
        panel1.updateUI(); 
        clearTf(); 
}                                         
 
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here: 
        panel2.removeAll(); 
        panel2.updateUI(); 
        if(selected == null) { 
            return; 
        } 
        Visitor vs = new Visitor(tf1.getText(),tf2.getText()); 
        selected.setVs(vs); 
        selected.refreshName(); 
         
        clearTf(); 
}                                         
 
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here: 
        panel2.removeAll(); 
         
        Component[] cpt = panel1.getComponents(); 
        int size = cpt.length; 
        MyJButton[] btn = new MyJButton[size]; 
        for(int i = 0; i < size; i++) { 
            btn[i] = (MyJButton) cpt[i]; 
        } 
         
        //MyJButton temp; 
        for(int i = 0; i < size; i++) { 
            if(btn[i].getVs().getName().contains(tf1.getText()) || btn[i].getVs().getTel().contains(tf2.getText())) { 
                panel2.add(new MyJButton(btn[i].getVs())); 
            } 
           
        } 
         
         
        panel1.updateUI(); 
        panel2.updateUI(); 
}                                         
private void clearTf() { 
        tf1.setText(""); 
        tf2.setText(""); 
}

MyJButton.java

/* 
 * To change this license header, choose License Headers in Project Properties. 
 * To change this template file, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
package kr.razberry; 
 
import javax.swing.JButton; 
 
/** 
 * 
 * @author capta 
 */ 
public class MyJButton extends JButton { 
    Visitor vs; 
 
    public MyJButton(Visitor vs) { 
        super(vs.getText()); 
        this.vs = vs; 
    } 
 
    public Visitor getVs() { 
        return vs; 
    } 
 
    public void setVs(Visitor vs) { 
        this.vs = vs; 
    } 
     
    public void refreshName() { 
        this.setText(vs.getText()); 
    } 
     
} 

Visitor.java

/* 
 * To change this license header, choose License Headers in Project Properties. 
 * To change this template file, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
package kr.razberry; 
 
/** 
 * 
 * @author capta 
 */ 
public class Visitor { 
     
    private String name; 
     
    private String tel; 
 
    public Visitor(String name, String tel) { 
        this.name = name; 
        this.tel = tel; 
    } 
     
 
    /** 
     * Get the value of tel 
     * 
     * @return the value of tel 
     */ 
    public String getTel() { 
        return tel; 
    } 
 
    /** 
     * Set the value of tel 
     * 
     * @param tel new value of tel 
     */ 
    public void setTel(String tel) { 
        this.tel = tel; 
    } 
 
 
    /** 
     * Get the value of name 
     * 
     * @return the value of name 
     */ 
    public String getName() { 
        return name; 
    } 
 
    /** 
     * Set the value of name 
     * 
     * @param name new value of name 
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 
     
    public String getText() { 
        return name + "/" + tel; 
    } 
 
}